<add>的元素 <listeners><trace><add> Element for <listeners> for <trace>

侦听器 集合添加侦听器。Adds a listener to the Listeners collection.

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

语法Syntax

<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
typetype 必需的特性。Required attribute.

指定侦听器的类型。Specifies the type of the listener. 必须使用满足指定 完全限定的类型名称中指定的要求的字符串。You must use a string that meets the requirements specified in Specifying Fully Qualified Type Names.
initializeDatainitializeData 可选特性。Optional attribute.

传递到指定类的构造函数的字符串。The string passed to the constructor for the specified class.
namename 可选特性。Optional attribute.

指定侦听器的名称。Specifies the name of the listener.

子元素Child Elements

元素Element 描述Description
<filter> 将筛选器添加到跟踪的集合中的侦听器 ListenersAdds a filter to a listener in the Listeners collection for a trace.

父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。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 为 ASP.NET 配置节指定根元素。Specifies the root element for the ASP.NET configuration section.
trace 包含用于收集、存储和路由跟踪消息的侦听器。Contains listeners that collect, store, and route tracing messages.

备注Remarks

DebugTrace 类共享相同的侦听器集合。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. 侦听器类派生自 TraceListenerThe listener classes derive from the TraceListener.

如果未指定 name 跟踪侦听器的属性,则 Name 跟踪侦听器的默认值为空字符串 ( "" ) 。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. 但是,如果应用程序有多个侦听器,则应为每个跟踪侦听器指定唯一的名称,以便在和集合中标识和管理单个跟踪侦听器 Listeners ListenersHowever, 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.

备注

添加具有相同类型且具有相同名称的多个跟踪侦听器会导致只将该类型和名称的一个跟踪侦听器添加到 Listeners 集合中。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. 但是,可以通过编程方式将多个相同的侦听器添加到 Listeners 集合中。However, you can programmatically add multiple identical listeners to the Listeners collection.

InitializeData属性的值取决于所创建的侦听器的类型。The value for the initializeData attribute depends on the type of listener you create. 并非所有跟踪侦听器都需要指定 initializeDataNot all trace listeners require that you specify initializeData.

备注

使用 initializeData 属性时,可能会收到编译器警告 "未声明 ' initializeData ' 特性"。When you use the initializeData attribute, you may get the compiler warning "The 'initializeData' attribute is not declared." 之所以出现此警告,是因为配置设置是针对抽象基类验证的 TraceListener ,后者不识别 initializeData 属性。This warning occurs because the configuration settings are validated against the abstract base class TraceListener, which does not recognize the initializeData attribute. 通常,对于具有采用参数的构造函数的跟踪侦听器实现,你可以忽略此警告。Typically, you can ignore this warning for trace listener implementations that have a constructor that takes a parameter.

下表显示了 .NET Framework 附带的跟踪侦听器,并说明了其 initializeData 属性的值。The following table shows the trace listeners that are included with the .NET Framework and describes the value of their initializeData attributes.

跟踪侦听器类Trace listener class initializeData 特性值initializeData attribute value
System.Diagnostics.ConsoleTraceListener useErrorStream构造函数的值 ConsoleTraceListenerThe useErrorStream value for the ConsoleTraceListener constructor. initializeData 属性设置为 " true " 可将跟踪和调试输出写入 Console.Error ;false要写入的 "" Console.OutSet the initializeData attribute to "true" to write trace and debug output to Console.Error; "false" to write to Console.Out.
System.Diagnostics.DelimitedListTraceListener 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.EventSchemaTraceListener 写入的文件的名称 EventSchemaTraceListenerThe name of the file that the EventSchemaTraceListener writes to.
System.Diagnostics.TextWriterTraceListener 写入的文件的名称 TextWriterTraceListenerThe name of the file that the TextWriterTraceListener writes to.
System.Diagnostics.XmlWriterTraceListener 写入的文件的名称 XmlWriterTraceListenerThe name of the file that the XmlWriterTraceListener writes to.

示例Example

下面的示例演示如何使用 <add> 元素将侦听器 MyListenerMyEventListener 侦听器集合添加到 侦听器The following example shows how to use <add> elements to add the listeners MyListener and MyEventListener to the Listeners collection. MyListener 创建一个名为的文件 MyListener.log ,并将输出写入文件。MyListener creates a file called MyListener.log and writes the output to the file. MyEventListener 在事件日志中创建一个条目。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