Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting values from Log4Net configuration

I have implement a custom log4net appender by extending the AppenderSkeleton-class. It was as simple as anyone could ask for and works perfectly.

My problem is that I had to hardcode a few values and I'd like to remove them from my code to the configuration of the appender. Since log4net knows how it is configured I think there should be a way to ask log4net for it's configuraion.

My appender could look something like this:

<appender name="MyLogAppender" type="xxx.yyy.zzz.MyLogAppender">
      <MyProperty1>property</MyProperty1>
      <MyProperty2>property</MyProperty2>
      <MyProperty3>property</MyProperty3>
</appender>

How to get the value of MyProperty1-3 so I can use it inside my Appender?

Thanks in advance Roalnd

like image 614
Roland Avatar asked Feb 22 '11 16:02

Roland


People also ask

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.

What is RollingFileAppender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

Does log4net create directory?

When using a file appender, the destination folder does not have to exist. Log4net creates the folder. Using an administrator account, connect to the Coveo Master server.


1 Answers

It depends a bit on the type but for simple types you can do the following:

Define a property like this:

// the value you assign to the field will be the default value for the property
private TimeSpan flushInterval = new TimeSpan(0, 5, 0);

public TimeSpan FlushInterval
{
     get { return this.flushInterval; }
     set { this.flushInterval = value; }
}

This you can configure as follows:

<appender name="MyLogAppender" type="xxx.yyy.zzz.MyLogAppender">
    <flushInterval value="02:45:10" />
</appender>

This certainly works for string, bool, int and TimeSpan.

Note: If your settings requires some logic to be activated (e.g. create a timer) then you can implement this in the ActivateOptions method.

like image 185
Stefan Egli Avatar answered Sep 23 '22 16:09

Stefan Egli