以编程方式添加、启用和禁用 NLog 记录器

发布于 2024-12-05 10:54:25 字数 37 浏览 0 评论 0原文

如何从 NLog 代码中添加、编辑、删除、启用和禁用记录器?

How can I add, edit, delete, enable, and disable loggers from code for NLog?

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

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

发布评论

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

评论(3

時窥 2024-12-12 10:54:25

添加:

var logTarget = new ...
logTarget.Layout = "Your layout format here";
// e.g. "${logger}: ${message} ${exception:format=tostring}";

// specify what gets logged to the above target
var loggingRule = new LoggingRule("*", LogLevel.Debug, logTarget);

// add target and rule to configuration
LogManager.Configuration.AddTarget("targetName", logTarget);
LogManager.Configuration.LoggingRules.Add(loggingRule);
LogManager.Configuration.Reload();

删除完成

LogManager.Configuration.LoggingRules.Remove(loggingRule);
LogManager.Configuration.Reload();

To add:

var logTarget = new ...
logTarget.Layout = "Your layout format here";
// e.g. "${logger}: ${message} ${exception:format=tostring}";

// specify what gets logged to the above target
var loggingRule = new LoggingRule("*", LogLevel.Debug, logTarget);

// add target and rule to configuration
LogManager.Configuration.AddTarget("targetName", logTarget);
LogManager.Configuration.LoggingRules.Add(loggingRule);
LogManager.Configuration.Reload();

Removal is done with

LogManager.Configuration.LoggingRules.Remove(loggingRule);
LogManager.Configuration.Reload();
↘紸啶 2024-12-12 10:54:25

我知道这是一个旧答案,但我想为任何希望以编程方式修改目标和记录规则的人提供反馈,因为 Configuration.Reload() 不起作用。

要以编程方式更新现有目标,您需要使用 ReconfigExistingLoggers 方法:

var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
target.FileName = "${logDirectory}/file2.txt";
LogManager.ReconfigExistingLoggers();

动态添加和删除日志记录规则的示例:

if (VerboseLogging && !LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Add(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}
else if (!VerboseLogging && LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Remove(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}

如文档中所写:

循环访问之前由 GetLogger 返回的所有记录器。和
重新计算他​​们的目标和过滤器列表。修改后有用
以编程方式配置以确保所有记录器都已
正确配置。

这个答案和示例来自托尼的答案:

在运行时更新 NLog 目标文件名

I know this is an old answer but I wanted give feedback for anyone looking to make modifications to their targets and logging rules programmatically that Configuration.Reload() doesn't work.

To update existing targets programmatically you need to use the ReconfigExistingLoggers method:

var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
target.FileName = "${logDirectory}/file2.txt";
LogManager.ReconfigExistingLoggers();

An example that adds and removes logging rules on the fly:

if (VerboseLogging && !LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Add(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}
else if (!VerboseLogging && LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Remove(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}

As written in docs:

Loops through all loggers previously returned by GetLogger. and
recalculates their target and filter list. Useful after modifying the
configuration programmatically to ensure that all loggers have been
properly configured.

This answer and sample comes from Tony's answer in:

Update NLog target filename at runtime

深巷少女 2024-12-12 10:54:25

NLog 4.6.7 可以将布局变量分配给 LoggingRule 级别,并在运行时更改这些布局变量。

<nlog>
    <variable name="myLevel" value="Warn" />
    <rules>
      <logger minLevel="${var:myLevel}" />
    </rules>
</nlog>

然后您可以在代码中执行此操作:

LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();

另请参阅: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

NLog 4.6.7 makes it possible to assign Layout-variables to LoggingRule-levels, and change these Layout-variables at runtime.

<nlog>
    <variable name="myLevel" value="Warn" />
    <rules>
      <logger minLevel="${var:myLevel}" />
    </rules>
</nlog>

Then you can do this in code:

LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();

See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

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