<add> Element for <listeners>

Adds a listener to the Listeners collection.

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

<add name="name" 
     type="trace listener class name, Version, Culture, PublicKeyToken"
     initializeData="data"/>

Required Attributes

Attribute Description
type Specifies the type of the listener. You must use a string that meets the requirements specified in Specifying Fully Qualified Type Names.

Optional Attributes

Attribute Description
initializeData The string passed to the constructor for the specified class.
name Specifies the name of the listener.

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.

The value for the initializeData attribute depends on the type of listener you create. Not all trace listeners require you to specify initializeData. The following table shows the trace listeners that ship with the .NET Framework SDK and describes the value of their initializeData attributes.

Trace listener class initializeData attribute value
System.Diagnostics.EventLogTraceListener The name of the event log trace listener.
System.Diagnostics.TextWriterTraceListener The name of the file that the TextWriterTraceListener writes to.

Example

The following example shows how to use <add> elements 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 | Trace Class | Debug Class | EventLogTraceListener Class | TextWriterTraceListener