使用 log4net 始终记录值
我在应用程序中选择了几个我希望始终记录值的位置。我可以简单地使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 log4net 专家,但类似这样的事情可能会满足您的要求:
此代码将从 LogManager 获取命名记录器,并以编程方式将其级别设置为“ALL”。如果您稍后在代码中检索此记录器,它将始终记录日志,即使在配置文件中将日志级别设置为 OFF 也是如此。
要进行测试,请在配置文件中将根日志级别设置为“OFF”,然后使用下面的代码:
我测试了它,它似乎确实有效。
我在此处和此处。
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:
I tested it and it does seem to work.
I found this information here and here.