Permalink
Browse files

BUG 6491: Creating target w/ xmlns fails

  • Loading branch information...
jkowalski committed Nov 1, 2011
1 parent 853e37a commit 135261d5895b03d062709fe62e727f5923274b0c
Showing with 35 additions and 6 deletions.
  1. +22 −6 src/NLog/Config/XmlLoggingConfiguration.cs
  2. +13 −0 tests/NLog.UnitTests/Config/ExtensionTests.cs
@@ -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)
{
@@ -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))
{
@@ -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)
@@ -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)
@@ -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);
@@ -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)
@@ -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;
@@ -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);
+ }
}
}

1 comment on commit 135261d

haf commented on 135261d Nov 1, 2011

Smoooth. Thanks for fixing!

Please sign in to comment.