diff --git a/tests/NLog.UnitTests/Targets/Wrappers/FilteringTargetWrapperTests.cs b/tests/NLog.UnitTests/Targets/Wrappers/FilteringTargetWrapperTests.cs index ec36acf28f..5931bf1184 100644 --- a/tests/NLog.UnitTests/Targets/Wrappers/FilteringTargetWrapperTests.cs +++ b/tests/NLog.UnitTests/Targets/Wrappers/FilteringTargetWrapperTests.cs @@ -31,6 +31,8 @@ // THE POSSIBILITY OF SUCH DAMAGE. // +using NLog.Filters; + namespace NLog.UnitTests.Targets.Wrappers { using System; @@ -43,7 +45,7 @@ namespace NLog.UnitTests.Targets.Wrappers using Xunit; public class FilteringTargetWrapperTests : NLogTestBase - { + { [Fact] public void FilteringTargetWrapperSyncTest1() { @@ -273,10 +275,11 @@ public void FilteringTargetWrapperWhenRepeatedFilter() { LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@" + - + @@ -301,6 +304,57 @@ public void FilteringTargetWrapperWhenRepeatedFilter() Assert.Equal(5, myTarget.Logs.Count); } + [Fact] + public void FilteringTargetWrapperWithConditionAttribute_correctBehavior() + { + // Arrange + LogManager.Configuration = CreateConfigWithCondition(); + var myTarget = LogManager.Configuration.FindTargetByName("memory"); + + // Act + var logger = LogManager.GetLogger(nameof(FilteringTargetWrapperWhenRepeatedFilter)); + logger.Info("Hello World"); + logger.Info("2"); // Will be ignored + logger.Info("3"); // Will be ignored + LogManager.Flush(); + + // Assert + Assert.Equal(1, myTarget.Logs.Count); + } + + [Fact] + public void FilteringTargetWrapperWithConditionAttribute_validCondition() + { + // Arrange + var expectedCondition = "(length(message) > 2)"; + + // Act + var config = CreateConfigWithCondition(); + + // Assert + var myTarget = config.FindTargetByName("target1"); + + Assert.Equal(expectedCondition, myTarget.Condition?.ToString()); + var conditionBasedFilter = Assert.IsType(myTarget.Filter); + Assert.Equal(expectedCondition, conditionBasedFilter.Condition?.ToString()); + } + + private static XmlLoggingConfiguration CreateConfigWithCondition() + { + return XmlLoggingConfiguration.CreateFromXmlString(@" + + + + + + + + + + "); + } + + class MyAsyncTarget : Target { public int WriteCount { get; private set; } diff --git a/tools/MakeNLogXSD/XsdFileGenerator.cs b/tools/MakeNLogXSD/XsdFileGenerator.cs index f9069e47e5..fbef5ebf03 100644 --- a/tools/MakeNLogXSD/XsdFileGenerator.cs +++ b/tools/MakeNLogXSD/XsdFileGenerator.cs @@ -206,13 +206,13 @@ private static XElement GetAttributeElement(XElement propertyElement) result.Add(new XAttribute("type", enumType)); } - else if (IgnoreTypes.Contains(propertyType)) + else { - return null; - } - else - { - result.Add(new XAttribute("type", GetXsdType(propertyType, true))); + string xsdType = GetXsdType(propertyType, false); + if (xsdType == null) + return null; + + result.Add(new XAttribute("type", xsdType)); } var doc = propertyElement.Element("doc"); @@ -284,14 +284,14 @@ private static XElement GetPropertyElement(XElement propertyElement) result.Add(new XAttribute("maxOccurs", "1")); result.Add(new XAttribute("type", enumType)); } - else if (IgnoreTypes.Contains(propertyType)) - { - return null; - } else { + string xsdType = GetXsdType(propertyType, false); + if (xsdType == null) + return null; + result.Add(new XAttribute("maxOccurs", "1")); - result.Add(new XAttribute("type", GetXsdType(propertyType, false))); + result.Add(new XAttribute("type", xsdType)); } return result; @@ -301,19 +301,23 @@ private static XElement GetPropertyElement(XElement propertyElement) private static string GetXsdType(string apiTypeName, bool attribute) { - if (string.IsNullOrWhiteSpace(apiTypeName)) { throw new NotSupportedException("Unknown API type '" + apiTypeName + "'."); } + if (IgnoreTypes.Contains(apiTypeName)) + { + return null; + } + switch (apiTypeName) { case "Layout": return attribute ? "SimpleLayoutAttribute" : "Layout"; case "NLog.Filters.Filter": - return "Filter"; + return attribute ? null : "Filter"; case "Condition": return "Condition";