Getting values from Log4Net configuration

别等时光非礼了梦想. 提交于 2019-12-23 08:03:54

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5081242/getting-values-from-log4net-configuration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!