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

As @MartinUllrich already mentioned this line break can't be disabled and you have to implement a custom logger to avoid it.

Registration:

loggerFactory.AddProvider(new CustomLoggerProvider());

The implementation (can be extended with using of the original ConsoleLogger source code - for example, you could add the GetLogLevelConsoleColors method):

public class CustomLoggerProvider : ILoggerProvider
{
    public void Dispose() { }

    public ILogger CreateLogger(string categoryName)
    {
        return new CustomConsoleLogger(categoryName);
    }

    public class CustomConsoleLogger : ILogger
    {
        private readonly string _categoryName;

        public CustomConsoleLogger(string categoryName)
        {
            _categoryName = categoryName;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            Console.WriteLine($"{logLevel}: {_categoryName}[{eventId.Id}]: {formatter(state, exception)}");
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return true;
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }
    }
}

At the moment, this not configurable. The source code is here on GitHub:

logBuilder.Append(logName);
logBuilder.Append("[");
logBuilder.Append(eventId);
logBuilder.AppendLine("]");

If you want that, you need to write your own logger. However you can just copy the source code of the console logger, modify as needed and change the namespaces so it doesn't interfere with the version Microsoft ships.

You can also open an issue on the logging repo to ask for this option.


This is updated in .NET 5: https://docs.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter. Now provides simple, systemd and json


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...