loggingnlogerror-logging

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


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"

Solution

  • 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" />