Nlog: LogLevel setting confusion

Created on 9 Dec 2019  ·  8Comments  ·  Source: NLog/NLog

I am using ASP.NET Core 3 and trying to figure out where log level is set.

I read that logging.SetMinimumLevel is overwritten by appsettings.development.json however when I set LogLevel default to None I still seem to get logs locally.

Within my nlog.config I am setting minlevel within the rules and this does seem to be respected however.
i.e. (this rule tells nlog to send an email to me)

Even if my appsettings.development.json looks like this, it is ignored

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "None",
      "Microsoft": "None",
      "Microsoft.Hosting.Lifetime": "None"
    }
  }
}

I am setting it up via IHostBuilder such as

c# public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .ConfigureLogging((hostingContext, logging) => { logging.ClearProviders(); //logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Critical); //logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); }) .UseNLog(); // NLog: Setup NLog for Dependency injection

Is the appsettings log level even respected? (thanks)

nlog-configuration question

All 8 comments

If you are having issue with understanding how to setup Microsoft Extension Logging LogLevel-Filtering, then this forum might not be the right one. Especially when you have no issue configuring NLog-minLevels.

If you only want NLog as a single LoggingProvider, then you can consider doing this (Disables MEL-LogLevel filtering completely):

c# public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }) .ConfigureServices(collection => { collection.AddSingleton<ILoggerFactory>(services => new NLog.Extensions.Logging.NLogLoggerFactory()); }) .UseNLog(); // NLog: Setup NLog for Dependency injection

Else we have this wiki-page about AspNetCore3:

https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

Are you saying the settings within appsettings are only relevant to Microsoft logging? Do they have any bearing on NLog level logging?

This section implies NLog: setup NLog for Dependency injection
”5. Configure appsettings.json”

Clarification would be really appreciated. Thanks

Thx

Microsoft Extension Logger (MEL) is the abstraction layer, and NLog is the implementation of a LoggingProvider.

MEL keeps it logging-filters in appsettings.json, and only forwards logevents to NLog that passes those filters.

Thanks.
What I have found is that changes to these appsettings key-values do not have any impact on logs that are generated. Is that normal/expected?

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "None",
      "Microsoft": "None",
      "Microsoft.Hosting.Lifetime": "None"
    }
  }
}

Again you are asking about how to configure MEL logging filters, but this is a forum about NLog. Maybe ask Microsoft or Stackoverflow.

But the idea is that MEL always wins. If not then you are probably doing something wrong. Maybe try the AspNetCore3 example project that I linked to just above.

What I have found is that changes to these appsettings key-values do not have any impact on logs that are generated. Is that normal/expected?

Those are respected, but not I depends:

  • When logging to Microsoft Extension Logger (MEL) , the logging is first filtered by MEL (those appsettings.json), and after that by NLog.
  • When directly logging to NLog, those settings in the appsettings aren't used.

Please note that you could also setup NLog from the appsettings.json (NLog section, not Logging), see https://github.com/NLog/NLog.Extensions.Logging/wiki/Json-NLog-Config

Hope this helps with the confusion! :)

Closing this question as answered. Please re-open if not.

NLog.Extension.Logging ver. 1.7.2 now supports the NLogProviderOptions ReplaceLoggerFactory and RemoveLoggerFactoryFilter.

And the goal is to change default-value for RemoveLoggerFactoryFilter=true. See also: https://github.com/NLog/NLog.Extensions.Logging/issues/483

Was this page helpful?
0 / 5 - 0 ratings