tags:

views:

140

answers:

2

Given two appenders: A1, A2 and multiple loggers one of which is L1

Is it possible to configure log4net such that:

A1 gets DEBUG and above from all loggers except L1, for which it gets INFO and above
A2 gets DEBUG and above from all loggers

I have looked at appender threshold, filters, and every other configuration combination I can think of but none of them seem to accomplish the above.

A: 

According to the docs:

Filters form a chain that the event has to pass through. Any filter along the way can accept the event and stop processing, deny the event and stop processing, or allow the event on to the next filter. If the event gets to the end of the filter chain without being denied it is implicitly accepted and will be logged.

So it seems that something like this would work (untested pseudo-code):

A1
    Level >= INFO:  Accept
    Logger == L1: Deny
    Level >= DEBUG: Accept
    DenyAll

A2
    Level >= DEBUG: Accept
David Lynch
I tried this, but the DenyAll filter at the end of A1 prevented anything from being logged. I removed the DenyAll line and A1 got DEBUG for all other loggers but nothing at all from L1. I think the deny on logger L1 falls under the "deny the event and stop processing" statement of the docs. Having said that, even though it did not solve this problem I do appreciate your suggestion.
markee
A: 

You could do the following:

  1. Define your two appenders (A1, A2) without any filter
  2. Create a BufferingForwardingAppender, that forwards to A1 and filters on level >= INFO
  3. Create a BufferingForwardingAppender, that forwards to A2 (no filter or >= DEBUG)
  4. Configure the root logger to use appenders A1, A2
  5. Configure the L1 logger to not inherit appenders (additivity = false); reference the two BufferingForwardingAppender instead

I did not test it, but I do not see why this would not work.

Stefan Egli
Thanks Stefan, this solution did achieve the desired result. I only needed one extra BufferingForwardingAppender since logger L1 can reference A2 directly.
markee
yes of course... silly that I did not see that
Stefan Egli