如何使用配置变量在 NLog FileTarget 上配置 maxArchiveDays

How to configure maxArchiveDays on NLog FileTarget using config variable

我在 NLog.config 中有 int 变量 <variable name="logLifetime2" value="5"/> 但是当我在 maxArchiveDays="${logLifetime2}" 中使用它时,我有错误。 maxArchiveDays 将我的变量视为字符串。

<variable name="logLifetime2"  value="5"/>
<targets>
     <target name="Global" xsi:type="File"
              fileName="Logs/GlobalLog.txt"
              archiveNumbering="Date"
              enableArchiveFileCompression="true"
              archiveFileName="Archive/IPSlog.{#}.zip"
              archiveDateFormat="yyyy-MM-dd"
              maxArchiveDays="${logLifetime2}"
              archiveEvery="Day" />
</targets>

如何在 NLog.Config 中创建 int 变量?

使用此配置时:

<?xml version="1.0" encoding="utf-8" ?>
<nlog throwConfigExceptions="true" internalLogFile="${basedir}/Logs/nlog-internal.txt" internalLogLevel="Debug" >

  <variable name="logLifetime2"  value="5"/>

  <targets>
    <target name="Global" type="File"
       fileName="Logs/GlobalLog.txt"
       archiveNumbering="Date"
       enableArchiveFileCompression="true"
       archiveFileName="Archive/IPSlog.{#}.zip"
       archiveDateFormat="yyyy-MM-dd"
       maxArchiveDays="${logLifetime2}"
       archiveEvery="Day" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="Global" />
  </rules>
</nlog>

然后我的 nlog-internal.txt 包含这个:

2020-11-12 16:01:58.1235 Debug Setting 'NLog.Targets.FileTarget.maxArchiveDays' to '5'

所以它可以在我的机器上运行。确保使用支持 maxArchiveDays 的 NLog 版本 (Introduced with NLog 4.7)

但我想您必须忍受来自 XML-Schema (XSD)-validation 的任何错误,因为 NLog-Config-Variables 非常特殊。