42

In my NLog configuration, I have a catch-all logger but a specific logger I have created is very spammy and I want its output to go to its own file. That part is easy, but the catch-all logger receives the spammy log messages as well. How do I tell the main logger to log everything but to exclude the spammy logger?

I'm using NLog 2.0.

Nathan Ridley
  • 32,688
  • 32
  • 118
  • 193

1 Answers1

94

I think something like this is what you want:

<logger name="SpammyLogger" minlevel="Off" maxlevel="Trace" final="true" />  
<logger name="SpammyLogger" minlevel="Debug" maxlevel="Fatal" writeTo="SpammyFileTarget" final="true" />  
<logger name="*" levels="Trace" writeTo="RegularFileTarget/" />  

Adding final="true" means that no more rules will be executed for the events produced by "SpammyLogger", but it applies only to the specified levels.(see https://github.com/nlog/nlog/wiki/Configuration-file#rules)

See this link for more NLog info that you might find helpful:

Most useful NLog configurations

Julian
  • 30,223
  • 19
  • 105
  • 147
wageoghe
  • 26,942
  • 13
  • 84
  • 115
  • 11
    The order you defined the logger is important. If you were to define "*" before "SpammyLogger", the final attribute would be useless. Took sometime to figure that one out. – guiomie Aug 05 '15 at 14:31
  • This all works fine as long as there is only one logger you want to exclude. But as soon as there are more, it is impossible to model this with logging rules. – bitbonk Aug 12 '15 at 14:46
  • 2
    I think this is broken, as @bitbonk mentioned, if you have more than one. I am trying to use specific logger rules for 10 different loggers, but the catch-all always catches the trace logs from everything. – dodexahedron Jan 20 '16 at 22:06
  • 1
    This has changed for NLog 4, you now must specify both min and max levels in your final rule. See http://nlog-project.org/2015/06/09/nlog-4-has-been-released.html – Florian Doyon May 17 '16 at 10:49
  • 8
    `minlevel="Off" maxlevel="Trace" final="true"` does nothing, at least not in NLog 4.3.10, because `Off` is actually the _highest_ log level so there are no levels between `Off` and `Trace`. See https://github.com/NLog/NLog/blob/v4.3.10/src/NLog/LogLevel.cs#L86 . – mdonoughe Oct 26 '16 at 13:18