如何使用 Log4net 打开/关闭日志记录

发布于 2024-09-30 17:06:33 字数 72 浏览 0 评论 0原文

我已使用 Log4net 记录了所有应用程序事务。有什么方法可以在不更改为日志记录编写的代码的情况下从公共位置禁用和启用日志记录。

I have logged all my applications transaction using Log4net. Is there any way I can disable and enable logging from a common place without altering the code written for logging.

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

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

发布评论

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

评论(3

无畏 2024-10-07 17:06:33

我假设您想通过代码切换正在运行的应用程序中的日志记录。我没有尝试,但我认为调用以下方法应该禁用日志记录:

LogManager.GetRepository().ResetConfiguration();

要重新启用日志记录,您将调用

XmlConfigurator.Configure();

(或 XmlConfigurator 的其他方法之一)。

但是,调用 Configure 似乎存在问题 > 方法重复。我不知道这对你来说是否是个问题,但至少我警告过你......

I assume you want to toggle logging in your running application through code. I did not try it but I think that calling the following method should disable logging:

LogManager.GetRepository().ResetConfiguration();

To re-enable logging you would call

XmlConfigurator.Configure();

(or one of the other methods of the XmlConfigurator).

However there seems to be an issue with calling the Configure method repeatedly. I do not know if this would be a problem for you, but at least I warned you...

痴骨ら 2024-10-07 17:06:33

您可以通过更改 log4net 配置文件来关闭它们。具体将附加程序的级别或全局级别更改为“OFF”值。有关详细信息,请参阅此处

You can turn them off by changing the log4net config file. Specifically change the appender's level or global level to "OFF" value. See here for details

旧瑾黎汐 2024-10-07 17:06:33

假设我从常见问题解答中理解您的问题: http://logging.apache.org/log4net /release/faq.html

如何在运行时完全禁用所有日志记录?

将层次结构上的阈值设置为 Level OFF 将禁用该层次结构中的所有日志记录。这可以在 log4net 配置文件中通过将 log4net 配置元素上的“threshold”属性设置为“OFF”来完成。例如:

Assuming I understand your question, from the FAQ: http://logging.apache.org/log4net/release/faq.html

How do I completely disable all logging at runtime?

Setting the Threshold on the Hierarchy to Level OFF will disable all logging from that Hierarchy. This can be done in the log4net configuration file by setting the "threshold" attribute on the log4net configuration element to "OFF". For example:

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