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執行的範例,請DefaultTraceListener參閱TextWriterTraceListenerEventLogTraceListener類別。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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。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.

另請參閱