Share via


<listeners> Element for <source>

Adds or removes listeners in the Listeners collection for a TraceSource. A listener directs the tracing output to an appropriate target, such as a log, window, or text file.

<listeners> 
  <add>...</add>
  <remove ... />
  <clear/>
</listeners>

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 Listeners collection.

<remove> Element for <listeners> for <trace>

Removes a listener from the Listeners collection.

<clear> Element for <listeners> for <source>

Clears the Listeners collection for a trace source.

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 trace listeners that collect, store, and route messages and the level where a trace switch is set.

sources

Contains trace sources that initiate tracing messages.

source

Specifies a trace source that initiates tracing messages.

Remarks

Configuration File

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 <listeners> element to add a console trace listener to the mySource source and to remove the default trace listener.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="mySource" switchName="sourceSwitch" 
        switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <add name="console" 
            type="System.Diagnostics.ConsoleTraceListener">
            <filter type="System.Diagnostics.EventTypeFilter" 
              initializeData="Error"/>
          </add>
          <remove name="Default"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="sourceSwitch" value="Warning"/>
    </switches>
  </system.diagnostics>
</configuration>

See Also

Reference

Trace and Debug Settings Schema
TraceListener

Concepts

Trace Listeners