Share via


<sharedListeners> Element 

Contains listeners that any source or trace element can reference. These listeners do not receive any traces by default, and it is not possible to retrieve these listeners at run time. Listeners identified as shared listeners can be added to sources or traces by name.

<sharedListeners> 
  <add>...</add>
</sharedListeners>

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

None.

Child Elements

Element Description

<add> Element for <listeners> for <trace>

Adds a listener to the sharedListeners collection.

Parent Elements

Element Description

Configuration

The root element in every configuration file used by the common language runtime and .NET Framework applications.

system.diagnostics

Specifies the root element for the ASP.NET configuration section.

Remarks

Adding a listener to the shared listeners collection does not make it an active listener. It must still be added to a trace source or a trace by adding it to the Listeners collection for that trace element. The listener classes in the .NET Framework derive from the TraceListener class.

This element can be used in the machine configuration file (Machine.config) and the application configuration file.

Example

The following example shows how to use the <sharedListeners> element to add the listener console to the Listeners collection for both the TraceSource and Trace classes. The console trace listener writes trace information to the console through calls to either TraceSource or Trace.

<system.diagnostics>
  <sources>
    <source name="mySource" switchName="sourceSwitch value="Verbose" >
      <listeners>
        <add name="console" />
      </listeners>
    </source>
  </sources>  
  <sharedListeners>
    <add name="console" type="System.Diagnostics.ConsoleTraceListener" >
      <filter type="System.Diagnostics.EventTypeFilter" 
        initializeData="Warning" />
    </add>
  </sharedListeners>
  <trace>
    <listeners>
      <add name="console" />
    </listeners>
  </trace>
</system.diagnostics> 

See Also

Reference

Trace and Debug Settings Schema
TraceListener

Concepts

Trace Listeners