Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change log4net conversion pattern or layout at runtime

Tags:

.net

log4net

I'm using the LogInfo() method to log to a rolling flat file, but I need to change the conversion pattern or pattern layout (whatever you want to call it) temporarily when calling it in a certain circumstance. Is this possible?

like image 272
adam0101 Avatar asked Jun 10 '10 15:06

adam0101


People also ask

What is conversionPattern in log4net?

<conversionPattern value=" Log Date is %date{ddd MMM dd yyyy}, Level is %level, Sent from %logger, with the message - %message %newline" />

Where is log4net configuration file?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

Why to use log4net?

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.


1 Answers

Yes it is, for instance like this:

var appenders = log4net.LogManager.GetRepository().GetAppenders();
foreach (var rollingFileAppender in appenders.OfType<log4net.Appender.RollingFileAppender>())
{
     rollingFileAppender.Layout = new log4net.Layout.PatternLayout("- %message%newline");
}
like image 107
Stefan Egli Avatar answered Oct 04 '22 02:10

Stefan Egli