我可以将参数传递给自定义 log4net Appender 的构造函数吗?
我想将参数传递给自定义附加程序的构造函数,因此我想我必须重写附加程序的初始化机制。问题是我在文档中找不到连接它的方法,这让我认为这是不可能的(或者文档不完整)。
至于1.2.10版本,如果不修改源代码,这是不可能的。相关部分位于 Repository\Hierarchy\XmlHierarchyConfigurator.cs:L286
中:
IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));
如您所见,它应该使用此重载(或类似的方式)来允许我实现我的需求。
Activator.CreateInstance(Type, Object[])
I'd like to pass arguments to a custom appender's constructor so I guess I have to override Appenders' initialization mechanism. Problem is that I can't find, in the docs, a way to hook it up, and it makes me think that it's not possible (or that the docs are incomplete).
As for version 1.2.10, this is not possible without modifying the source code. The relevant section is in Repository\Hierarchy\XmlHierarchyConfigurator.cs:L286
:
IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));
As you can see, it should use this overload (or something along that way) to allow me to achieve my needs.
Activator.CreateInstance(Type, Object[])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解您的目标,但如果您希望您的附加程序是可配置的,您基本上必须在您的附加程序上公开一个属性。然后,您可以通过编程方式或在配置文件中设置此属性。
UdpAppender 公开这样的属性:(
实际上有点复杂,因为它们检查 setter 中的值是否是有效端口。)
在配置文件中,您可以像这样使用它:
这对于像字符串这样的简单类型非常有效, int ...但也适用于一些复杂类型,例如
IPAddress
。如果您有自己的类型,那么使其工作会更加困难,我必须首先检查这是如何完成的。I am not sure if I understand your goal, but if you want your appender to be configurable you basically have to expose a property on your appender. Then you can either set this property either programmatically or in the configuration file.
The UdpAppender exposes a property like this:
(It is actually a bit more complex as they check if the value in the setter is a valid port.)
In the configuration file you use it like this:
This works very well for simple types like string, int ... but also for some complex types like
IPAddress
. If you have your own type then it will be more difficult to make it work and I would have to check first how this is done.至于1.2.10版本,如果不修改源代码,这是不可能的。
相关部分位于 Repository\Hierarchy\XmlHierarchyConfigurator.cs 的第 286 行:
如您所见,它应该使用
overload (or something along that way) to allow me to achieve my needs.
As for version 1.2.10, this is not possible without modifying the source code.
The relevant section is in Repository\Hierarchy\XmlHierarchyConfigurator.cs at line 286:
As you can see, it should use
overload (or something along that way) to allow me to achieve my needs.