.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 useConsoleFormatterOptions.TimestampFormat
instead.
The builder.UseConsole()
now looks like this under the covers:
// Microsoft.Extensions.Logging.ConsoleLoggerExtensions
public static ILoggingBuilder AddConsole(this ILoggingBuilder builder)
{
builder.AddConfiguration();
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;
}
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 => { });
});
Top comments (0)