NLog config rule not taking affect

1k Views Asked by At

I have the following nlog.config file for my project. When I debug locally, it works as expected, that Hangfire messages are being filtered to only show Warn and above. However on our staging server (IIS 8.5) nlog seems to ignore the rule and just logs everything (including Info) to elmah:

  <?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">
  <extensions>
    <add assembly="NLog.Elmah"/>
  </extensions>
  <targets>
    <target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
  </targets>
  <rules>
    <logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
    <logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
  </rules>
</nlog>

Even if I remove the Hangfire.* rule and change the catchall to minlevel="Warn" it still logs Info items.

1

There are 1 best solutions below

2
Rolf Kristensen On

Think you are running two different versions of NLog.

This will capture all log-events with warning (and above levels):

<logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />

This means all log-events with info or below will try the next rule:

<logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />

Try this configuration instead:

<?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">
  <extensions>
    <add assembly="NLog.Elmah"/>
  </extensions>
  <targets>
    <target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
  </targets>
  <rules>
    <logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
    <logger name="Hangfire.*" maxLevel="Warn" final="true" /> <!-- BlackHole -->
    <logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
  </rules>
</nlog>