tags:

views:

587

answers:

4

Hi,

Is there any way to have different layout based on level of the log message when using log4net? Say, if it is a fatal error, I want to see all kind of information possible - class name, method name, line number etc. But for normal, debug and warning, I want to see only the message (I hope, this can increase the performance).

I am using log4net in C# WinForms. My requirement is to log all the previous 512 messages in to a file when a fatal error occures, and I want to see class name, method name, line number etc only for Fatal errors, for all other levels, just a message.

A: 

You could just use a different Appender for each "Level" and have them identical but for the pattern layout.

A: 

Yes, I can use different appenders, but I am using a BufferingForwardingAppender, so that I can buffer and log messages only when a Fatal error occurs. Do you know, how I can configure different appenders for each levels, and still works with BufferingForwardingAppender?

I hope I can configure 4 file appender for each level to log in to same file, and set filter to log only the desired level. But I am not sure whether this approach can save the messages in the same order as they come. Do you have any idea?

Crazy88
There is appender/filter/levelToMatch element, which allows limiting level of messages sent to the appender by log4net.
DK
Thanks DK. My original question was about having different layout for each level in log4net, not about limiting level of messages logged.
Crazy88
+1  A: 

This post has a good explanation of a log4net config file:

log4net Configuration

Pay attention to the layout elements in the appenders. They define the pattern for how the error and any information you choose are output.

slude
A: 

I think you're looking for LevelRangeFilter and a two-appender combination. One appender/filter combo for FATAL level (fatal being the min and max) and one appender/filter combo for everthing else (with ERROR or INFO being the max depending on if you wanted to include errors for debugging purposes)

Example here: http://stackoverflow.com/questions/619532/what-do-you-have-in-your-log4net-config-hacks-optimizations-observations

Brandon Langley