TextWriterTraceListener 類別

定義

將追蹤或偵錯輸出導至 TextWriterStream,例如 FileStreamDirects tracing or debugging output to a TextWriter or to a Stream, such as FileStream.

public ref class TextWriterTraceListener : System::Diagnostics::TraceListener
public class TextWriterTraceListener : System.Diagnostics.TraceListener
type TextWriterTraceListener = class
    inherit TraceListener
Public Class TextWriterTraceListener
Inherits TraceListener
繼承
TextWriterTraceListener
衍生

範例

下列範例會執行 TextWriterTraceListener 類別的實例,這個實例會使用名為 myOutputWriterStreamWriter 來寫入名為 TestFile.txt的檔案。The following example implements an instance of the TextWriterTraceListener class that uses a StreamWriter called myOutputWriter to write to a file named TestFile.txt. 首先,此範例會建立輸出的檔案。First the example creates a file for output. 然後,它會建立第一個文字寫入器的 StreamWriter,為它指派輸出檔,並將它新增至 ListenersThen it creates the StreamWriter for the first text writer, assigns it the output file, and adds it to the Listeners. 然後,程式碼會將一行文字輸出至檔案。Then, the code outputs one line of text to the file. 最後,此範例會排清輸出緩衝區。Finally, the example flushes the output buffer.

執行此範例之後,您可以開啟 TestFile.txt 檔案以查看輸出。After running this sample, you can open the TestFile.txt file to see the output.

void main()
{
   #if defined(TRACE)
   // Create a file for output named TestFile.txt.
   Stream^ myFile = File::Create( "TestFile.txt" );
   
   // Create a new text writer using the output stream and
   // add it to the trace listeners.
   TextWriterTraceListener^ myTextListener = 
      gcnew TextWriterTraceListener( myFile );
   Trace::Listeners->Add( myTextListener );
  
   // Write output to the file.
   Trace::Write( "Test output " );
  
   // Flush the output.
   Trace::Flush();
   Trace::Close();
   #endif
}
public class Sample
{

public static int Main(string[] args) {
    // Create a file for output named TestFile.txt.
    Stream myFile = File.Create("TestFile.txt");
 
    /* Create a new text writer using the output stream, and add it to
     * the trace listeners. */
    TextWriterTraceListener myTextListener = new 
       TextWriterTraceListener(myFile);
    Trace.Listeners.Add(myTextListener);
 
    // Write output to the file.
    Trace.Write("Test output ");

    // Flush the output.
    Trace.Flush(); 

    return 0;
 }
}
Public Class Sample
    
    Public Shared Sub Main()
        ' Create a file for output named TestFile.txt.
        Dim myFile As Stream = File.Create("TestFile.txt")
        
        ' Create a new text writer using the output stream, and add it to
        ' the trace listeners. 
        Dim myTextListener As New TextWriterTraceListener(myFile)
        Trace.Listeners.Add(myTextListener)

        
        ' Write output to the file.
        Trace.Write("Test output ")
        
        ' Flush the output.
        Trace.Flush() 

        System.Environment.ExitCode = 0
    End Sub

End Class

備註

TextWriterTraceListener 類別提供 Writer 屬性,可取得或設定接收追蹤或偵錯工具輸出的文字寫入器。The TextWriterTraceListener class provides the Writer property to get or set the text writer that receives the tracing or debugging output.

重要

此型別代表 IDisposable 介面。This type implements the IDisposable interface. 當您完成使用型別時,您應該直接或間接處置它。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

這個類別也會提供方法來 Close Writer,使其不再接收追蹤或偵錯工具輸出、Flush Writer的輸出緩衝區,以及將訊息 WriteWriterThis class also provides methods to Close the Writer so that it no longer receives tracing or debugging output, to Flush the output buffer for the Writer, and to Write a message to the Writer.

您必須啟用追蹤或偵錯工具,才能使用追蹤接聽程式。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 like 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>  

注意

如果嘗試寫入的檔案已在使用中或無法使用,檔案名會自動加上 GUID。If an attempt is made to write to a file that is in use or unavailable, the file name is automatically prefixed by a GUID.

建構函式

TextWriterTraceListener()

使用 TextWriterTraceListener 作為輸出接收者,初始化 TextWriter 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class with TextWriter as the output recipient.

TextWriterTraceListener(Stream)

使用資料流做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output.

TextWriterTraceListener(Stream, String)

使用資料流做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output.

TextWriterTraceListener(String)

使用檔案做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output.

TextWriterTraceListener(String, String)

使用檔案做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.

TextWriterTraceListener(TextWriter)

使用指定的寫入器當做追蹤或偵錯之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output.

TextWriterTraceListener(TextWriter, String)

使用指定的寫入器做為追蹤或偵錯之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。Initializes a new instance of the TextWriterTraceListener class with the specified name, using the specified writer as recipient of the tracing or debugging output.

屬性

Attributes

取得在應用程式組態檔中定義的自訂追蹤接聽程式屬性。Gets the custom trace listener attributes defined in the application configuration file.

(繼承來源 TraceListener)
Filter

取得或設定追蹤接聽程式的追蹤篩選。Gets or sets the trace filter for the trace listener.

(繼承來源 TraceListener)
IndentLevel

取得或設定縮排層級。Gets or sets the indent level.

(繼承來源 TraceListener)
IndentSize

取得或設定縮排的空格數目。Gets or sets the number of spaces in an indent.

(繼承來源 TraceListener)
IsThreadSafe

取得值,指出追蹤接聽程式是否為安全執行緒。Gets a value indicating whether the trace listener is thread safe.

(繼承來源 TraceListener)
Name

取得或設定這個 TraceListener 的名稱。Gets or sets a name for this TraceListener.

(繼承來源 TraceListener)
NeedIndent

取得或設定值,指出是否要縮排輸出。Gets or sets a value indicating whether to indent the output.

(繼承來源 TraceListener)
TraceOutputOptions

取得或設定追蹤輸出選項。Gets or sets the trace output options.

(繼承來源 TraceListener)
Writer

取行或設定接收追蹤或偵錯之輸出的文字寫入器。Gets or sets the text writer that receives the tracing or debugging output.

方法

Close()

關閉 Writer 使其不再接收追蹤或偵錯的輸出。Closes the Writer so that 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.

(繼承來源 TraceListener)
Dispose(Boolean)

處置這個 TextWriterTraceListener 物件。Disposes this TextWriterTraceListener object.

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.

(繼承來源 TraceListener)
Fail(String, String)

當您實做 TraceListener 類別時,發出錯誤訊息和詳細的錯誤訊息給您建立的接聽程式。Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class.

(繼承來源 TraceListener)
Flush()

清除 Writer 的輸出緩衝區。Flushes the output buffer for the Writer.

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.

(繼承來源 TraceListener)
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()

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

(繼承來源 TraceListener)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

寫入追蹤資訊、資料物件的陣列與事件資訊至特定接聽程式的輸出。Writes trace information, an array of data objects and event information to the listener specific output.

(繼承來源 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32)

寫入追蹤和事件資訊至特定接聽程式的輸出。Writes trace and event information to the listener specific output.

(繼承來源 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

寫入追蹤資訊、訊息與事件資訊至特定接聽程式的輸出。Writes trace information, a message, and event information to the listener specific output.

(繼承來源 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

寫入追蹤資訊、格式化的物件陣列與事件資訊至特定接聽程式的輸出。Writes trace information, a formatted array of objects and event information to the listener specific output.

(繼承來源 TraceListener)
TraceTransfer(TraceEventCache, String, Int32, String, Guid)

寫入追蹤資訊、訊息、相關活動身分識別與事件資訊至特定接聽程式的輸出。Writes trace information, a message, a related activity identity and event information to the listener specific output.

(繼承來源 TraceListener)
Write(Object)

將物件的 ToString() 方法的值寫入當您實作 TraceListener 類別時所建立的接聽程式。Writes the value of the object's ToString() method to the listener you create when you implement the TraceListener class.

(繼承來源 TraceListener)
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.

(繼承來源 TraceListener)
Write(String)

將訊息寫入這個執行個體的 WriterWrites a message to this instance's Writer.

Write(String, String)

將分類名稱和訊息寫入當您實作 TraceListener 類別時所建立的接聽程式。Writes a category name and a message to the listener you create when you implement the TraceListener class.

(繼承來源 TraceListener)
WriteIndent()

將縮排寫入當您實作這個類別時所建立的接聽程式,並且將 NeedIndent 屬性重設為 falseWrites the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.

(繼承來源 TraceListener)
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.

(繼承來源 TraceListener)
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.

(繼承來源 TraceListener)
WriteLine(String)

將訊息寫入這個執行個體的 Writer,後面接著行結束字元。Writes a message to this instance's Writer followed by a line terminator. 預設行結束字元為後面接著換行符號 (\r\n) 的歸位字元。The default line terminator is a carriage return followed by a line feed (\r\n).

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.

(繼承來源 TraceListener)

適用於

另請參閱