Skip to content

Commit

Permalink
LoggingConfigurationParser - Recognize LoggingRule.DefaultFilterAction (
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Mar 23, 2021
1 parent 4802c8a commit 9c2ce5b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
38 changes: 25 additions & 13 deletions src/NLog/Config/LoggingConfigurationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ private LoggingRule ParseRuleElement(ILoggingConfigurationElement loggerElement)
bool enabled = true;
bool final = false;
string writeTargets = null;
string defaultFilterAction = null;
foreach (var childProperty in loggerElement.Values)
{
switch (childProperty.Key?.Trim().ToUpperInvariant())
Expand Down Expand Up @@ -608,6 +609,9 @@ private LoggingRule ParseRuleElement(ILoggingConfigurationElement loggerElement)
case "MAXLEVEL":
maxLevel = childProperty.Value;
break;
case "DEFAULTFILTERACTION":
defaultFilterAction = childProperty.Value;
break;
default:
InternalLogger.Debug("Skipping unknown property {0} for element {1} in section {2}",
childProperty.Key, loggerElement.Name, "rules");
Expand Down Expand Up @@ -640,7 +644,7 @@ private LoggingRule ParseRuleElement(ILoggingConfigurationElement loggerElement)

ParseLoggingRuleTargets(writeTargets, rule);

ParseLoggingRuleChildren(loggerElement, rule);
ParseLoggingRuleChildren(loggerElement, rule, defaultFilterAction);

return rule;
}
Expand Down Expand Up @@ -724,14 +728,14 @@ private void ParseLoggingRuleTargets(string writeTargets, LoggingRule rule)
}
}

private void ParseLoggingRuleChildren(ILoggingConfigurationElement loggerElement, LoggingRule rule)
private void ParseLoggingRuleChildren(ILoggingConfigurationElement loggerElement, LoggingRule rule, string defaultFilterAction = null)
{
foreach (var child in loggerElement.Children)
{
LoggingRule childRule = null;
if (child.MatchesName("filters"))
{
ParseFilters(rule, child);
ParseFilters(rule, child, defaultFilterAction);
}
else if (child.MatchesName("logger") && loggerElement.MatchesName("logger"))
{
Expand All @@ -757,23 +761,22 @@ private void ParseLoggingRuleChildren(ILoggingConfigurationElement loggerElement
}
}

private void ParseFilters(LoggingRule rule, ILoggingConfigurationElement filtersElement)
private void ParseFilters(LoggingRule rule, ILoggingConfigurationElement filtersElement, string defaultFilterAction = null)
{
filtersElement.AssertName("filters");

var defaultActionResult = filtersElement.GetOptionalValue("defaultAction", null);
if (defaultActionResult != null)
defaultFilterAction = filtersElement.GetOptionalValue("defaultAction", null) ?? filtersElement.GetOptionalValue("defaultFilterAction", null) ?? defaultFilterAction;
if (defaultFilterAction != null)
{
PropertyHelper.SetPropertyFromString(rule, nameof(rule.DefaultFilterResult), defaultActionResult,
PropertyHelper.SetPropertyFromString(rule, nameof(rule.DefaultFilterResult), defaultFilterAction,
_configurationItemFactory);
}

foreach (var filterElement in filtersElement.Children)
{
string name = filterElement.Name;

Filter filter = _configurationItemFactory.Filters.CreateInstance(name);
ConfigureObjectFromAttributes(filter, filterElement, false);
var filterType = filterElement.GetOptionalValue("type", null) ?? filterElement.Name;
Filter filter = _configurationItemFactory.Filters.CreateInstance(filterType);
ConfigureObjectFromAttributes(filter, filterElement, true);
rule.Filters.Add(filter);
}
}
Expand Down Expand Up @@ -1044,10 +1047,19 @@ private void ConfigureObjectFromAttributes(object targetObject, ILoggingConfigur
PropertyHelper.SetPropertyFromString(targetObject, childName, ExpandSimpleVariables(childValue),
_configurationItemFactory);
}
catch (NLogConfigurationException ex)
{
if (MustThrowConfigException(ex))
throw;
}
catch (Exception ex)
{
InternalLogger.Warn(ex, "Error when setting '{0}' on attibute '{1}'", childValue, childName);
throw;
if (ex.MustBeRethrownImmediately())
throw;

var configException = new NLogConfigurationException(ex, $"Error when setting value '{childValue}' for property '{childName}' on element '{element}'");
if (MustThrowConfigException(configException))
throw;
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/NLog.UnitTests/Config/RuleConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,33 @@ public void FiltersTest_defaultFilterAction()
AssertDebugLastMessage("d1", "test 1");
}

[Fact]
public void FiltersTest_defaultFilterResult()
{
LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@"
<nlog>
<targets>
<target name='d1' type='Debug' layout='${message}' />
</targets>
<rules>
<logger name='*' level='Warn' writeTo='d1'>
<filters defaultFilterAction='Ignore'>
<filter type='when' condition=""starts-with(message, 't')"" action='Log' />
</filters>
</logger>
</rules>
</nlog>");

LogManager.Configuration = c;
var logger = LogManager.GetLogger("logger1");
logger.Warn("test 1");
AssertDebugLastMessage("d1", "test 1");

logger.Warn("x-mass");
AssertDebugLastMessage("d1", "test 1");
}

[Fact]
public void FiltersTest_defaultFilterAction_noRules()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/NLog.UnitTests/Config/XmlConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void XmlConfig_ParseFilter_WithoutAttributes()
<target name='debug' type='Debug' layout='${message}' />
</targets>
<rules>
<logger name='*' minlevel='debug' appendto='debug' defaultFilterResult='ignore'>
<logger name='*' minlevel='debug' appendto='debug' defaultFilterAction='ignore'>
<filters>
<whenContains />
</filters>
Expand Down

0 comments on commit 9c2ce5b

Please sign in to comment.