log4net-使用多个配置文件进行配置 [英] log4net - configure using multiple configuration files

查看:459
本文介绍了log4net-使用多个配置文件进行配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含主机和可插拔模块(插件)的应用程序.

I have an application consisting of a host and pluggable modules (plugins).

我希望能够为主机和每个其他模块配置log4net. 他们每个人都应该有自己的配置文件,并且每个人都将登录到不同的文件.

I want to be able to configure log4net for the host and for each of the other modules. Each of them should have its own configuration file and each will log to a different file.

仅主机具有App.config文件.插件有自己的配置文件,其中包含log4net配置节.

Only the host has an App.config file. The plugins have their own config file containing the log4net config sections.

从其中一个插件调用XmlConfigurator.Configure将覆盖主机的app.config log4net定义.

Calling XmlConfigurator.Configure from one of the plugins overrides the host's app.config log4net definitions.

是否有一种简单的方法可以附加配置而不是覆盖配置?

Is there an easy way to append configurations instead of overriding them?

谢谢, 盖.

推荐答案

我自己遇到了这个问题.尽管这并不是我所谓的理想",但我认为当前最好的解决方案是使用XmlConfigurator类的ConfigureAndWatch方法.基本上,您的主机部件使用特定的配置文件来设置log4net配置.当您的插件加载时,让他们将其配置元素写入同一配置文件中的log4net配置部分.由于已告知log4net使用ConfigureAndWatch监视该文件的更改,因此当文件中添加了新数据时,log4net将重新加载配置,该配置现在将包括插件中的配置元素.

I just ran into this problem myself. While it's not exactly what I'd call "ideal," I think the best solution currently is to use the ConfigureAndWatch method of the XmlConfigurator class. Basically, you have your host assembly setup the log4net configuration using a specific configuration file. When your plugins load, have them write their configuration elements into the log4net configuration section in that same configuration file. Since log4net has been told to watch that file for changes with ConfigureAndWatch, when the file has that new data added, log4net will reload the configuration which will now include the configuration elements from the plugin(s).

这有点骇人听闻,但似乎可行.下一步当然是将其移至我的日志记录框架中,以便对配置文件进行联合访问.

This is a bit hackish, but it seems to work. The next step of course will be moving this into my logging framework so that access to the configuration file is federated.

请注意,在写入/保存插件的配置与重新加载更新的配置并将其应用于记录器存储库之间存在短暂的延迟.

Do note that there is a short delay between the plugin's configuration being written/saved and the updated configuration being reloaded and applied to the logger repository.

这篇关于log4net-使用多个配置文件进行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆