TraceListener 类

定义

为监视跟踪和调试输出的侦听器提供 abstract 基类。Provides the abstract base class for the listeners who monitor trace and debug output.

public ref class TraceListener abstract : MarshalByRefObject, IDisposable
public abstract class TraceListener : MarshalByRefObject, IDisposable
type TraceListener = class
    inherit MarshalByRefObject
    interface IDisposable
Public MustInherit Class TraceListener
Inherits MarshalByRefObject
Implements IDisposable
继承
TraceListener
派生
实现

注解

有关如何实现 TraceListener的示例,请参阅 DefaultTraceListenerTextWriterTraceListenerEventLogTraceListener 类。For examples of how to implement a TraceListener, see the DefaultTraceListener, TextWriterTraceListener and the EventLogTraceListener classes.

您必须启用跟踪或调试才能使用跟踪侦听器。You must enable tracing or debugging to use a trace listener. 以下语法是编译器特定的。The following syntax is compiler specific. 如果使用C#或 Visual Basic 以外的编译器,请参阅编译器的文档。If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • 若要在中C#启用调试,请在编译代码时将 /d:DEBUG 标志添加到编译器命令行,也可以将 #define DEBUG 添加到文件的顶部。To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. 在 Visual Basic 中,将 /d:DEBUG=True 标志添加到编译器命令行。In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

  • 若要启用跟踪C#,请在编译代码时将 /d:TRACE 标志添加到编译器命令行,或将 #define TRACE 添加到文件的顶部。To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. 在 Visual Basic 中,将 /d:TRACE=True 标志添加到编译器命令行。In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

若要添加跟踪侦听器,请编辑与应用程序名称对应的配置文件。To add a trace listener, edit the configuration file that corresponds to the name of your application. 在此文件中,您可以添加一个侦听器,设置其类型并设置其参数,删除侦听器,或者清除之前由应用程序设置的所有侦听器。Within this file, you can add a listener, set its type and set its parameter, remove a listener, or clear all the listeners previously set by the application. 配置文件的格式应与下面的示例类似。The configuration file should be formatted similar to the following example.

<configuration>  
  <system.diagnostics>  
    <trace autoflush="false" indentsize="4">  
      <listeners>  
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />  
        <remove name="Default" />  
      </listeners>  
    </trace>  
  </system.diagnostics>  
</configuration>  

有关使用配置文件进行跟踪的详细信息,请参阅<>For more information on using configuration files for tracing, see <system.diagnostics>.

实施者说明

从此类继承以实现 DebugTrace 类的自定义侦听器。Inherit from this class to implement a custom listener for the Debug and Trace classes. 至少必须实现 Write(String)WriteLine(String) 方法。At a minimum, you must implement the Write(String) and WriteLine(String) methods. 此外,还可以实现 Fail(String)Close()Flush() 方法。Additionally, you can implement the Fail(String), Close() and Flush() methods.

构造函数

TraceListener()

初始化 TraceListener 类的新实例。Initializes a new instance of the TraceListener class.

TraceListener(String)

用指定的名称作为侦听器来初始化 TraceListener 类的新实例。Initializes a new instance of the TraceListener class using the specified name as the listener.

属性

Attributes

获取应用程序配置文件中定义的自定义跟踪侦听器特性。Gets the custom trace listener attributes defined in the application configuration file.

Filter

获取或设置跟踪侦听器的跟踪筛选器。Gets or sets the trace filter for the trace listener.

IndentLevel

获取或设置缩进级别。Gets or sets the indent level.

IndentSize

获取或设置缩进的空格数。Gets or sets the number of spaces in an indent.

IsThreadSafe

获取一个值,该值指示跟踪侦听器是否是线程安全的。Gets a value indicating whether the trace listener is thread safe.

Name

获取或设置此 TraceListener 的名称。Gets or sets a name for this TraceListener.

NeedIndent

获取或设置一个值,该值指示是否缩进输出。Gets or sets a value indicating whether to indent the output.

TraceOutputOptions

获取或设置跟踪输出选项。Gets or sets the trace output options.

方法

Close()

在派生类中被重写时,关闭输出流以使它不再接收跟踪或调试输出。When overridden in a derived class, closes the output stream so it no longer receives tracing or debugging output.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放 TraceListener 使用的所有资源。Releases all resources used by the TraceListener.

Dispose(Boolean)

释放 TraceListener 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the TraceListener and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Fail(String)

实现 TraceListener 类时,向所创建的侦听器发出错误信息。Emits an error message to the listener you create when you implement the TraceListener class.

Fail(String, String)

实现 TraceListener 类时,向所创建的侦听器发出错误信息和详细错误信息。Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class.

Flush()

在派生类中被重写时,刷新输出缓冲区。When overridden in a derived class, flushes the output buffer.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetSupportedAttributes()

获取跟踪侦听器支持的自定义特性。Gets the custom attributes supported by the trace listener.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

向特定于侦听器的输出中写入跟踪信息、数据对象和事件信息。Writes trace information, a data object and event information to the listener specific output.

TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

向特定于侦听器的输出中写入跟踪信息、数据对象的数组和事件信息。Writes trace information, an array of data objects and event information to the listener specific output.

TraceEvent(TraceEventCache, String, TraceEventType, Int32)

向特定于侦听器的输出写入跟踪和事件信息。Writes trace and event information to the listener specific output.

TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

向特定于侦听器的输出中写入跟踪信息、消息和事件信息。Writes trace information, a message, and event information to the listener specific output.

TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

向特定于侦听器的输出中写入跟踪信息、格式化对象数组和事件信息。Writes trace information, a formatted array of objects and event information to the listener specific output.

TraceTransfer(TraceEventCache, String, Int32, String, Guid)

向侦听器特定的输出中写入跟踪信息、消息、相关活动标识和事件信息。Writes trace information, a message, a related activity identity and event information to the listener specific output.

Write(Object)

实现 ToString() 类时,向所创建的侦听器写入对象的 TraceListener 方法值。Writes the value of the object's ToString() method to the listener you create when you implement the TraceListener class.

Write(Object, String)

实现 ToString() 类时,向所创建的侦听器写入类别名称和对象的 TraceListener 方法值。Writes a category name and the value of the object's ToString() method to the listener you create when you implement the TraceListener class.

Write(String)

在派生类中被重写时,向在该派生类中所创建的侦听器写入指定消息。When overridden in a derived class, writes the specified message to the listener you create in the derived class.

Write(String, String)

实现 TraceListener 类时,向所创建的侦听器写入类别名称和消息。Writes a category name and a message to the listener you create when you implement the TraceListener class.

WriteIndent()

实现此类时,向所创建的侦听器写入缩进,并将 NeedIndent 属性重置为 falseWrites the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.

WriteLine(Object)

实现 ToString() 类时,向所创建的侦听器写入对象的 TraceListener 方法值,后跟行结束符。Writes the value of the object's ToString() method to the listener you create when you implement the TraceListener class, followed by a line terminator.

WriteLine(Object, String)

实现 ToString() 类时,向所创建的侦听器写入类别名称和对象的 TraceListener 方法值,后跟行结束符。Writes a category name and the value of the object's ToString() method to the listener you create when you implement the TraceListener class, followed by a line terminator.

WriteLine(String)

在派生类中被重写时,向在该派生类中所创建的侦听器写入消息,后跟行结束符。When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.

WriteLine(String, String)

实现 TraceListener 类时,向所创建的侦听器写入类别名称和消息,后跟行结束符。Writes a category name and a message to the listener you create when you implement the TraceListener class, followed by a line terminator.

适用于

线程安全性

此类型是线程安全的。This type is thread safe.

另请参阅