Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilteringTargetWrapper - Fix XSD for Filter-property #3476

Merged
merged 4 commits into from Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,6 +31,8 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using NLog.Filters;

namespace NLog.UnitTests.Targets.Wrappers
{
using System;
Expand All @@ -43,7 +45,7 @@ namespace NLog.UnitTests.Targets.Wrappers
using Xunit;

public class FilteringTargetWrapperTests : NLogTestBase
{
{
[Fact]
public void FilteringTargetWrapperSyncTest1()
{
Expand Down Expand Up @@ -273,10 +275,11 @@ public void FilteringTargetWrapperWhenRepeatedFilter()
{
LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString(@"
<nlog>
<variable name='test' value='${message}' />
<targets>
<target name='debug' type='BufferingWrapper'>
<target name='filter' type='FilteringWrapper'>
<filter type='whenRepeated' layout='${message}' timeoutSeconds='30' action='Ignore' />
<filter type='whenRepeated' layout='${var:test:whenempty=${guid}}' timeoutSeconds='30' action='Ignore' />
<target name='memory' type='Memory' />
</target>
</target>
Expand All @@ -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<MemoryTarget>("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<FilteringTargetWrapper>("target1");

Assert.Equal(expectedCondition, myTarget.Condition?.ToString());
var conditionBasedFilter = Assert.IsType<ConditionBasedFilter>(myTarget.Filter);
Assert.Equal(expectedCondition, conditionBasedFilter.Condition?.ToString());
}

private static XmlLoggingConfiguration CreateConfigWithCondition()
{
return XmlLoggingConfiguration.CreateFromXmlString(@"
<nlog>
<targets>
<target name='target1' type='FilteringWrapper' condition='length(message) &gt; 2' >
<target name='memory' type='Memory' />
</target>
</targets>
<rules>
<logger name='*' minlevel='Debug' writeTo='target1'/>
</rules>
</nlog>");
}


class MyAsyncTarget : Target
{
public int WriteCount { get; private set; }
Expand Down
30 changes: 17 additions & 13 deletions tools/MakeNLogXSD/XsdFileGenerator.cs
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down