Partager via


<add> Element for <listeners> for <trace> 

Adds a listener to the Listeners collection.

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

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute Description

type

Required attribute.

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

initializeData

Optional attribute.

The string passed to the constructor for the specified class.

name

Optional attribute.

Specifies the name of the listener.

Child Elements

None.

Parent Elements

Element Description

configuration

The root element in every configuration file used by the common language runtime and .NET Framework applications.

listeners

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

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

If you do not specify the name attribute of the trace listener, the Name of the trace listener defaults to an empty string (""). If your application has only one listener, you can add it without specifying a name, and remove it by specifying an empty string for the name. However, if your application has more than one listener, you should specify unique names for each trace listener, which allows you to identify and manage individual trace listeners within the Listeners and Listeners collections.

Note

Adding more than one trace listener of the same type and with the same name results in only one trace listener of that type and name being added to the Listeners collection. However you can programmatically add multiple identical listeners to the Listeners collection.

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 and describes the value of their initializeData attributes.

Trace listener class initializeData attribute value

System.Diagnostics.ConsoleTraceListener

The useErrorStream value for the ConsoleTraceListener constructor. Set the initializeData attribute to "true" to write trace and debug output to System.Console.Error; "false" to write to System.Console.Out.

System.Diagnostics.DelimitedListTraceListener

The name of the file the DelimitedListTraceListener writes to.

System.Diagnostics.EventLogTraceListener

The name of the name of an existing event log source.

System.Diagnostics.TextWriterTraceListener

The name of the file that the TextWriterTraceListener writes to.

System.Diagnostics.XmlWriterTraceListener

The name of the file that the XmlWriterTraceListener 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, 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"/>
            <add name="configConsoleListener"
                 type="System.Diagnostics.ConsoleTraceListener, system, version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
         </listeners>
      </trace>
   </system.diagnostics>
</configuration>

See Also

Reference

Trace and Debug Settings Schema
Trace Class
Debug Class
EventLogTraceListener Class
TextWriterTraceListener
ConsoleTraceListener

Concepts

Trace Listeners