19

Very often I want to exclude logging from a specific chatty logger up to a certain level. Unfortunately, the first of the following rules is final for all levels of that logger, so that the second rule (which is simply my default rule) will not log anything from it:

<logger name="ChattyLogger" maxlevel="Warn" writeTo="blackhole" final="true" />

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

One possibility is to use filters instead:

<logger name="ChattyLogger" writeTo="blackhole">
  <filters>
    <when condition="level&lt;=LogLevel.Warn" action="IgnoreFinal" />
  </filters>
</logger>

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

But the syntax is ugly, especially the length and the need to escape the condition expression.

Since this seems like such a common requirement, I'm wondering if I overlooked something.

I'm using nlog under Silverlight, but I presume that shouldn't matter.

John
  • 6,693
  • 3
  • 51
  • 90

1 Answers1

15

Try this:

<logger name="ChattyLogger" maxlevel="Warn"/>
<logger name="ChattyLogger" minlevel="Error" final="true" writeTo="default"/>
<logger name="*" minlevel="Info" writeTo="default" />
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • Great answer, but surely the first line does nothing, and can be removed? – Nigel Touch Oct 14 '15 at 18:05
  • @NigelTouch It is needed, because nlog matches a logger by name and level, at least that's what I've noticed. I'm sure you already figured this out, I'm just putting it here for other people. A lost a little time with it, but the version that finally worked for me was: ` ` .Without the `final`on the first rule it defaulted to the global rule "*". – veili_13 Dec 11 '20 at 00:12
  • 1
    I spent a half day because I used "Warning" instead of "Warn". Thank you. – ddagsan Aug 14 '23 at 13:02