Duplicate log entries caused by duplicate rules

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Duplicate log entries caused by duplicate rules

gregmac
I'm using nlog 2.0 beta 1, and I have a file with the following rules:

  <logger name="*" minlevel="Info" writeTo="file"/>
  <logger name="SubSystem.*" minlevel="Trace" writeTo="file"/>

This causes any messages of info or higher by anything prefixed SubSystem to be logged twice. I understand why this would happen, but this is clearly not my intent here -- I just want to see Trace messages for one subsystem, and Info or up for anything else.

Obviously, a fix could be doing this:

  <logger name="OtherSystem1.*" minlevel="Info" writeTo="file"/>
  <logger name="OtherSystem2.*" minlevel="Info" writeTo="file"/>
  <logger name="OtherSystem3.*" minlevel="Info" writeTo="file"/>
  <logger name="SubSystem.*" minlevel="Trace" writeTo="file"/>

but clearly this is not scalable.

I can't find anyone that's discussed this before, and nothing in the documentation that states how it works either way. Can someone point me in the right direction if this conversation/documentation exists?

Otherwise, I'd propose a change to either:
 * Modify nlog so it doesn't do these duplicates -- basically, once a given message has been written to a given target, don't ever write to the same target again.
 * Add a DuplicateFilter, which would cache the previous message(s) (not sure if 1 is enough or if it would need multiples, in the case of a multi-threaded app), and then basically if the previous entry is identical (same message/timestamp/etc) to the current one, don't log it.

I kind of thing the first option is the way to go. I can't imagine why this behaviour would ever be desirable -- and even if it is, all you'd have to do is create a copy of your target with a different name, and you could log it twice.
Reply | Threaded
Open this post in threaded view
|

Re: Duplicate log entries caused by duplicate rules

gregmac
I just realized I could do:

  <logger name="*" minlevel="Info" writeTo="file"/>
  <logger name="SubSystem.*" levels="Trace,Debug" writeTo="file"/>

But I still think this is a bit too much onus on the end user, when the logger could easily sort this out.
Reply | Threaded
Open this post in threaded view
|

Re: Duplicate log entries caused by duplicate rules

Jarek Kowalski
Administrator
Try final="true":

  <logger name="OtherSystem1.*" minlevel="Info" writeTo="file" final="true"/>
  <logger name="OtherSystem2.*" minlevel="Info" writeTo="file" final="true"/>
  <logger name="OtherSystem3.*" minlevel="Info" writeTo="file" final="true"/>
  <logger name="SubSystem.*" minlevel="Trace" writeTo="file" final="true"/>


Final means - stop processing further rules if this one matches.

Jarek
NLog Blog