如何配置“何时”条件NLog规则?

时间:2018-08-17 23:27:58

标签: nlog

我正在尝试记录条件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>

1 个答案:

答案 0 :(得分:0)

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

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

现在您需要:

  1. 逆转条件,action=ignore
  2. 或者,添加一个忽略所有内容的第二个过滤器
相关问题