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

指定用于收集、存储和路由消息的侦听器。Specifies a listener that collects, stores, and routes messages. 侦听器将跟踪输出定向到适当的目标。Listeners direct the tracing output to an appropriate target.

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

语法Syntax

<listeners>
  <add>...</add>  
  <clear/>  
  <remove ... />  
</listeners>  

特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

无。None.

子元素Child Elements

元素Element 描述Description
<add> 将侦听器添加到 Listeners 集合中。Adds a listener to the Listeners collection.
<clear> 清除跟踪的 Listeners 集合。Clears the Listeners collection for trace.
<remove> 从集合中移除侦听器 ListenersRemoves a listener from the Listeners collection.

父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。The root element in every configuration file used by the common language runtime and .NET Framework applications.
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. 随 .NET Framework 附带的侦听器类派生自 TraceListener 类。The listener classes shipped with the .NET Framework derive from the TraceListener class.

配置文件Configuration File

此元素可在计算机配置文件中使用 ( # A0) 和应用程序配置文件。This element can be used in the machine configuration file (Machine.config) and the application configuration file.

示例Example

下面的示例演示如何使用 <listeners> 元素将侦听器 MyListener 和添加 MyEventListener侦听器 集合。The following example shows how to use the <listeners> element 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"/>  
      </listeners>  
    </trace>  
  </system.diagnostics>  
</configuration>  

请参阅See also