NLog - 使用过滤器忽略某些消息

NLog - ignore certain messages using filters

我想让 NLog 使用过滤器忽略某些消息。

来自我的日志文件 -

2020-07-15 13:18:53.3291 ERROR MyAppName:MyAppName Some other error    
2020-07-15 13:18:53.3291 ERROR MyAppName:MyAppName.BusinessLogic.Notification.ProcessorControl Unknown : Unable to connect for notification control. 
2020-07-15 13:18:53.3291 ERROR MyAppName:MyAppName.BusinessLogic.Notification.ProcessorControl Exception Occurred in Unknown : AggregateException One or more errors occurred.
2020-07-15 13:18:53.3291 ERROR MyAppName:MyAppName Some other error 

来自 Nlog.config 文件 -

<target xsi:type="File"
        name="file"
        fileName="${var:basedir}/Logs/MyAppName-${shortdate}.log"
        layout="${longdate} [${event-context:item=CC} ${event-context:item=Workstation} ${event-context:item=User} ${event-context:item=IL}] ${level:uppercase=true} ${logger} ${message} ${onexception: ${exception:format=shortType,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}" >
</target>
    ...

<logger name="MyAppName:*" minlevel="Error" writeTo="file" >
      <filters>
        <when condition="contains('${message}','MyAppName.BusinessLogic.Notification')" action="Ignore" />
        <!--<when condition="contains('${message}','*MyAppName.BusinessLogic.Notification*')" action="Ignore" />-->
     </filters>
</logger>

我添加了过滤器以忽略任何可能包含该字符串的消息
MyAppName.BusinessLogic.Notification”但它不起作用。
我也尝试在注释行中使用 *MyAppName.BusinessLogic.Notification* - 但我的日志文件中仍然充满了这些消息。
谁能指导我如何解决这个问题?

还有一个问题- $message 不代表完整的错误消息,就像日志文件中的这样 -

MyAppName:MyAppName.BusinessLogic.Notification.ProcessorControl Unknown : Unable to connect for notification control. 

谢谢!

您正在编写规则,因此您必须将其包裹在 <rules></rules> 标记周围。并使用 name="*" 代替 name="MyAppName:*".

试试下面的代码;

<rules>
    <logger name="*" minlevel="Error" writeTo="file" >
          <filters>
            <when condition="contains('${message}','MyAppName.BusinessLogic.Notification')" action="Ignore" />
            <!--<when condition="contains('${message}','*MyAppName.BusinessLogic.Notification*')" action="Ignore" />-->
         </filters>
    </logger>
</rules>

"MyAppName.BusinessLogic.Notification" 可能不在您的 ${message} 中,但它是您的记录器名称 (${logger})

${message}

写日志事件时,这里有一些例子${message}

  • logger.Info("My message"); - ${message} 产生 "My message"
  • logger.Info("Order {0}", 123); - ${message} 产生 "Order 123"
  • logger.Info("Order {OrderId}", 123); - ${message} 产生 "Order 123"。结构化风格,阅读更多here。在这种情况下,您还有 ${event-properties:OrderId} 产生 123.
  • 注意:使用 ASP.NET Core integration 时,请阅读 logger.LogInformation 而不是 logger.Info

${logger}

记录器名称,示例:

  • LogManager.GetLogger("logger1"); - ${logger} 产生 "logger1"
  • LogManager.GetCurrentClassLogger(); - ${logger} 产生类似 "MyClassNamespace.MyClass"
  • 的结果
  • 注入 ILogger<MyClass> - ${logger} 产生类似 "MyClassNamespace.MyClass" 的结果 - 如果你使用 ASP.NET 核心集成。

过滤

根据记录器名称进行过滤很容易,它内置于 <logger> 规则中。 name 属性应理解为“名称过滤器”- 支持 *? 通配符。

示例:

<rules>
    <logger name="MyAppName.BusinessLogic.*" minlevel="Error" writeTo="ErrorWithBusinessLogicStuffTarget" />

阅读更多here

备注

  • 过滤时,final 属性会很方便。对于所有选项,请参阅 here
  • 您也可以使用 <when> 来过滤 ${logger},但这更难维护且性能较低。