Search code examples
.net-corenlogilogger

Manage logging configuration with NLog in .NET Core 3


I'm using NLog in a .NET Core 3.1 worker service application. Following the tutorial of NLog I inserted an nlog.config file to manage the configuration.

Now I'm confused because I have three points where I configure the logging:

  1. In the code where I need to create a logger in a dependency injection context

    // Other code...
    
    services.AddScoped<IApplyJcdsCommandsJob, ApplyJcdsCommandsJob>(provider =>
    {
        var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder
                .ClearProviders()
                .AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Trace)
                .AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Trace)
                .AddFilter("ApplyJcdsCommandsJob", Microsoft.Extensions.Logging.LogLevel.Trace)
                //.AddConsole()
                //.AddEventLog();
                .AddNLog(configuration);
        });
    
        Microsoft.Extensions.Logging.ILogger logger = loggerFactory.CreateLogger<CommandsJob>();
    
        return new CommandsJob(logger);
    })
    
    // Other code...
    
  2. In appSettings.json

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Trace",
          "System": "Trace",
          "Microsoft": "Trace"
        }
      }
    }
    
  3. In NLog.config

    The default config file produced by the nuget package installation:

    <!-- a section of the config -->
    <targets>
        <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    </targets>
    
    <rules>
        <logger name="*" minlevel="Trace" writeTo="f" />
    </rules>
    <!-- ... -->
    

What I see is that if I remove the Nlog.config file, the log file will not be created. Other changes seam to have no effect.

How are this configurations related? What is the best way to switch on/off the logging and set the level?


Solution

  • People that decide to use NLog usually also want to disable all MEL-filtering to avoid the confusion with two filtering systems. So the NLog wiki-tutorial is targeted those users.

    I guess people who are MEL-users first will probably just use new HostBuilder().CreateDefaultBuilder().Build() (Will setup everything with all guns enabled).

    But if staying with the simple example, then you need to remove:

    loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);

    And add:

    loggingBuilder.AddConfiguration(config.GetSection("Logging"));

    So it looks like this:

    serviceCollection.AddLogging(loggingBuilder =>
    {
       loggingBuilder.ClearProviders();
       loggingBuilder.AddConfiguration(config.GetSection("Logging"));
       loggingBuilder.AddNLog(config);
    })
    

    ILoggingBuilder.AddConfiguration can be found at Nuget: Microsoft.Extensions.Logging.Configuration