【问题标题】:How do configure "when" conditional NLog rules?如何配置“何时”条件 NLog 规则?
【发布时间】:2018-08-20 16:41:51
【问题描述】:

我正在尝试记录条件 NLog。我总是传递一个字符串(在这个例子中是 Rule1 或 Rule2),它指定什么规则作为消息的第一部分。

我的问题是,使用下面的当前规则,两个规则都被标记为“true”和日志文件。我看不出这怎么可能。我在这个例子中使用了“Rule1”和“Rule2”这两个字符串,但实际传递的字符串完全不同(不是同一个单词不同的数字)。

传递以记录的示例消息是...

Ex1:${message} = "Rule1,Information about the rule,foo

Ex2:${message} = "Rule2,Information about the other rule,bar

这是我的示例目标

<!-- language: config -->
<?xml version="1.0" encoding="utf-8" ?>
<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="Off" internalLogFile="c:\temp\nlog-internal.log"
  keepVariablesOnReload="true">

  <variable name="dir_Logging" value="C:/<myPath>"/>
  <variable name="dir_Archive_Rule1" value="${dir_Logging}/Log Archives/Rule1/${shortdate}"/>
  <variable name="dir_Archive_Rule2" value="${dir_Logging}/Log Archives/Rule2/${shortdate}"/>
  <variable name="varRule1" value="Rule1"/>
  <variable name="varRule2" value="Rule2"/>

  <targets>
    <target xsi:type="File" name="rule1Logs"
      archiveOldFileOnStartup="true"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd_HH-mm-ss"
      fileName="${dir_Logging}/Logs - Rule1.csv"
      archiveFileName="${dir_Archive_Rule1}/Logs - Rule1.{#}.csv">
        <layout xsi:type="SimpleLayout"
          text="&quot;${date:format= yyyy/MM/dd}&quot;,&quot;${date:format= HH\:mm\:ss.fff}&quot;,${message}"/>
    </target>
    <target xsi:type="File" name="rule2Logs"
      archiveOldFileOnStartup="true"
      archiveNumbering="Date"
      archiveDateFormat="yyyy-MM-dd_HH-mm-ss"
      fileName="${dir_Logging}/Logs - Rule2.csv"
      archiveFileName="${dir_Archive_Rule2}/Logs - Rule2.{#}.csv">
        <layout xsi:type="SimpleLayout"
          text="&quot;${date:format= yyyy/MM/dd}&quot;,&quot;${date:format= HH\:mm\:ss.fff}&quot;,${message}"/>
    </target>
  </targets>

  <rules>
    <logger name="*" level="Info" writeTo="rule1Logs">
      <filters>
        <when condition="starts-with('${message}','${varRule1}')" action="Log"/>
      </filters>
    </logger>
    <logger name="*" level="Info" writeTo="rule2logs">
      <filters>
        <when condition="starts-with('${message}','${varRule2}')" action="Log"/>
      </filters>
    </logger>
  </rules>

【问题讨论】:

    标签: nlog


    【解决方案1】:

    过滤器的默认值为“中性”,因此如果它与您的过滤器不匹配,则会被记录。

    这是一个known limitation,将在 NLog 4.6 (See code change in NLog 4.6) 中解决

    现在你需要:

    1. 反转条件和action=ignore
    2. 或者,添加一个忽略所有的第二个过滤器

    【讨论】:

    • 我搞定了。我没有意识到我在消息中传递了引号。添加 "工作前后。
    猜你喜欢
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多