NLog Core 2.0 自动记录

NLog Core 2.0 logs automatically

我正在使用 .Net Core 2.0 构建 API。

我正在设置 NLog 进行日志记录,但有些 "mysteries" 我无法弄清楚。

我遵循了 this 配置,但我不明白两件事:

  1. Nlog 如何区分 nlog-allnlog-own。我的意思是,两个文件的规则相同:

    <logger name="*" minlevel="Trace" writeTo="allfile" />
    
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    
  2. 为什么 NLog 会自动登录到 nlog-all?我怎样才能控制它?
  3. 是的,我撒谎了,我说了 2 件事,但是如果要在 xml nlog.config 文件中配置所有内容,那么配置 appsettings.json 有什么意义?

奇迹发生在您从示例中很好地排除的日志记录规则中:

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

记录规则从上到下评估:

  • 最上面的 "allFile" 规则将匹配所有记录器 (name="*")

  • 中间的 BlackHole-rule 将匹配所有 Microsoft-loggers 并且由于 final="true" 然后它将停止进一步匹配。

  • 最后一个 "ownFile" 将从所有非 Microsoft 开头的记录器接收所有内容。

另见 https://github.com/nlog/NLog/wiki/Configuration-file#rules

另见 https://github.com/nlog/NLog/wiki/Tutorial