问题
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