Is there a way to format the output format in .NET Core logging?

Viewed 28703

I'm using the built in logging provider for logging into the console (Microsoft.Extensions.Logging.Console) in a .NET Core console application.

Each logging entry produces two lines in the output. I would like to have each entry in one single line. Is there a way to customize the output format?

Here is an example how I use it:

static void Main(string[] args)
{
    var serviceProvider = new ServiceCollection()
      .AddLogging() // This adds the Microsoft logging.
      .AddSingleton<IProjectGeneratorService, CSharpProjectGeneratorService>()
      .BuildServiceProvider();

    // Configure the console logging.
    serviceProvider
      .GetService<ILoggerFactory>()
      .AddConsole(LogLevel.Debug);

    // Write a logging entry
    var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<Program>();
    logger.LogDebug("Application started...");
}

What I get is:

dbug: Generator.Program[0]
      Application started...

What I would like to have is something like this:

dbug: Generator.Program[0]: Application started...

Any idea? I know, I could write a custom logger, but I would like to know if there is an other way.

Thanks.

7 Answers

Although you can't specify your own custom format, it does support an alternative "systemd" format which you can select like this:

logging.AddConsole(options => {
  options.Format=ConsoleLoggerFormat.Systemd;
});

This outputs each log entry on one line even if you the text has newlines in it (so exceptions aren't very pretty). It also doesn't use colors which is an advantage if you're redirecting to a file.

This is now easy to do with SimpleConsoleFormatter from the Microsoft.Extensions.Logging.Console 5.0.0 package.

The source code of SimpleConsoleFormatter is here on Github.

Example:

static void Main(string[] args)
{
      var serviceProvider = new ServiceCollection()
            .AddLogging(loggingBuilder => loggingBuilder
                .AddSimpleConsole(formatterOptions =>
                {
                    formatterOptions.SingleLine = true;
                })
                .SetMinimumLevel(LogLevel.Debug))
            .BuildServiceProvider();

      // Write a logging entry
      var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
      logger.LogDebug("Application started...");
}

Output:

dbug: Generator.Program[0]: Application started...

Starting from .NET 5 you can configure logging formatters to write each log entry in a single line. Use SingleLine property of Formatter to configure it

For example, you can configure console formatter in your appsettings.json without writing any additional code:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "Console": {
      "FormatterName": "simple",
      "FormatterOptions": {
        "SingleLine": true,
        "TimestampFormat": "HH:mm:ss "
      }
    }
  }
}

You can find more information in Microsoft Docs:

Related