How to turn ON and OFF logging for specific levels in NLog

人盡茶涼 提交于 2019-12-24 00:10:47

问题


I am using NLog in my application. I want to create a switch in order to turn ON and OFF specific logger levels.

  <rules>
    <logger name="*" minlevel="Debug" writeTo="f" />
    <logger levels="Error,Warn,Fatal,Debug,Info" name="CustomLogger" writeTo="database"/>
  </rules>

How can I turn off logging for a specific logger level. I do not want to remove it from the below line.

<logger levels="Error,Warn,Fatal,Debug,Info" name="CustomLogger" writeTo="database"/>

How to turn Info logging On and OFF using something like this:

internalLogLevel="Off"

回答1:


Add a final rule that writes the logs to a "blackhole". Add this as the first rule. Turn it off/on with the enabled attribute.

e.g. To disable the "info" level, add this as first rule of <rules>:

<logger levels="Info" name="*" writeTo="blackHole" final="true" enabled="true" />

and the blackhole target to <targets>

<target name="blackHole" xsi:type="Null" />



回答2:


Maybe a combination of filters and global NLog variables?

https://github.com/NLog/NLog/wiki/Filtering-log-messages

https://github.com/NLog/NLog/wiki/Gdc-layout-renderer



来源:https://stackoverflow.com/questions/46295447/how-to-turn-on-and-off-logging-for-specific-levels-in-nlog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!