Skip to content

Commit

Permalink
Merge pull request #467 from ilya-g/FinalRuleMatching
Browse files Browse the repository at this point in the history
LoggingRule.Final only suppresses matching levels.
  • Loading branch information
kichristensen committed Dec 29, 2014
2 parents 9f0a2c1 + 2e7510e commit 12c752f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/NLog/LogFactory.cs
Expand Up @@ -596,7 +596,7 @@ internal void ReconfigExistingLoggers(LoggingConfiguration configuration)
}
}

internal void GetTargetsByLevelForLogger(string name, IList<LoggingRule> rules, TargetWithFilterChain[] targetsByLevel, TargetWithFilterChain[] lastTargetsByLevel)
internal void GetTargetsByLevelForLogger(string name, IList<LoggingRule> rules, TargetWithFilterChain[] targetsByLevel, TargetWithFilterChain[] lastTargetsByLevel, bool[] suppressedLevels)
{
foreach (LoggingRule rule in rules)
{
Expand All @@ -607,11 +607,14 @@ internal void GetTargetsByLevelForLogger(string name, IList<LoggingRule> rules,

for (int i = 0; i <= LogLevel.MaxLevel.Ordinal; ++i)
{
if (i < this.GlobalThreshold.Ordinal || !rule.IsLoggingEnabledForLevel(LogLevel.FromOrdinal(i)))
if (i < this.GlobalThreshold.Ordinal || suppressedLevels[i] || !rule.IsLoggingEnabledForLevel(LogLevel.FromOrdinal(i)))
{
continue;
}

if (rule.Final)
suppressedLevels[i] = true;

foreach (Target target in rule.Targets)
{
var awf = new TargetWithFilterChain(target, rule.Filters);
Expand All @@ -628,12 +631,8 @@ internal void GetTargetsByLevelForLogger(string name, IList<LoggingRule> rules,
}
}

this.GetTargetsByLevelForLogger(name, rule.ChildRules, targetsByLevel, lastTargetsByLevel);
this.GetTargetsByLevelForLogger(name, rule.ChildRules, targetsByLevel, lastTargetsByLevel, suppressedLevels);

if (rule.Final)
{
break;
}
}

for (int i = 0; i <= LogLevel.MaxLevel.Ordinal; ++i)
Expand All @@ -650,10 +649,11 @@ internal LoggerConfiguration GetConfigurationForLogger(string name, LoggingConfi
{
TargetWithFilterChain[] targetsByLevel = new TargetWithFilterChain[LogLevel.MaxLevel.Ordinal + 1];
TargetWithFilterChain[] lastTargetsByLevel = new TargetWithFilterChain[LogLevel.MaxLevel.Ordinal + 1];
bool[] suppressedLevels = new bool[LogLevel.MaxLevel.Ordinal + 1];

if (configuration != null && this.IsLoggingEnabled())
{
this.GetTargetsByLevelForLogger(name, configuration.LoggingRules, targetsByLevel, lastTargetsByLevel);
this.GetTargetsByLevelForLogger(name, configuration.LoggingRules, targetsByLevel, lastTargetsByLevel, suppressedLevels);
}

InternalLogger.Debug("Targets for {0} by level:", name);
Expand Down
29 changes: 29 additions & 0 deletions tests/NLog.UnitTests/Config/RuleConfigurationTests.cs
Expand Up @@ -296,5 +296,34 @@ public void FiltersTest()
Assert.Equal("starts-with(message, 'z')", conditionBasedFilter.Condition.ToString());
Assert.Equal(FilterResult.Ignore, conditionBasedFilter.Action);
}


[Fact]
public void LoggingRule_Final_SuppressesOnlyMatchingLevels()
{
LoggingConfiguration c = CreateConfigurationFromString(@"
<nlog>
<targets>
<target name='d1' type='Debug' layout='${message}' />
</targets>
<rules>
<logger name='a' level='Debug' final='true' />
<logger name='*' minlevel='Debug' writeTo='d1' />
</rules>
</nlog>");

LogManager.Configuration = c;
Logger a = LogManager.GetLogger("a");
Assert.False(a.IsDebugEnabled);
Assert.True(a.IsInfoEnabled);
a.Info("testInfo");
a.Debug("suppressedDebug");
AssertDebugLastMessage("d1", "testInfo");

Logger b = LogManager.GetLogger("b");
b.Debug("testDebug");
AssertDebugLastMessage("d1", "testDebug");
}
}
}

0 comments on commit 12c752f

Please sign in to comment.