How can I unsubscribe a NLog target
I don't know why RemoveTarget
doesn't work. But if you remove the target from each rule the test passes:
Because of = () =>
{
foreach (var rule in config.LoggingRules)
{
rule.Targets.Remove(target);
}
Logger.Info("Test");
};
And if you remove the LoggingRule
instead of the target it also works:
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 ver. 4.5 fixes LoggingConfiguration.RemoveTarget
so it removes target from all registered LoggingRules.
See also: https://github.com/NLog/NLog/pull/2549