【问题标题】:LogManager.ReconfigExistingLoggers() doesn't update loggers archive life timeLogManager.ReconfigExistingLoggers() 不更新记录器归档生命周期
【发布时间】:2021-01-08 15:22:08
【问题描述】:

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()
  • Lo​​gManager.Configuration.Variables["logLifetime"] 更改后返回 1

有什么问题?

【问题讨论】:

  • 每个日志文件都包含一个日期,因此您每天都会获得一个新的文件名。那你在更新什么?

标签: c# xml logging nlog


【解决方案1】:

NLog Config Variables 可以在两种模式下运行:

  • 静态模式 - ${logLifetime}
  • 动态模式 - ${var:logLifetime}

静态模式适用于所有类型的属性,与其类型无关,但它们不会对运行时更改做出反应。

动态模式仅适用于 NLog Layout 类型的属性。 MaxArchiveDays is integer type 并且不会工作。另见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 配置。

【讨论】:

    猜你喜欢
    • 2016-10-05
    • 2016-12-03
    • 2020-06-01
    • 1970-01-01
    • 2010-10-11
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多