1

I am trying to apply NLog to my ASP.NET core application. I am following the guide from NLog website: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

The below is the rules suggested for nlog.config

<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>

There are two lines have the same logger name patterns

logger name="*" minlevel="Trace"

but for different targets. One is for allfile target, the other is for ownFile-web target

It does not make sense for me. It seems duplicate to me. Any comments? Thanks!

Julian
  • 33,915
  • 22
  • 119
  • 174
LHA
  • 9,398
  • 8
  • 46
  • 85
  • 1
    The example is not production-code, but allows you to see that logging and filtering works. The target `allfile` will contain all log-message from the Microsoft-engine. The target `ownFile-web` will contain errors/warnings from Microsoft-engine (after the BlackHole-filter) and all your own log-messages. – Rolf Kristensen Aug 28 '18 at 16:42

1 Answers1

2

You need to read the rules from top to bottom. There are 3 rules:

  1. if level is trace or up, then log to allfile. So this file will contain all the log messages (including those from externals)
  2. if the level is at max info (so trace, debug and info), and if logger name starts with Microsoft., then skip log messages (as there is no writeTo=) and stop processing (notice the final=true)
  3. if level is trace or up, then log to ownFile-web. But because (trace, debug and info) Microsoft. log messages are skipped (see previous rule), this file will only contain your own logs + warnings and errors from Microsoft - and not the trace, debug and info from Microsoft.
Julian
  • 33,915
  • 22
  • 119
  • 174
  • Thanks. My question is: The first rule allfile already log all messages so why we need the second and third rules? – LHA Aug 28 '18 at 18:35
  • 2
    @Loc To show you how to do filtering. The `ownFile-web` contains the exciting stuff, while `allfile` contains everything (also the non-interesting stuff). – Rolf Kristensen Aug 28 '18 at 19:15
  • 2
    @Loc Normally you would disable the `allfile` in a production system, because logging is expensive and slows down your application. But it is nice to have when debugging. – Rolf Kristensen Aug 28 '18 at 20:33