Home > other >  Unable to reduce verbosity of specific loggers in NLog
Unable to reduce verbosity of specific loggers in NLog

Time:05-20

I upgraded from NLog 4. to NLog v5 a couple of days ago in an ASP.Net / .Net 6 environment. As soon as I updated I noticed that I was getting details from many more loggers than I had in the past (I still haven't figure out why).

To reduce the chattiness, I began creating rules in my NLog.config to remove the noise. I used both the NLog documentation as well as this SO question as my guide: NLog disable specific logger - for real.

Unfortunately, the rules seem to be ignored. I'm still getting lots and lots of Info level logging from Microsoft.AspNetCore.* and Microsoft.EntityFrameworkCore.*

Am I missing something simple here?

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

CodePudding user response:

You are hit by this breaking change NLog.Extensions.Logging without any filter in NLog 5.0.

The recommend solution is to update your NLog.config to perform the filtering. It can be done like this using 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 also introduces support for 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>

Notice finalMinLevel="" is similar to Microsoft Logging Filter Configuration, where it is easier to override the default-filter like for "Microsoft.Hosting.Lifetime".

  • Related