Reputation: 260
I'm building an API using .Net Core 2.0.
I'm setting up NLog for logging but there are some "mysteries" that I can't figure out.
I have followed this configuration but I don't understand 2 things:
How Nlog differentiate nlog-all
from nlog-own
. I mean, the rules are the same for both files:
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
nlog-all
automatically? How can I take control over it?appsettings.json
if everything is to be configured in the xml nlog.config
file?Upvotes: 0
Views: 325
Reputation: 19942
The magic happens in the logging-rule that you have nicely excluded from the example:
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
The logging rules are evaluated from top to bottom:
The top "allFile"-rule will match all logger (name="*")
The middle BlackHole-rule will match all Microsoft-loggers and because of final="true" then it will stop further matching.
The last "ownFile" will then receive everything from all loggers not starting with Microsoft.
See also https://github.com/nlog/NLog/wiki/Configuration-file#rules
See also https://github.com/nlog/NLog/wiki/Tutorial
Upvotes: 1