Skip to content

Custom attributes are lost when configuring TraceListeners via an XML configuration file #108428

@gvkries

Description

@gvkries

Description

When configuring trace listeners for System.Diagnostics.Trace using an XML configuration file (e.g., <Listeners> or <SharedListener> elements), it should be possible to add custom attributes for TraceListener implementations. These custom attributes should be added to the TraceListener.Attributes property. However, after deserializing the configuration file, the Attributes property remains empty.

The issue arises due to a missing assignment in the ListenerElement, specifically in the following code block:

https://github.com/dotnet/runtime/blob/e9fa899d9d39d0df0ca539b195ed0304d5ff6ccf/src/libraries/System.Configuration.ConfigurationManager/src/System/Diagnostics/ListenerElementsCollection.cs#L109C47-L109C58

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

Here, the Attributes property is never populated correctly, as a new StringDictionary is always created.

Reproduction Steps

  1. Create an app.config file that configures a DelimitedListTraceListener and specifies a custom delimiter using the Attributes property:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add name="delimited" type="System.Diagnostics.DelimitedListTraceListener" initializeData="test.log" delimiter=":" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>
  1. Apply the configuration using TraceConfiguration.Register().

  2. Check the Delimiter property on the listener:

var listener = (DelimitedListTraceListener)Trace.Listeners["delimited"];
Assert.Equal(":", listener.Delimiter); // This assertion fails; the default value ";" is returned.

Expected behavior

TraceListener.Attributes property must be populated with unknown attributes from the XML configuration.

Actual behavior

TraceListener.Attributes is always empty.

Regression?

Works in .NET Framework.

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.Configurationbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions