This documentation is archived and is not being maintained.

<add> Element for <listeners>

Adds a listener to the Listeners collection.


<add name="name" 
     type="trace listener class name, Version, Culture, PublicKeyToken"

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.


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.


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.

      <trace autoflush="true" indentsize="0">
            <add name="MyListener"
                 type="System.Diagnostics.TextWriterTraceListener, Version, Culture, PublicKeyToken" 
            <add name="MyEventListener"
                 type="System.Diagnostics.EventLogTraceListener, Version, Culture, PublicKeyToken" 

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