【问题标题】:NLog whenRepeated filter, is it possible to add a condition?NLog当重复过滤器,是否可以添加条件?
【发布时间】:2020-10-08 17:20:34
【问题描述】:

假设一个正常的捕获所有文件记录器:

  <nlog autoReload="true">
    <targets>
      <target name="file" type="File" layout="${longdate} ${logger} ${message} ${exception:format=tostring}" fileName="$Log.log" archiveFileName="LogArchive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="7" concurrentWrites="true" keepFileOpen="false" encoding="utf-8" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="file" />
      </logger>
    </rules>
  </nlog>

当一个外部库重复地向异常发送垃圾邮件并且我不想用它们淹没我的日志文件时,我可以像这样排除它们:

  <rules>
    <logger name="*" minlevel="Info" writeTo="file" >
      <filters>
        <when condition="equals('${logger}','SpammyLogger')" action="Ignore" />
      </filters>
    </logger>
  </rules>

而且由于异常有时会被另一个类记录器捕获并作为内部异常重新抛出,所以我有更好的结果过滤它:

        <when condition="contains('${exception}','SpammyLogger.*')" action="Ignore" />

这很好,但我仍然想知道何时发生异常,我想每 x 秒记录一次,然后过滤其余部分。所以我发现了 whenRepeated 过滤器,它就是这样做的。我可以通过创建与记录器匹配的第二个记录器来设置它:

  <rules>
    <logger name="SpammyLogger.*" minlevel="Info" writeTo="file" final="true">
      <filters>
        <whenRepeated layout="${logger}" timeoutSeconds="30" action="Ignore" FilterCountMessageAppendFormat=" (Hits: {0})" />
        </filters>
      </logger>
    <logger name="*" minlevel="Info" writeTo="file" />
  </rules>

这行得通,但同样它没有过滤重新抛出内部异常,只是过滤原始记录器。我想将它添加为 * 记录器的过滤器,而不是创建第二个。或者至少,能够过滤 {exception} 文本而不是前面示例中的记录器。

所以 TL;DR,因为 whenRepeated 没有像 when 过滤器这样的条件参数,所以我不能以同样的方式使用它。我也不太明白为什么它需要在过滤器参数中使用layout(这是一种格式化输出的方法)?有没有办法像正常的when filter一样使用它来过滤异常的内容?

【问题讨论】:

    标签: nlog


    【解决方案1】:

    听起来像是一个非常特定领域的问题,但也许这会起作用:

          <rules>
            <!-- Handle output from spammy-loggers -->
            <logger name="SpammyLogger*" minLevel="Info" writeTo="file" final="true">
              <filters>
                <!-- Final ignore spammy logging without exceptions (Semi fastfilter) -->
                <when condition="'${exception:format=type}' == ''" action="IgnoreFinal" />
    
                <!-- Final ignore of repeated spammy exceptions (from spammy loggers) -->
                <whenRepeated layout="${exception:format=type}" timeoutSeconds="30" action="IgnoreFinal" FilterCountMessageAppendFormat=" (Hits: {0})" />
              </filters>
            </logger>
    
            <!-- Handle output from other loggers -->
            <logger name="*" minLevel="Info" writeTo="file">
              <filters>
                <!-- Log all good messages without exceptions (Semi fast filter) -->
                <when condition="'${exception:format=type}' == ''" action="Log" />
    
                <!-- Log all good messages with exceptions (Slow filter) -->
                <when condition="not contains('${exception}','SpammyLogger')" action="Log" />
    
                <!-- Final ignore of repeated spammy exceptions (from other loggers) -->
                <whenRepeated layout="${logger}${exception:format=type}" timeoutSeconds="30" action="IgnoreFinal" FilterCountMessageAppendFormat=" (Hits: {0})" />
              </filters>
            </logger>
          </rules>
    

    【讨论】:

    • 谢谢,我喜欢反转条件并首先记录有效内容的想法。在我不想忽略 SpammyLogger 的正常非异常输出的情况下,我可以删除第一个块对吗?另外,我假设您添加了第一个“半快速过滤器”行以提高性能?
    • Yes 过滤 Logger-name 是最快的过滤。使用==-compare 的过滤是半快速的,无需分配。但是用not contains 过滤会做一些分配。
    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 2023-01-13
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多