I use Serilog for logging in most of my console applications as specified here, however the AddConsole and AddDebug extension methods are now obsolete in .NET Core 2.2 giving the below errors.

‘ConsoleLoggerExtensions.AddConsole(ILoggerFactory)’ is obsolete: ‘This method is obsolete and will be removed in a future version. The recommended alternative is AddConsole(this ILoggingBuilder builder).’

‘ConsoleLoggerExtensions.AddDebug(ILoggerFactory)’ is obsolete: ‘This method is obsolete and will be removed in a future version. The recommended alternative is AddDebug(this ILoggingBuilder builder).’

serviceCollection.AddSingleton(new LoggerFactory()
	.AddConsole()
	.AddSerilog()
	.AddDebug());
serviceCollection.AddLogging();

After a bit of research it turns out I just needed to make use of the ILoggingBuilder constructor accessible through the AddLogging method of the service collection.

serviceCollection.AddLogging(loggingBuilder =>
{
	loggingBuilder.AddConsole();
	loggingBuilder.AddSerilog();
	loggingBuilder.AddDebug();
});

4 Comments

Derek Broughton · 8th May 2019 at 2:38 pm

Thanks so much. As usual, Microsoft’s error message leaves an awful log up to your imagination!

Dasith · 2nd August 2019 at 4:24 am

You can also follow this method if you want to instantiate it outside of the DI.
`var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole())`

    Shinigami · 2nd August 2019 at 10:47 am

    Thanks, hadn’t seen that documentation before. Looks like it’s going to change again for 3.0.

Leave a Reply

Your email address will not be published. Required fields are marked *