【问题标题】:NLog Core 2.0 logs automaticallyNLog Core 2.0 自动记录
【发布时间】:2018-10-12 20:59:52
【问题描述】:

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

我正在设置 NLog 进行日志记录,但有一些我无法弄清楚的“谜团”。

我已经按照this的配置,但我不明白2件事:

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

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

【问题讨论】:

    标签: logging .net-core nlog


    【解决方案1】:

    神奇之处发生在您已从示例中很好地排除的日志规则中:

      <!-- 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 规则将匹配所有 Microsoft 记录器,并且由于 final="true",它将停止进一步匹配。

    • 最后一个“ownFile”将接收来自所有记录器的所有内容,而不是从 Microsoft 开始。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多