Skip to content

Commit

Permalink
BUG 6491: Creating target w/ xmlns fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowalski committed Nov 1, 2011
1 parent 853e37a commit 135261d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/NLog/Config/XmlLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ private static string CleanWhitespace(string s)
return s;
}

private static string StripOptionalNamespacePrefix(string attributeValue)
{
if (attributeValue == null)
{
return null;
}

int p = attributeValue.IndexOf(':');
if (p < 0)
{
return attributeValue;
}

return attributeValue.Substring(p + 1);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Target is disposed elsewhere.")]
private static Target WrapWithAsyncTargetWrapper(Target target)
{
Expand Down Expand Up @@ -498,7 +514,7 @@ private void ParseTargetsElement(NLogXmlElement targetsElement)
foreach (var targetElement in targetsElement.Children)
{
string name = targetElement.LocalName;
string type = targetElement.GetOptionalAttribute("type", null);
string type = StripOptionalNamespacePrefix(targetElement.GetOptionalAttribute("type", null));

switch (name.ToUpper(CultureInfo.InvariantCulture))
{
Expand Down Expand Up @@ -580,7 +596,7 @@ private void ParseTargetElement(Target target, NLogXmlElement targetElement)

if (IsTargetElement(name))
{
string type = childElement.GetRequiredAttribute("type");
string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type"));

Target newTarget = this.configurationItemFactory.Targets.CreateInstance(type);
if (newTarget != null)
Expand Down Expand Up @@ -616,7 +632,7 @@ private void ParseTargetElement(Target target, NLogXmlElement targetElement)

if (IsTargetElement(name))
{
string type = childElement.GetRequiredAttribute("type");
string type = StripOptionalNamespacePrefix(childElement.GetRequiredAttribute("type"));

Target newTarget = this.configurationItemFactory.Targets.CreateInstance(type);
if (newTarget != null)
Expand Down Expand Up @@ -658,7 +674,7 @@ private void ParseExtensionsElement(NLogXmlElement extensionsElement, string bas
prefix = prefix + ".";
}

string type = addElement.GetOptionalAttribute("type", null);
string type = StripOptionalNamespacePrefix(addElement.GetOptionalAttribute("type", null));
if (type != null)
{
this.configurationItemFactory.RegisterType(Type.GetType(type, true), prefix);
Expand Down Expand Up @@ -850,7 +866,7 @@ private bool SetLayoutFromElement(object o, NLogXmlElement layoutElement)
// and is a Layout
if (typeof(Layout).IsAssignableFrom(targetPropertyInfo.PropertyType))
{
string layoutTypeName = layoutElement.GetOptionalAttribute("type", null);
string layoutTypeName = StripOptionalNamespacePrefix(layoutElement.GetOptionalAttribute("type", null));

// and 'type' attribute has been specified
if (layoutTypeName != null)
Expand Down Expand Up @@ -878,7 +894,7 @@ private void ConfigureObjectFromElement(object targetObject, NLogXmlElement elem

private Target WrapWithDefaultWrapper(Target t, NLogXmlElement defaultParameters)
{
string wrapperType = defaultParameters.GetRequiredAttribute("type");
string wrapperType = StripOptionalNamespacePrefix(defaultParameters.GetRequiredAttribute("type"));

Target wrapperTargetInstance = this.configurationItemFactory.Targets.CreateInstance(wrapperType);
WrapperTargetBase wtb = wrapperTargetInstance as WrapperTargetBase;
Expand Down
13 changes: 13 additions & 0 deletions tests/NLog.UnitTests/Config/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,18 @@ public void ExtensionTest4()
Assert.AreEqual("MyExtensionNamespace.WhenFooFilter", configuration.LoggingRules[0].Filters[0].GetType().FullName);
}

[Test]
public void CustomXmlNamespaceTest()
{
var configuration = CreateConfigurationFromString(@"
<nlog throwExceptions='true' xmlns:foo='http://bar'>
<targets>
<target name='d' type='foo:Debug' />
</targets>
</nlog>");

var d1Target = (DebugTarget)configuration.FindTargetByName("d");
Assert.IsNotNull(d1Target);
}
}
}

0 comments on commit 135261d

Please sign in to comment.