【问题标题】:How to configure maxArchiveDays on NLog FileTarget using config variable如何使用配置变量在 NLog FileTarget 上配置 maxArchiveDays
【发布时间】:2020-11-12 10:20:38
【问题描述】:

我在 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变量?

【问题讨论】:

  • 你能显示你的 NLog.config 的全部内容吗?你用的是什么版本的 NLog?在 NLog.config 中使用 NLog-config-variables 时,它们会充当逐字复制并且可以正常工作。
  • &lt;variable name="logLifetime2" value="5"/&gt; &lt;targets&gt; &lt;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" /&gt; 我使用最后一个 nlog 块包。我无法发送所有 Nlog.config,因为有字符限制
  • 当其他人在您的问题中发布有关缺少详细信息的 cmets 时。那么最好只编辑您的问题,并在问题中包含缺少的详细信息。

标签: xml nlog


【解决方案1】:

使用此配置时:

<?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 非常特殊。

【讨论】:

  • 我对 Nlog.config 的智能感知有问题。这个例子真的有效
  • @ИванОнищенко 啊,所以你的问题是 XML-schema-validation。我的示例不包括 XSD 验证,所以这就是没有错误的原因。请记住在更新 NLog 时更新您的 NLog.xsd。另见:nuget.org/packages/NLog.Schema
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多