Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate AddNLog from .NET Core 2.2 to 3.0

Tags:

In the Startup class of my project I have the following Configure method:

private void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    loggerFactory.AddNLog();
    ...
}

This worked OK in .NET Core 2.2, but after upgrading to 3.0, I get the warning Method 'NLog.Extensions.Logging.ConfigureExtenstions.AddNLog' is obsolete: instead use ILoggingBuilder.AddNLog() or IHostBuilder.UseNLog().

So I tried to update the method to

private void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggingBuilder loggingBuilder)
{
    ...
    loggingBuilder.AddNLog();
    ...
}

or

private void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostBuilder hostBuilder)
{
    ...
    hostBuilder.UseNLog();
    ...
}

But in both cases I got a DI exception: Could not resolve a service of type {Microsoft.Extensions.Logging.ILoggingBuilder/Microsoft.Extensions.Hosting.IHostBuilder} for the parameter {loggingBuilder/hostBuilder} of method 'Configure' on type 'MyProject.Startup'.

I could not find any viable source on how to change the NLog configuration fro .NET Core 3.0 and there is nothing about logging in the official Microsoft guide. Does anyone know how to solve this issue?

like image 480
Storm Avatar asked Nov 26 '19 09:11

Storm


1 Answers

With ASP.NET Core 2+, the pattern to bootstrap an ASP.NET Core site has changed. NLog adapts that. With the latest version of NLog.Extensions.Logging.ConfigureExtenstions, the old methods are made obsolete.

For example, ASP.NET Core nowadays uses a CreateHostBuilder.

I would recommend to follow:

  1. Migrate from ASP.NET Core 2.2 to 3.0 | Microsoft Docs
  2. And then: Getting started with ASP.NET Core 3 · NLog/NLog Wiki
like image 84
Julian Avatar answered Oct 11 '22 17:10

Julian