9

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.

Crazy88
  • 163
  • 3
  • 4
  • Answer you are looked for is [here](https://stackoverflow.com/questions/31821137/log4net-conversionpattern-per-level-for-single-fileappender/33241442) – JayJay May 23 '18 at 13:43
  • This post has a good explanation of a log4net config file: [log4net Configuration](http://weblogs.asp.net/tgraham/archive/2007/03/15/a-realistic-log4net-config.aspx) Pay attention to the layout elements in the appenders. They define the pattern for how the error and any information you choose are output. – Switters Sep 24 '08 at 14:13

2 Answers2

2

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: What do you have in your log4net config? Hacks, optimizations, observations?

Community
  • 1
  • 1
Brandon Langley
  • 553
  • 3
  • 9
1

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

lotsoffreetime
  • 1,100
  • 5
  • 11