Closed Irinniada closed 2 years ago
Used this example-application:
And updated with this appsettings.json
{
"LogLevel": "Debug",
"NLog": {
"throwConfigExceptions": true,
"autoReload": true,
"internalLogToConsole": true,
"internalLogLevel": "Debug",
"targets": {
"async": true,
"fileTarget": {
"type": "File",
"fileName": "nlog-mainfile.txt",
"layout": "${longdate}|${level:uppercase=true}|${logger}|${message:exceptionSeparator=\r\n:withException=true}"
},
"errorFileTarget": {
"type": "File",
"fileName": "nlog-errorfile.txt",
"layout": "${longdate}|${level:uppercase=true}|${logger}|${message:exceptionSeparator=\r\n:withException=true}"
},
"consoleTarget": {
"type": "Console",
"layout": "${message:exceptionSeparator=\r\n:withException=true}"
}
},
"rules": [
{
"logger": "*",
"minLevel": "${configsetting:item=LogLevel}",
"writeTo": "consoleTarget"
},
{
"logger": "*",
"minLevel": "${configsetting:item=LogLevel}",
"writeTo": "fileTarget"
},
{
"logger": "*",
"minLevel": "Error",
"writeTo": "errorFileTarget"
}
]
}
}
This produce the following NLog-config-ouput:
2022-05-19 08:02:35.7225 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ consoleTarget ]
2022-05-19 08:02:35.7225 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ fileTarget ]
2022-05-19 08:02:35.7225 Debug logNamePattern: (:All) levels: [ Error Fatal ] writeTo: [ errorFileTarget ]
Then changed "LogLevel": "Debug",
to "LogLevel": "Info",
and saved the appsettings.json file while the console-application was running:
2022-05-19 08:03:36.1285 Debug logNamePattern: (:All) levels: [ Info Warn Error Fatal ] writeTo: [ consoleTarget ]
2022-05-19 08:03:36.1285 Debug logNamePattern: (:All) levels: [ Info Warn Error Fatal ] writeTo: [ fileTarget ]
2022-05-19 08:03:36.1285 Debug logNamePattern: (:All) levels: [ Error Fatal ] writeTo: [ errorFileTarget ]
Please attach an example project that reproduces the issue with reload not working when updating appsettings.json-file.
Please attach an example project that reproduces the issue with reload not working when updating appsettings.json-file.
Ok, I analize project again and understood that I changed another .json 🤦 When I change config in Debug folder everything works. Sorry to bother you ^_^
Happy that you found the cause of the issue.
Btw. you don't have to perform an explict config-reload, unless you know the appsettings.json changed. So this:
var logger = LogManager.Setup().ReloadConfiguration().GetCurrentClassLogger();
Can just be this:
var logger = LogManager.Setup().GetCurrentClassLogger();
You can also remove the call LogManager.ReconfigExistingLoggers();
since it is only needed after having modified the LoggingRules of the existing configuration.
I'm trying to control LogLevel in log from appsetting.json. I've added my Nlog config here and custom field LogLevel. In rules I've added
"${configsetting:item=LogLevel}"
and it works. But I want to be able to change LogLevel when app is running. So I addautoReload
to json andreloadOnChange = true
to ConfigurationBuilder. But it's not working for NLog.Some of
appsetting.json
:And my code is
The only way it works for me:
but it means to write it under every log.
Is there any nice way to reload NLog with appsetting?