question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No effect setting loglevel from appSettings.json

See original GitHub issue

Hi, I’m trying to combine the log configuration from code/appSettings.json/NLog.config in a .net core console application (.NET Core 3.1). I started from the online example adding the load of the appSettings.json. So I have

In the code

// Inspecting config at runtime I see the value: [0] = {[Logging:LogLevel:Default, Warning]}
//...
.AddLogging(loggingBuilder =>
{
	loggingBuilder.ClearProviders();
	loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
	loggingBuilder.AddNLog(config);
})

In appSettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

In Nlog.config

<logger name="*" minlevel="Trace" writeTo="f" />

I expected to have an output filtered on Warning level, but I have all the levels contained in the code; 2020-01-16 11:54:26.9520 TRACE Doing hard work! Action1 2020-01-16 11:54:26.9881 DEBUG Doing hard work! Action1 2020-01-16 11:54:26.9881 INFO Doing hard work! Action1 2020-01-16 11:54:26.9881 WARN Doing hard work! Action1 2020-01-16 11:54:26.9881 ERROR Doing hard work! Action1 2020-01-16 11:54:26.9881 FATAL Doing hard work! Action1

It works when I change the min level in code or nlog.config from trace to warning: 2020-01-16 12:27:47.1587 WARN Doing hard work! Action1 2020-01-16 12:27:47.1927 ERROR Doing hard work! Action1 2020-01-16 12:27:47.1927 FATAL Doing hard work! Action1

What am I doing wrong? This is the simple project: ConsoleAppLog.zip

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
toniopettinatocommented, Jan 17, 2020

Perfect, it works with the AddConfiguration() I agree, it is probably not convenient to have multiple configurations (MEL + NLog). I did it just to understand how the chain works.

3reactions
snakefootcommented, Jan 16, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ILoggingBuilder Logging LogLevel in Appsettings Json ...
I've created a new . net core 2.1 web app and deployed to Azure and Log Stream and and Application Logging to Blob...
Read more >
Logging in .NET Core and ASP.NET Core
If no LogLevel is specified, logging defaults to the Information level. ... In the preceding JSON, the Debug provider's default log level is ......
Read more >
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Read more >
Don't let ASP.NET Core Console Logging Slow your App down
Any logging level settings affect all of these providers by default. Using the configuration settings in appsetting.json and setting to ...
Read more >
Untitled
Development.json LogLevel for System and Microsoft set to "Information" … whirlpool ... As … No effect setting loglevel from appSettings.json #389 - GitHub....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found