LogManager.ReconfigExistingLoggers() 不更新记录器存档生命周期
LogManager.ReconfigExistingLoggers() doesn't update loggers archive life time
Nlog.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="DEBUG" internalLogFile="temp\nlog-internal.log">
<variable name="LogLevel" value="DEBUG"/>
<variable name="brief" value="${longdate} | ${level} | ${logger} | ${message}"/>
<variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
<variable name="logLifetime" value="3"/>
<targets >
<target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="S42K" xsi:type="File" layout="${brief}" fileName="Logs/S42K.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/S42K.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="ServerApp" xsi:type="File" archiveDateFormat="yyyy-MM-dd" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" fileName="Logs/ServerApp.txt"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="Color" xsi:type="ColoredConsole" />
<target name="S42KRawData" xsi:type="File" layout="${brief}" fileName="Logs/S42KRawData.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/S42KRawData.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="Advancis" xsi:type="File" layout="${brief}" fileName="Logs/Advancis.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/Advancis.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
</targets>
<rules>
<logger name="Global" minlevel="${LogLevel}" writeTo="Global,Color" />
<logger name="S42K" minlevel="${LogLevel}" writeTo="S42K,Color" />
<logger name="ServerApp" minlevel="${LogLevel}" writeTo="ServerApp,Color" ></logger>
<logger name="S42KRawData" minlevel="TRACE" writeTo="S42KRawData,Color" />
<logger name="Advancis" minlevel="${LogLevel}" writeTo="Advancis,Color" />
</rules>
</nlog>
服务启动时的代码部分
LogManager.Configuration.Variables["logLifetime"] = SetingFromINIfile.Setting[settingIterator++].Parameter;//contains "1"
LogManager.ReconfigExistingLoggers();
但记录器不会重新配置并默认使用 3 天
一些细节:
- 在任务中调用 ReconfigExistingLoggers()
- LogManager.Configuration.Variables["logLifetime"] return 1 改变后
有什么问题?
NLog Config Variables 可以在两种模式下运行:
- 静态模式 -
${logLifetime}
- 动态模式 -
${var:logLifetime}
静态模式适用于所有类型的属性,与其类型无关,但它们不会对运行时更改做出反应。
动态模式仅适用于 NLog Layout 类型的属性。 MaxArchiveDays is integer type and will not work. See also https://github.com/NLog/NLog/wiki/Var-Layout-Renderer
作为解决方法,您可以从代码更新 NLog 配置,并直接在 FileTarget 对象上分配 属性:
var fileTargets = LogManager.Configuration.AllTargets.OfType<FileTarget>();
另一种解决方法是使用 autoReload="true"
并仅更新 NLog.config 文件,NLog 将 automatically reload 配置。
Nlog.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="DEBUG" internalLogFile="temp\nlog-internal.log">
<variable name="LogLevel" value="DEBUG"/>
<variable name="brief" value="${longdate} | ${level} | ${logger} | ${message}"/>
<variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
<variable name="logLifetime" value="3"/>
<targets >
<target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="S42K" xsi:type="File" layout="${brief}" fileName="Logs/S42K.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/S42K.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="ServerApp" xsi:type="File" archiveDateFormat="yyyy-MM-dd" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" fileName="Logs/ServerApp.txt"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="Color" xsi:type="ColoredConsole" />
<target name="S42KRawData" xsi:type="File" layout="${brief}" fileName="Logs/S42KRawData.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/S42KRawData.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
<target name="Advancis" xsi:type="File" layout="${brief}" fileName="Logs/Advancis.txt" archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/Advancis.{#}.zip" archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="${logLifetime}"
archiveEvery="Day" />
</targets>
<rules>
<logger name="Global" minlevel="${LogLevel}" writeTo="Global,Color" />
<logger name="S42K" minlevel="${LogLevel}" writeTo="S42K,Color" />
<logger name="ServerApp" minlevel="${LogLevel}" writeTo="ServerApp,Color" ></logger>
<logger name="S42KRawData" minlevel="TRACE" writeTo="S42KRawData,Color" />
<logger name="Advancis" minlevel="${LogLevel}" writeTo="Advancis,Color" />
</rules>
</nlog>
服务启动时的代码部分
LogManager.Configuration.Variables["logLifetime"] = SetingFromINIfile.Setting[settingIterator++].Parameter;//contains "1"
LogManager.ReconfigExistingLoggers();
但记录器不会重新配置并默认使用 3 天
一些细节:
- 在任务中调用 ReconfigExistingLoggers()
- LogManager.Configuration.Variables["logLifetime"] return 1 改变后
有什么问题?
NLog Config Variables 可以在两种模式下运行:
- 静态模式 -
${logLifetime}
- 动态模式 -
${var:logLifetime}
静态模式适用于所有类型的属性,与其类型无关,但它们不会对运行时更改做出反应。
动态模式仅适用于 NLog Layout 类型的属性。 MaxArchiveDays is integer type and will not work. See also https://github.com/NLog/NLog/wiki/Var-Layout-Renderer
作为解决方法,您可以从代码更新 NLog 配置,并直接在 FileTarget 对象上分配 属性:
var fileTargets = LogManager.Configuration.AllTargets.OfType<FileTarget>();
另一种解决方法是使用 autoReload="true"
并仅更新 NLog.config 文件,NLog 将 automatically reload 配置。