No effect setting loglevel from appSettings.json
See original GitHub issueHi, 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
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.
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:
ILoggingBuilder.AddConfiguration
can be found at Nuget: Microsoft.Extensions.Logging.Configuration