Reputation: 155
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
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);
});
Upvotes: 0
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