Share via


<listeners> Element

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

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

<listeners>
</listeners>

Child Elements

Element Description
<add> Adds a listener to the Listeners collection.
<remove> Removes a listener from the Listeners collection.

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 SDK derive from the TraceListener class.

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, Version, Culture, PublicKeyToken" 
                 initializeData="MyListener.log"/>
            <add name="MyEventListener"
                 type="System.Diagnostics.EventLogTraceListener, Version, Culture, PublicKeyToken" 
                 initializeData="MyConfigEventLog"/>
         </listeners>
      </trace>
   </system.diagnostics>
</configuration>

Configuration File

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

See Also

Trace and Debug Settings Schema | TraceListener Class | DefaultTraceListener Class | TextWriterTraceListener Class | EventLogTraceListener Class