Skip to content

Commit

Permalink
Fix the ListenerElement.Attributes property (#108936)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter authored Oct 21, 2024
1 parent c9a4d87 commit 8b1d30a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ListenerElement(bool allowReferences) : base(typeof(TraceListener))
_properties.Add(s_propOutputOpts);
}

public StringDictionary Attributes => _attributes ?? new StringDictionary();
public StringDictionary Attributes => _attributes ??= new StringDictionary();

[ConfigurationProperty("filter")]
public FilterElement Filter => (FilterElement)this[s_propFilter];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;
using Xunit;

namespace System.ConfigurationTests
Expand Down Expand Up @@ -66,5 +68,42 @@ public void IsKey()

Assert.Equal(ConfigurationPropertyOptions.None, attribute.Options);
}

public static string AttributeConfiguration =
@"<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<system.diagnostics>
<trace>
<listeners>
<add name=""delimited"" type=""System.Diagnostics.DelimitedListTraceListener"" delimiter="":"" />
</listeners>
</trace>
</system.diagnostics>
</configuration>";

[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "ListenerElement uses HashTable not StringDictionary")]
public void CustomAttributeIsPresent()
{
using (var temp = new TempConfig(AttributeConfiguration))
{
ConfigurationSection section = ConfigurationManager.OpenExeConfiguration(temp.ExePath).GetSection("system.diagnostics");
ConfigurationElementCollection listeners = (ConfigurationElementCollection)GetPropertyValue(GetPropertyValue(section, "Trace"), "Listeners");
Assert.Equal(2, listeners.Count);

ConfigurationElement[] items = new ConfigurationElement[2];
listeners.CopyTo(items, 0);
Assert.Equal("delimited", GetPropertyValue(items[1], "Name"));
StringDictionary attributes = (StringDictionary)GetPropertyValue(items[1], "Attributes");
Assert.Equal(1, attributes.Count);

// Verify the attribute is present.
Assert.Equal(":", attributes["delimiter"]);

static object GetPropertyValue(object obj, string propertyName) => obj.GetType().
GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance).
GetValue(obj);
}
}
}
}

0 comments on commit 8b1d30a

Please sign in to comment.