Reputation: 23
I am trying to access appsettings.json properties into NLog.Config file. I am writing rules in nlog and accessing loglevels from appsettings using configsetting layout renderer. It is working fine with normal properties (Microsoft), using below code
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft}" writeTo="logfile,Loggly" />
With properties that have dot in name (Microsoft.Hosting.Lifetime) I am not able to read the value
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft.Hosting.Lifetime}" writeTo="logfile,Loggly" />
Json sample below:
"Logging": {
"LogLevel": {
"Default": "Error",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Error"
}
I tried below but nothing worked so far
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel'.Microsoft.Hosting.Lifetime'}" writeTo="logfile,Loggly" />
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.'Microsoft.Hosting.Lifetime'}" writeTo="logfile,Loggly" />
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft\.Hosting\.Lifetime}" writeTo="logfile,Loggly" />
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft/.Hosting/.Lifetime}" writeTo="logfile,Loggly" />
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel'.[Microsoft.Hosting.Lifetime]'}" writeTo="logfile,Loggly" />
EDIT 1: Tried Rolf Kristensen's solution and it is partly working with one minor issue. Logs are working as per loglevel set with just one minor issue.
My nlog.config file settings
<logger name="Microsoft.Hosting.Lifetime*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft\\.Hosting\\.Lifetime}" writeTo="logfile,Loggly" />
<logger name="Microsoft.Hosting.Lifetime*" maxlevel="${configsetting:item=Logging.LogLevel.Microsoft\\.Hosting\\.Lifetime}" writeTo="void" final="true" />
<logger name="Microsoft*" minlevel="${configsetting:item=Logging.LogLevel.Microsoft}" writeTo="logfile,Loggly" />
<logger name="Microsoft*" maxlevel="${configsetting:item=Logging.LogLevel.Microsoft}" writeTo="void" final="true"/>
<logger name="*" minlevel="${configsetting:item=Logging.LogLevel.Default}" writeTo="logfile,Loggly" />
appsettings.json set
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Error",
"Microsoft.Hosting.Lifetime": "Information"
}
Expectation is that for Microsoft.Hosting.Lifetime
rule it should push logs till and including Information level to void. But its pushing till Error level to void. I think it is picking the loglevel set for Microsoft
.
Logs taken from nlog internal logs:
2022-08-25 13:09:30.7512 Warn Logger: Microsoft.Hosting.Lifetime configured with duplicate output to target: FileTarget(Name=logfile). LoggingRule with NamePattern='Microsoft*' and Level=Error has been skipped.
2022-08-25 13:09:30.7512 Warn Logger: Microsoft.Hosting.Lifetime configured with duplicate output to target: FileTarget(Name=logfile). LoggingRule with NamePattern='*' and Level=Fatal has been skipped.
2022-08-25 13:09:30.7512 Debug Targets configured when LogLevel >= Trace for Logger: Microsoft.Hosting.Lifetime
2022-08-25 13:09:30.7512 Debug Logger Microsoft.Hosting.Lifetime [Trace] => void
2022-08-25 13:09:30.7678 Debug Logger Microsoft.Hosting.Lifetime [Debug] => void
2022-08-25 13:09:30.7678 Debug Logger Microsoft.Hosting.Lifetime [Info] => logfile Loggly void
2022-08-25 13:09:30.7678 Debug Logger Microsoft.Hosting.Lifetime [Warn] => logfile Loggly void
2022-08-25 13:09:30.7678 Debug Logger Microsoft.Hosting.Lifetime [Error] => logfile Loggly void
2022-08-25 13:09:30.7678 Debug Logger Microsoft.Hosting.Lifetime [Fatal] => logfile Loggly
Upvotes: 0
Views: 535
Reputation: 19877
NLog.Extensions.Logging v5.0.2 have been released. Where you can do this:
${configsetting:item=Logging.LogLevel.Microsoft\\.Hosting\\.Lifetime}
Or you can do this:
${configsetting:item=Logging.LogLevel.Microsoft..Hosting..Lifetime}
See also: https://www.nuget.org/packages/NLog.Extensions.Logging/5.0.2
Upvotes: 0