Search code examples
c#.net-corenlog

Changing loglevel in appsettings.json won't get applied to NLog


I am using NLog with dotnet core app configured like described here. https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

In nlog.config if I set minLevel to Error and launch my app I can see on logger that logDebug is disabled.

If I restore back to trace then all levels are set to true (error, trace, debug, info, warning)

and if I put this into config file (appsettings.json)

"Logging": {
        "LogLevel": {
            "Default": "Error",
            "Microsoft": "Information"
        }
    }

then nothing happens, all logger levels are still set to true, what might be wrong ? enter image description here


Solution

  • As Snakefoot mentioned,

    this is the config of Microsoft Extension Logging.

    When using NLog + Injected .NET Core loggers (using Microsoft Extension Logging), the flow is as follows:

    ILogger<MyClass>LoggerFactory in Microsoft Extension Logging → LogFactory in NLog.

    To configure NLog, you should update the NLog <rules> in your nlog.config, e.g.

    (also possible from code, see docs)

    <rules>
       <logger name="*" minlevel="debug" />
    </rules>
    

    Please note that you won't see debug logs if the minlevel is "debug" in NLog and "error" the appsettings.json

    note 2: When using NLog direct, e.g. the NLog.LogManager.GetCurrentClassLogger(), you only need to configure NLog and the appsettings.json config isn't used for those logs.