无法减少 NLog 中特定记录器的详细程度

Unable to reduce verbosity of specific loggers in NLog

我几天前在 ASP.Net / .Net 6 环境中从 NLog 4 升级到 NLog v5。一更新,我就注意到我从比过去更多的记录器中获取详细信息(我仍然不明白为什么)。

为了减少闲聊,我开始在 NLog.config 中创建规则以消除噪音。我同时使用了 NLog 文档和这个 SO 问题作为我的指南:NLog disable specific logger - for real.

不幸的是,规则似乎被忽略了。我仍然从 Microsoft.AspNetCore.*Microsoft.EntityFrameworkCore.*

获得大量信息级别的日志记录

我是不是漏掉了一些简单的东西?

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Info"
      internalLogFile="c:\temp\internal-nlog.txt">

  <!-- Ensure all times are logged in UTC -->
  <time type="AccurateUTC" />

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="NLog.Database"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <target name="db"
            xsi:type="Database"
            commandType="StoredProcedure"
            commandText="[dbo].[NLog_AddEntry_p]"
            connectionString="${configsetting:item=ConnectionStrings.DefaultConnection}">
      <parameter name="@errorId" layout="${mdlc:item=errorId}" />
      <parameter name="@ipAddress" layout="${aspnet-request-ip}" />
      <parameter name="@machineName" layout="${machinename}" />
      <parameter name="@siteName" layout="${iis-site-name}" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@username" layout="${aspnet-user-identity}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@properties" layout="${all-event-properties:separator=|}" />
      <parameter name="@serverName" layout="${aspnet-Request-Host}" />
      <parameter name="@serverAddress" layout="${aspnet-request-url}" />
      <parameter name="@callSite" layout="${callsite}" />

      <parameter name="@requestMethod" layout="${aspnet-request-method}" />
      <parameter name="@payload" layout="${aspnet-request-posted-body}" />
      <parameter name="@queryString" layout="${aspnet-request-querystring}" />
      <parameter name="@referrer" layout="${aspnet-request-referrer}" />
      <parameter name="@userAgent" layout="${aspnet-request-useragent}" />
        
      <!--<parameter name="@exception" layout="${exception:tostring}:${stacktrace:format=Enum:topFrames=Integer:skipFrames=Integer:separator=String}" />-->
      <parameter name="@exception" layout="${exception:format=ToString,StackTrace:format=ToString}${newline}" />
    </target>
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
      <!--Disable logging of specific overly talkative loggers-->
      <logger name="Microsoft.AspNetCore.*" minlevel="Warning" final="true" />
      <logger name="Microsoft.EntityFrameworkCore.*" minlevel="Warning" final="true" />
      <!--END Disable logging of specific overly talkative loggers-->

      <logger name="*" minlevel="Info" writeTo="db" />
      <logger name="*" minLevel="Fatal" writeTo="mail" />
  </rules>
</nlog>

您受到 NLog 5.0 中这一重大变化 NLog.Extensions.Logging without any filter 的打击。

推荐的解决方案是更新您的 NLog.config 以执行过滤。可以这样使用 maxLevel="":

  <!-- rules to map from logger name to target -->
  <rules>
      <logger name="Microsoft.Hosting.Lifetime" minLevel="Info" writeTo="db" />
      <!--Disable logging of specific overly talkative loggers-->
      <logger name="Microsoft*" maxlevel="Info" final="true" />
      <!--END Disable logging of specific overly talkative loggers-->

      <logger name="*" minlevel="Info" writeTo="db" />
      <logger name="*" minLevel="Fatal" writeTo="mail" />
  </rules>

NLog 5.0 还引入了对 finalMinLevel="":

的支持
  <!-- rules to map from logger name to target -->
  <rules>
      <!--Disable logging of specific overly talkative loggers-->
      <logger name="Microsoft*" finalMinLevel="Warn" />
      <logger name="Microsoft.Hosting.Lifetime" finalMinLevel="Info" />
      <!--END Disable logging of specific overly talkative loggers-->

      <logger name="*" minlevel="Info" writeTo="db" />
      <logger name="*" minLevel="Fatal" writeTo="mail" />
  </rules>

注意 finalMinLevel="" 类似于 Microsoft 日志记录筛选器配置,其中更容易覆盖 "Microsoft.Hosting.Lifetime" 的 default-filter。