views:

257

answers:

1

Hi

I'm looking for a way to collect a set of data, that will only be used for debugging, the data should only be logged if I log an exception. AFAIK it can happend when ILog.Error, Fatal or Debug with an exception argument when logging other data with an exception, the data shouldn't be logged.

I plan to use the GlobalContext or ThreadContext for building the dataset.

My idea was to hook into Log4Net and attach to an event I would imagine, to alter the message pattern to include contexts. But I can't find any event that would help me, perhaps there also might be a easier way?

What do you think about the overall design of this, am I on the right track or am I missing something?

If this way is good, how can I implement it?

+1  A: 

Hooking in and changing the pattern doesn't feel quite right. I would suggest looking at the filter stuff. So you would set up your "ForMessagesWithExceptionsOnly" appender which uses the MessagesWithExceptionOnly filter. This appender would ofcourse only process messages that contains an exception.

To implement your MessagesWithExceptionOnly filter, have a look at FilterSkeleton.

Peter Lillevold
I don't quite get it. Do you want to create two appenders with different layout patterns, one to print the context property and one to just print the message. Create two filers, one that accepts only loggingevents with exceptions and one that denies. And then add a filter to the appropriate appender?That seems a bit cumbersome.
Karsten
Exactly. Except that you don't have to create two filter classes, you can reuse the same filter on both appenders and only have inverted AcceptOnMatch settings on the two. A bit more config perhaps, but imo a much cleaner approach. I also suspect modifying patterns at runtime would have a perf impact.
Peter Lillevold