如何在运行时增加NLOG日志级别

发布于 2025-01-24 20:58:18 字数 1429 浏览 5 评论 0原文

我的应用程序在程序开头有太多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()结果?

有关您的信息:

  1. log定义为私有静态读取logger log = logmanager.getCurrentClassLogger();
  2. 我一直在寻找一个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:

  1. log is defined as private static readonly Logger log = LogManager.GetCurrentClassLogger();.
  2. 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 技术交流群。

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

发布评论

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

评论(1

童话里做英雄 2025-01-31 20:58:18

调用logmanager.reconfigexistingLoggers()在循环似乎对我有用。

配置

<logger name="*" minlevel="Info" writeTo="console" />

代码

logger.Trace("Trace 1");
logger.Info("Info 1");

foreach (var rule in LogManager.Configuration.LoggingRules)
{
    for (var i = 0; i <= 5; i++)
    {
        rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
    }
}

LogManager.ReconfigExistingLoggers();

logger.Trace("Trace 2"); // This is now printed.
logger.Info("Info 2");

Calling LogManager.ReconfigExistingLoggers() after the loop seems to work for me.

Config

<logger name="*" minlevel="Info" writeTo="console" />

Code

logger.Trace("Trace 1");
logger.Info("Info 1");

foreach (var rule in LogManager.Configuration.LoggingRules)
{
    for (var i = 0; i <= 5; i++)
    {
        rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
    }
}

LogManager.ReconfigExistingLoggers();

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