Parkyster
Parkyster

Reputation: 155

nlog .netcore turn off microsoft INFO level logging

How can I turn off Microsoft info level logging in nlog / .netcore ?

I still want info logging for my other loggers

I currently have:

  <rules>
    <logger name="Microsoft.*" minlevel="Error" writeTo="nxlog_json" />
    <logger name="Microsoft.*" minlevel="Error" writeTo="human_text_file" />
    <logger name="*" minlevel="Info" writeTo="nxlog_json" />
    <logger name="*" minlevel="Info" writeTo="human_text_file" />
  </rules>

This does not work, If i remove:

<logger name="*" minlevel="Info" writeTo="nxlog_json" />
<logger name="*" minlevel="Info" writeTo="human_text_file" />

It does work for the microsoft logging , but then removes all logging for the other loggers.

What am i missing ? I am assuming the logger name="" overrides the first entrys for Microsoft., but I don't want to have to explicitly specify my other loggers names.

Upvotes: 7

Views: 6746

Answers (2)

DJIM
DJIM

Reputation: 21

To do this programmatically use something along the lines of:

LogManager.Setup().LoadConfiguration(builder =>
{
   // turn off microsoft logging, unless warnings
   c.ForLogger("Microsoft.*").WriteToNil(NLog.LogLevel.Warn);

   var consoleTarget = c.ForTarget("console").WriteTo(new ConsoleTarget()).WithAsync();
   c.ForLogger().FilterMinLevel(NLog.LogLevel.Debug).WriteTo(consoleTarget);
});

See also: https://nlog-project.org/2021/08/25/nlog-5-0-preview1-ready.html#fluent-api-for-nlog-loggingconfiguration

Upvotes: 0

Alexey.Petriashev
Alexey.Petriashev

Reputation: 1734

Use final=true that stops other filters processing if current filter applyed. And write "blackHole" target to swallow messages:

<targets>
  <target xsi:type="Null" name="blackHole" />
</targets>
<rules>
  <logger name="Microsoft.*" minlevel="Info" writeTo="blackHole" final="true" />
  <logger name="*" minlevel="Info" writeTo="nxlog_json" />
  <logger name="*" minlevel="Info" writeTo="human_text_file" />
</rules>

NLog 4.5 allows one to specify an empty writeTo=""

<rules>
  <logger name="Microsoft.*" minlevel="Info" writeTo="" final="true" />
  <logger name="*" minlevel="Info" writeTo="nxlog_json" />
  <logger name="*" minlevel="Info" writeTo="human_text_file" />
</rules>

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

Upvotes: 13

Related Questions