Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c# - 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
log-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>

code part when service started

 LogManager.Configuration.Variables["logLifetime"] = SetingFromINIfile.Setting[settingIterator++].Parameter;//contains "1"
LogManager.ReconfigExistingLoggers();

but loggers doesn't reconfig and use 3 days by default

Some details:

  • ReconfigExistingLoggers() invoked in a task
  • LogManager.Configuration.Variables["logLifetime"] return 1 after changing

archives()

what is the problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

NLog Config Variables can operate in two modes:

  • Static mode - ${logLifetime}
  • Dynamic mode - ${var:logLifetime}

Static mode works for all types of properties independent of their type, but they will not react to runtime changes.

Dynamic mode only works for properties of the type NLog Layout. MaxArchiveDays is integer type and will not work. See also https://github.com/NLog/NLog/wiki/Var-Layout-Renderer

As a work around you can update the NLog Configuration from code, and assign the property directly on the FileTarget-objects:

var fileTargets = LogManager.Configuration.AllTargets.OfType<FileTarget>();

Another work-around would be to use autoReload="true" and just update the NLog.config file and NLog will automatically reload the configuration.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...