log4net - 使用多个配置文件进行配置

发布于 2024-08-31 03:03:39 字数 282 浏览 6 评论 0 原文

我有一个由主机和可插入模块(插件)组成的应用程序。

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

只有主机才有 App.config 文件。这些插件有自己的配置文件,其中包含 log4net 配置部分。

从插件之一调用 XmlConfigurator.Configure 会覆盖主机的 app.config log4net 定义。

有没有一种简单的方法来附加配置而不是覆盖它们?

谢谢, 盖。

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

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.

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

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?

Thanks,
Gai.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

苍暮颜 2024-09-07 03:03:39

看来您已经找到了适合您的解决方案。然而,我找到了一个不同的解决方案,我认为它“更理想”,所以我将其发布在这里,供将来可能发现这个问题的人使用。

log4net 有一个称为存储库的概念,可以单独配置。它不是很流行,也没有很好的文档记录,但这里是我找到的一些文档:

http:// /logging.apache.org/log4net/release/manual/repositories.html

无论如何,您需要做的就是在您的插件程序集或具有唯一的存储库名称的程序集上添加 RepositoryAttribute插件和 log4net 会将其与其他所有内容分开。

It appears you already have found a solution that works for you. However, I've found a different solution which I consider to be "more ideal", so I'll post it here for whoever might find this question in the future.

log4net has a concept called repositories which can be configured separately. It's not very popular nor very well documented, but here's some documentation I found:

http://logging.apache.org/log4net/release/manual/repositories.html

Anyway, all you need to do is add RepositoryAttribute on your plugin assembly or assemblies with a repository name unique to that plugin and log4net will keep it separate from everything else.

铜锣湾横着走 2024-09-07 03:03:39

另一种方法是在多个位置拥有多个 log4net 配置文件,将它们全部加载到 XDocument 中以有效地组合一个文件,然后调用 XmlConfigurator.Configure()

我这样做的方法是按照配置的 XSD 创建每个文件,将每个文件的 log4net 根节点的所有子节点加载到单独的 XElement 实例中,并将它们合并到一个带有根 log4net 节点的新 XDocument

查找文件是另一回事:使用某种约定来扫描文件(例如*.dll.log4net.config),将它们嵌入到程序集中,或者以其他方式将它们与模块打包在一起。

Bruno Marotta 在这里提供了此类代码的实现:

www.kopf.com.br/kaplof/using-multiple-configuration-文件与 log4net

Yet another way is to have multiple log4net configuration files in multiple places, load them all up in an XDocument to effectively compose a single one, and then call XmlConfigurator.Configure().

The way I did this was to create each file following the XSD for the configuration, load all the children of each file's log4net root node into separate XElement instances and merge them into a new XDocument with a root log4net node.

Finding the files is another matter: use some sort of convention to scan for files (e.g. *.dll.log4net.config), embed them in the assemblies, or package them some other way with your modules.

Bruno Marotta has offered an implementation of such code here:

www.kopf.com.br/kaplof/using-multiple-configuration-files-with-log4net

余生再见 2024-09-07 03:03:39

我自己刚刚遇到了这个问题。虽然这并不完全是我所说的“理想”,但我认为目前最好的解决方案是使用 XmlConfigurator 类的 ConfigureAndWatch 方法。基本上,您的主机程序集使用特定的配置文件设置 log4net 配置。当您的插件加载时,让它们将其配置元素写入同一配置文件中的 log4net 配置部分。由于 log4net 已被告知使用 ConfigureAndWatch 监视该文件的更改,因此当文件添加了新数据时,log4net 将重新加载配置,该配置现在将包含来自插件的配置元素。

这有点hackish,但似乎有效。当然,下一步会将其移至我的日志记录框架中,以便联合对配置文件的访问。

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

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.

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