Share via


<listeners> Element for <trace>

Specifies a listener that collects, stores, and routes messages. Listeners direct the tracing output to an appropriate target.

<configuration> Element
  <system.diagnostics> Element
    <trace> Element
      <listeners> Element for <trace>

<listeners> 
  <add>...</add>
  <clear/>
  <remove ... />
</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.

<clear> Element for <listeners> for <trace>

Clears the Listeners collection for trace.

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

Removes a listener from the Listeners 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.

trace

Contains listeners that collect, store, and route tracing messages.

Remarks

The Debug and Trace classes share the same Listeners collection. If you add a listener object to the collection in one of these classes, the other class uses the same listener. The listener classes shipped with the .NET Framework derive from the TraceListener class.

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 the listeners MyListener and MyEventListener to the Listeners collection. MyListener creates a file called MyListener.log and writes the output to the file. MyEventListener creates an entry in the event log.

<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="0">
      <listeners>
        <add name="myListener" 
          type="System.Diagnostics.TextWriterTraceListener, 
            system, version=1.0.3300.0, Culture=neutral, 
            PublicKeyToken=b77a5c561934e089" 
          initializeData="c:\myListener.log" />
        <add name="MyEventListener"
          type="System.Diagnostics.EventLogTraceListener, 
            system, version=1.0.3300.0, Culture=neutral, 
            PublicKeyToken=b77a5c561934e089"
          initializeData="MyConfigEventLog"/>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

See Also

Reference

Trace and Debug Settings Schema

TraceListener