如何在运行时增加NLOG日志级别
我的应用程序在程序开头有太多log.trace()
,导致程序出现故障,因此我无法使用常规nlog
配置来增加日志等级。
因此,我想在运行时提高日志级别。为此,我在互联网上发现了类似的东西:
foreach (var rule in LogManager.Configuration.LoggingRules)
{
// Iterate over all levels up to and including the target, (re)enabling them.
for (int i = 0; i <= 5; i++)
{
rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
}
}
但是,这似乎并不是在日志中显示以下行,尽管我确定这一行已经通过:
log.Trace("log trace works!");
有人知道确保从我的源代码的某个时刻开始,我可以看到log.trace()
结果?
有关您的信息:
log
定义为私有静态读取logger log = logmanager.getCurrentClassLogger();
。- 我一直在寻找一个
nlog。*
文件,其中包含&lt; nlog&gt;
标签,我什么也没找到。
编辑:
我正在使用nlog
版本4.5.4。
edit2(尝试强制读取配置):
我已经修改了nlog.config
文件如下:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<variable name="myLevel" value="Trace" />
<rules>
<logger minLevel="${var:myLevel}" />
</rules>
...
然后,我尝试强制重新阅读该配置,如下所示:
NLog.LogManager.Setup().LoadConfigurationFromFile("C:\\...\\NLog.config");
谢谢
My application has too much log.Trace()
at the beginning of the program, causing the program to malfunction, so I can't use general NLog
configuration to increase the log level.
So, I would like to increase the log level at runtime. For that, I've found something like this on the internet:
foreach (var rule in LogManager.Configuration.LoggingRules)
{
// Iterate over all levels up to and including the target, (re)enabling them.
for (int i = 0; i <= 5; i++)
{
rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
}
}
This, however, seems not to be working as the following line does not show up in the logs, although I'm sure this line has been passed:
log.Trace("log trace works!");
Does anybody know a way to make sure that, from a certain moment in my source code, I can see log.Trace()
results?
For your information:
log
is defined asprivate static readonly Logger log = LogManager.GetCurrentClassLogger();
.- I've been looking for an
nlog.*
file, containing an<nlog>
tag, I didn't find anything.
Edit:
I'm working with NLog
version 4.5.4.
Edit2 (try to force reading of configuration):
I've modified the nlog.config
file as follows:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<variable name="myLevel" value="Trace" />
<rules>
<logger minLevel="${var:myLevel}" />
</rules>
...
Then I tried to force the re-reading of that configuration, as follows:
NLog.LogManager.Setup().LoadConfigurationFromFile("C:\\...\\NLog.config");
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
logmanager.reconfigexistingLoggers()
在循环似乎对我有用。配置
代码
Calling
LogManager.ReconfigExistingLoggers()
after the loop seems to work for me.Config
Code