NLog whenRepeated过滤,是否可以加条件?
NLog whenRepeated filter, is it possible to add a condition?
假设一个正常的捕获所有文件记录器:
<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>
并且由于异常有时会被另一个 class 记录器捕获并作为 innerexception 重新抛出,我有更好的结果过滤它像这样:
<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>
这有效,但同样它没有过滤重新抛出的 innerexception,只是原始记录器。我想将它添加为 * 记录器的过滤器,而不是创建第二个记录器。或者至少,能够过滤 {exception} 文本而不是前面示例中的记录器。
所以 TL;DR,因为 whenRepeated
没有像 when
过滤器那样的条件参数,我不能以相同的方式使用它。我也不太明白为什么它需要在过滤器参数中使用 layout
(这是一种格式化输出的方式)?有没有办法像正常的 when filter 一样使用它来过滤异常的内容?
听起来像是一个非常特定领域的问题,但也许这会起作用:
<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>
假设一个正常的捕获所有文件记录器:
<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>
并且由于异常有时会被另一个 class 记录器捕获并作为 innerexception 重新抛出,我有更好的结果过滤它像这样:
<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>
这有效,但同样它没有过滤重新抛出的 innerexception,只是原始记录器。我想将它添加为 * 记录器的过滤器,而不是创建第二个记录器。或者至少,能够过滤 {exception} 文本而不是前面示例中的记录器。
所以 TL;DR,因为 whenRepeated
没有像 when
过滤器那样的条件参数,我不能以相同的方式使用它。我也不太明白为什么它需要在过滤器参数中使用 layout
(这是一种格式化输出的方式)?有没有办法像正常的 when filter 一样使用它来过滤异常的内容?
听起来像是一个非常特定领域的问题,但也许这会起作用:
<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>