开发者

Getting values from Log4Net configuration

开发者 https://www.devze.com 2023-02-13 05:34 出处:网络
I have implement a custom log4net appender by extending the AppenderSkeleton-class. It was as simple as anyone could ask for and works perfectly.

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</My开发者_高级运维Property3>
</appender>

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

Thanks in advance Roalnd


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消