DEV Community


The new .Net 5 Console Logging registation

deeja profile image Daniel Blackwell ・1 min read

.Net 5 has changed the way you can set up the console for logging.

You may have received this message on your change to .Net 5:

ConsoleLoggerOptions.TimestampFormat has been deprecated. Please use ConsoleFormatterOptions.TimestampFormat instead.

The builder.UseConsole() now looks like this under the covers:

// Microsoft.Extensions.Logging.ConsoleLoggerExtensions
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder)
    builder.AddConsoleFormatter<JsonConsoleFormatter, JsonConsoleFormatterOptions>();
    builder.AddConsoleFormatter<SystemdConsoleFormatter, ConsoleFormatterOptions>();
    builder.AddConsoleFormatter<SimpleConsoleFormatter, SimpleConsoleFormatterOptions>();
    builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ConsoleLoggerProvider>());
    LoggerProviderOptions.RegisterProviderOptions<ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services);
    return builder;
Enter fullscreen mode Exit fullscreen mode

So if you are wanting to change formatting, you can replace the services.AddConsole() with these extension methods:

services.AddLogging(opt =>
    // Provided by Microsoft.Extensions.Logging.Console
    opt.AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ");
    opt.AddJsonConsole(options => { });
    opt.AddSystemdConsole(options => { });
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide