3

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.

benpage
  • 4,418
  • 3
  • 41
  • 39

1 Answers1

2

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>
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Sadly this doesn't work. Also, even when I have the catchall (*) set to 'Warn' I still get info level messages. Also, doesn't 'final' mean "don't process any more rules"? so why would it fall through to the next level? – benpage Mar 01 '18 at 05:55
  • @benpage What is the logger-name (with namespace) for those info-level-messages ? Maybe update the layout to `layout="${logger}|${level}|${message}${exception:format=tostring}"` – Rolf Kristensen Mar 13 '18 at 18:44