Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net/c# - Different layout based on the level

Tags:

c#

log4net

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.

like image 892
Crazy88 Avatar asked Sep 24 '08 13:09

Crazy88


People also ask

What is log4net used for?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

Can we use log4j in C#?

The easiest and quickest way to get started using log4net is by installing it through the NuGet Package Manager. Assuming that you have created a console application project in Visual Studio, you can install log4net via NuGet Manager, by following these steps.

Is log4net same as log4j?

log4net is a port of the excellent Apache log4j™ framework to the Microsoft® . NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the . NET runtime.


1 Answers

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.

like image 197
Switters Avatar answered Oct 13 '22 00:10

Switters