从Log4Net配置获取值 [英] Getting values from Log4Net configuration
问题描述
我已经通过扩展AppenderSkeleton类实现了自定义log4net附加程序.它就像任何人都可以要求的那样简单,并且运行良好.
I have implement a custom log4net appender by extending the AppenderSkeleton-class. It was as simple as anyone could ask for and works perfectly.
我的问题是我必须对一些值进行硬编码,我想将它们从我的代码中删除到附加程序的配置中.由于log4net知道它是如何配置的,因此我认为应该有一种方法可以询问log4net的配置.
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>
如何获取MyProperty1-3的值,以便可以在Appender中使用它?
How to get the value of MyProperty1-3 so I can use it inside my Appender?
先谢谢了 罗恩(Roalnd)
Thanks in advance Roalnd
推荐答案
这取决于类型,但是对于简单类型,您可以执行以下操作:
It depends a bit on the type but for simple types you can do the following:
定义这样的属性:
// 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; }
}
您可以进行以下配置:
<appender name="MyLogAppender" type="xxx.yyy.zzz.MyLogAppender">
<flushInterval value="02:45:10" />
</appender>
这当然适用于字符串,布尔值,整数和时间跨度.
This certainly works for string, bool, int and TimeSpan.
注意:如果您的设置需要激活某些逻辑(例如创建计时器),则可以在ActivateOptions
方法中实现.
Note: If your settings requires some logic to be activated (e.g. create a timer) then you can implement this in the ActivateOptions
method.
这篇关于从Log4Net配置获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!