我可以将参数传递给自定义 log4net Appender 的构造函数吗?

发布于 2024-09-27 23:21:22 字数 443 浏览 0 评论 0原文

我想将参数传递给自定义附加程序的构造函数,因此我想我必须重写附加程序的初始化机制。问题是我在文档中找不到连接它的方法,这让我认为这是不可能的(或者文档不完整)。

至于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-10-04 23:21:22

我不确定我是否理解您的目标,但如果您希望您的附加程序是可配置的,您基本上必须在您的附加程序上公开一个属性。然后,您可以通过编程方式或在配置文件中设置此属性。

UdpAppender 公开这样的属性:(

public int LocalPort
{
   get; set;
}

实际上有点复杂,因为它们检查 setter 中的值是否是有效端口。)

在配置文件中,您可以像这样使用它:

<localPort value="8080" />

这对于像字符串这样的简单类型非常有效, 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:

public int LocalPort
{
   get; set;
}

(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:

<localPort value="8080" />

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.

风透绣罗衣 2024-10-04 23:21:22

至于1.2.10版本,如果不修改源代码,这是不可能的。

相关部分位于 Repository\Hierarchy\XmlHierarchyConfigurator.cs 的第 286 行:

`IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));`

如您所见,它应该使用

Activator.CreateInstance(Type, Object[])

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:

`IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));`

As you can see, it should use

Activator.CreateInstance(Type, Object[])

overload (or something along that way) to allow me to achieve my needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文