To implement the max level programmatically in condition

104 views Asked by At

I had

   <logger name="*" minlevel="Trace" writeTo="f1" /> 

Now I added the maxlevel. so

<logger name="*" minlevel="Trace" maxlevel="Error" writeTo="f1" />

I want to implement it programmatically. What I tried

Then in code:

var filter = new ConditionBasedFilter();
filter.Action = FilterResult.Log;
filter.Condition = "(level <= LogLevel.Info)";
var loggerRule = new LoggingRule(ruleName, LogLevel.Debug, target);
loggerRule.Filters.Add(filter);
nLogLoggingConfiguration.LoggingRules.Add(loggerRule);

But the filter is not working, because the result is same as if I don't add the filter. Which means whether I comment out the line loggerRule.Filters.Add(filter); or not, I get the same outcome.

EDIT:

It maybe the bug of NLOG. When you set up filter.Action = FilterResult.Log; and var loggerRule = new LoggingRule(ruleName, LogLevel.Debug, target);.

The condition cannot be applied to the rule.

1

There are 1 answers

0
AudioBubble On BEST ANSWER

We can do the following:

filter.Action = FilterResult.Ignore;
filter.Condition = "(level == LogLevel.Trace or level == LogLevel.Warn or level == LogLevel.Error or level == LogLevel.Fatal)"