[英]How can I unsubscribe a NLog target
我想要一个 NLog 目标停止监听日志。 RemoveTarget方法似乎不起作用。 这是一个失败的测试。
public class when_stopping_to_listen
{
static Logger Logger;
static MemoryTarget target;
Establish context = () =>
{
var config = new LoggingConfiguration();
Logger = LogManager.GetLogger("TestLogger");
target = new MemoryTarget {Layout = "${message}", Name = "TestTarget"};
config.AddTarget(target.Name, target);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, target));
LogManager.Configuration = config;
};
Because of = () =>
{
var config = LogManager.Configuration;
config.RemoveTarget(target.Name);
LogManager.Configuration = config;
Logger.Info("Test");
};
It should_be_empty = () => target.Logs.ShouldBeEmpty();
}
提前致谢。
我不知道为什么RemoveTarget
不起作用。 但是,如果您从每个规则中删除目标,则测试通过:
Because of = () =>
{
foreach (var rule in config.LoggingRules)
{
rule.Targets.Remove(target);
}
Logger.Info("Test");
};
如果您删除LoggingRule
而不是目标,它也可以工作:
public class when_stopping_to_listen
{
//...
static LoggingRule rule;
Establish context = () =>
{
//...
rule = new LoggingRule("*", LogLevel.Trace, target);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
};
Because of = () =>
{
var config = LogManager.Configuration;
config.LoggingRules.Remove(rule);
LogManager.Configuration = config;
Logger.Info("Test");
};
//...
}
NLog 版本。 4.5 修复了LoggingConfiguration.RemoveTarget
以便从所有注册的 LoggingRules 中删除目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.