Sergio
Sergio

Reputation: 260

NLog Core 2.0 logs automatically

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:

  1. 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" />
    
  2. Why NLog logs to nlog-all automatically? How can I take control over it?
  3. Yes I lied, I said 2 things, but what's the point of configuring appsettings.jsonif everything is to be configured in the xml nlog.config file?

Upvotes: 0

Views: 325

Answers (1)

Rolf Kristensen
Rolf Kristensen

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

Related Questions