Home > database >  NLog ignores appsettings.json within an MVC controller
NLog ignores appsettings.json within an MVC controller

Time:02-21

According to the documentation I setup my app to use NLog for logging:

IHost host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.CaptureStartupErrors(true);
        webBuilder.UseSetting(WebHostDefaults.PreventHostingStartupKey, "true");
        webBuilder.UseStartup<Startup>();
        webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
    })
    .ConfigureLogging((context, logging) =>
    {
        var configuration = context.Configuration.GetSection("NLog");
        if (configuration.Exists())
            NLog.LogManager.Configuration = new NLogLoggingConfiguration(configuration);
        logging
            .ClearProviders()
            .SetMinimumLevel(LogLevel.Trace);
    })
    .UseNLog()
    .Build();
host.StartAsync();

After the host have been built the LogManager.Configuration corresponds with NLog section of my appsettings.json. But it contains null within a controller, so the controller doesn't write logs. The log contains only the following records:

2022-02-21 10:16:46.6273 INFO Microsoft.Hosting.Lifetime Now listening on: ...
2022-02-21 10:16:46.6498 INFO Microsoft.Hosting.Lifetime Application started. Press Ctrl C to shut down.
2022-02-21 10:16:46.6498 INFO Microsoft.Hosting.Lifetime Hosting environment: Development
2022-02-21 10:16:46.6498 INFO Microsoft.Hosting.Lifetime Content root path: ...

Interesting the internal log says the logger had been shut down before host was stopped:

2022-02-21 10:16:35.8017 Info Auto loading assembly file: ...\bin\Debug\net5.0\NLog.Extensions.Logging.dll
2022-02-21 10:16:35.8017 Info Loading assembly file: ...\bin\Debug\net5.0\NLog.Extensions.Logging.dll
2022-02-21 10:16:35.8187 Info NLog.Extensions.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 1.7.4.1610. Product version: 1.7.4 e2bffa9e949fb4760d75aca224e78631c063f087. GlobalAssemblyCache: False
2022-02-21 10:16:35.8187 Info Auto loading assembly file: ...\bin\Debug\net5.0\NLog.Extensions.Logging.dll succeeded!
2022-02-21 10:16:35.8187 Info Auto loading assembly file: ...\bin\Debug\net5.0\NLog.Web.AspNetCore.dll
2022-02-21 10:16:35.8284 Info Loading assembly file: ...\bin\Debug\net5.0\NLog.Web.AspNetCore.dll
2022-02-21 10:16:35.8284 Info NLog.Web.AspNetCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.14.0.2042. Product version: 4.14.0 18bbcdbd5bd13b4565c32d72e09502b7ca6c71d4. GlobalAssemblyCache: False
2022-02-21 10:16:35.8284 Info Auto loading assembly file: ...\bin\Debug\net5.0\NLog.Web.AspNetCore.dll succeeded!
2022-02-21 10:16:35.8284 Info Message Template Auto Format enabled
2022-02-21 10:16:35.8549 Info Adding target FileTarget(Name=file)
2022-02-21 10:16:35.8634 Info Validating config: TargetNames=file, ConfigItems=18
2022-02-21 10:16:46.6575 Info AppDomain Shutting down. Logger closing...
2022-02-21 10:16:46.6575 Info Logger has been shut down.

Why it writes "AppDomain Shutting down" when the app is continuing to execute?

The logging works fine if I use nlog.config instead of appsettings.json, but I have to configure NLog with appsettings.json. So, I need your help to fix the issue.

CodePudding user response:

You can configure NLog to depend on lifetime of the Microsoft LoggerFactory like this:

.ConfigureLogging((context, logging) =>
{
    NLog.LogManager.AutoShutdown = false; // Unhook from AppDomain, to depend on host
    var configuration = context.Configuration.GetSection("NLog");
    if (configuration.Exists())
        NLog.LogManager.Configuration = new NLogLoggingConfiguration(configuration);
    logging
        .ClearProviders()
        .SetMinimumLevel(LogLevel.Trace);
})
.UseNLog(new NLogAspNetCoreOptions() { ShutdownOnDispose = true }) // Depend on Host
.Build();

The important settings are AutoShutdown and ShutdownOnDispose. When using NLog.Web.AspNetCore v5.0 then the settings will be configured by default.

  • Related