使用 log4net 始终记录值

发布于 2024-10-03 14:43:12 字数 528 浏览 0 评论 0原文

我在应用程序中选择了几个我希望始终记录值的位置。我可以简单地使用 Log.Info() 并保留它,但我更喜欢一个不会​​因级别配置的意外更改而禁用的解决方案。在这种情况下,只要 log4net 未禁用,我就希望触发这些日志语句。

最好的方法是什么?

只是看看一些 信息 看起来一种选择是创建一个自定义级别,其值设置为高于“紧急”,但我不知道这是否是一个带有我没有意识到的副作用的残酷可怕的黑客攻击,或者是一个合法的选择。我在文档中找不到任何明确的指导。

I have select few places in my application where I'd like to always log values. I could simply use Log.Info() and leave it at that, but I'd prefer a solution that can't be disabled by an accidental change to the level configuration. In this case, as long is log4net is not disabled, I want these log statements to fire.

What's the best approach?

Just looking at some information it looks like one option is to create a custom level with a value set above Emergency, but I don't know if that's a brutally awful hack with side effects I'm not realizing or a legitimate option. I couldn't find any clear guidance in the documentation.

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

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

发布评论

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

评论(1

神也荒唐 2024-10-10 14:43:12

我不是 log4net 专家,但类似这样的事情可能会满足您的要求:

此代码将从 LogManager 获取命名记录器,并以编程方式将其级别设置为“ALL”。如果您稍后在代码中检索此记录器,它将始终记录日志,即使在配置文件中将日志级别设置为 OFF 也是如此。

要进行测试,请在配置文件中将根日志级别设置为“OFF”,然后使用下面的代码:

  log4net.ILog log = log4net.LogManager.GetLogger("abc");
  log.Info("this won't log because root logger is OFF");

  //Reset the level
  log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;

  l.Level = l.Hierarchy.LevelMap["ALL"];

  //Try to log again
  log.Info("this will log because we just reset abc's level to ALL");

我测试了它,它似乎确实有效。

我在此处此处

I am not a log4net expert, but something like this might do what you want:

This code will get a named logger from the LogManager and will programmatically set its level to "ALL". If you retrieve this logger later in your code, it will always log, even if the log level is set to OFF in the config file.

To test, set the root log level to "OFF" in the config file, then use the code below:

  log4net.ILog log = log4net.LogManager.GetLogger("abc");
  log.Info("this won't log because root logger is OFF");

  //Reset the level
  log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;

  l.Level = l.Hierarchy.LevelMap["ALL"];

  //Try to log again
  log.Info("this will log because we just reset abc's level to ALL");

I tested it and it does seem to work.

I found this information here and here.

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