TextWriterTraceListener 類別

定義

將追蹤或偵錯輸出導至 TextWriterStream,例如 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
衍生

範例

下列範例會實作 類別的 TextWriterTraceListener 實例,這個實例會使用 StreamWriter 呼叫 myOutputWriter 寫入名為 TestFile.txt的檔案。 首先,此範例會建立輸出的檔案。 然後它會為第一個文字寫入器建立 StreamWriter ,併為其指派輸出檔案,並將它新增至 Listeners。 然後,程式代碼會將一行文字輸出至檔案。 最後,此範例會排清輸出緩衝區。

執行此範例之後,您可以開啟 TestFile.txt 檔案以查看輸出。

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 屬性,以取得或設定接收追蹤或偵錯輸出的文字寫入器。

重要

此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。

這個類別也會提供 方法給 CloseWriter ,使其不再收到追蹤或偵錯輸出、 Flush 的輸出緩衝區 Writer,以及 Write 的訊息 Writer

您必須啟用追蹤或偵錯,才能使用追蹤接聽程式。 下列語法是編譯程式特有的。 如果您使用 C# 或 Visual Basic 以外的編譯程式,請參閱編譯程式的檔。

  • 若要在 C# 中啟用偵錯, /d:DEBUG 請在編譯程式程式程式代碼時將旗標新增至編譯程式命令行,或新增 #define DEBUG 至檔案頂端。 在 Visual Basic 中,將 /d:DEBUG=True 旗標新增至編譯程式命令行。

  • 若要在 C# 中啟用追蹤,請在 /d:TRACE 編譯程式程式程式代碼時將旗標新增至編譯程式命令行,或將 新增 #define TRACE 至檔案頂端。 在 Visual Basic 中,將 /d:TRACE=True 旗標新增至編譯程式命令行。

若要在 .NET Framework 應用程式中新增追蹤接聽程式,請編輯對應至應用程式名稱的組態檔。 在此檔案中,您可以新增接聽程式、設定其類型並設定其參數、移除接聽程式,或清除應用程式先前設定的所有接聽程式。 組態檔的格式應該像下列範例一樣。

<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 的前置詞。

建構函式

TextWriterTraceListener()

使用 TextWriterTraceListener 作為輸出接收者,初始化 TextWriter 類別的新執行個體。

TextWriterTraceListener(Stream)

使用資料流做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。

TextWriterTraceListener(Stream, String)

使用資料流做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。

TextWriterTraceListener(String)

使用檔案做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。

TextWriterTraceListener(String, String)

使用檔案做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。

TextWriterTraceListener(TextWriter)

使用指定的寫入器當做追蹤或偵錯之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。

TextWriterTraceListener(TextWriter, String)

使用指定的寫入器做為追蹤或偵錯之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。

屬性

Attributes

取得在應用程式組態檔中定義的自訂追蹤接聽程式屬性。

(繼承來源 TraceListener)
Filter

取得或設定追蹤接聽程式的追蹤篩選。

(繼承來源 TraceListener)
IndentLevel

取得或設定縮排層級。

(繼承來源 TraceListener)
IndentSize

取得或設定縮排的空格數目。

(繼承來源 TraceListener)
IsThreadSafe

取得值,指出追蹤接聽程式是否為安全執行緒。

(繼承來源 TraceListener)
Name

取得或設定這個 TraceListener 的名稱。

(繼承來源 TraceListener)
NeedIndent

取得或設定值,指出是否要縮排輸出。

(繼承來源 TraceListener)
TraceOutputOptions

取得或設定追蹤輸出選項。

(繼承來源 TraceListener)
Writer

取行或設定接收追蹤或偵錯之輸出的文字寫入器。

方法

Close()

關閉 Writer 使其不再接收追蹤或偵錯的輸出。

Close()

當在衍生類別中覆寫時,關閉輸出資料流,使它不再接收追蹤或偵錯輸出。

(繼承來源 TraceListener)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 TraceListener 所使用的所有資源。

(繼承來源 TraceListener)
Dispose(Boolean)

處置這個 TextWriterTraceListener 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Fail(String)

當您實作 TraceListener 類別時,發出錯誤訊息給您建立的接聽程式。

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

當您實做 TraceListener 類別時,發出錯誤訊息和詳細的錯誤訊息給您建立的接聽程式。

(繼承來源 TraceListener)
Flush()

清除 Writer 的輸出緩衝區。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetSupportedAttributes()

取得由追蹤接聽程式支援的自訂屬性。

(繼承來源 TraceListener)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。

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

寫入追蹤資訊、資料物件與事件資訊至特定接聽程式的輸出。

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

寫入追蹤資訊、資料物件的陣列與事件資訊至特定接聽程式的輸出。

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

寫入追蹤和事件資訊至特定接聽程式的輸出。

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

寫入追蹤資訊、訊息與事件資訊至特定接聽程式的輸出。

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

寫入追蹤資訊、格式化的物件陣列與事件資訊至特定接聽程式的輸出。

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

寫入追蹤資訊、訊息、相關活動身分識別與事件資訊至特定接聽程式的輸出。

(繼承來源 TraceListener)
Write(Object)

將物件的 ToString() 方法的值寫入當您實作 TraceListener 類別時所建立的接聽程式。

(繼承來源 TraceListener)
Write(Object, String)

將類別名稱和物件的 ToString() 方法的值寫入當您實作 TraceListener 類別時所建立的接聽程式。

(繼承來源 TraceListener)
Write(String)

將訊息寫入這個執行個體的 Writer

Write(String, String)

將分類名稱和訊息寫入當您實作 TraceListener 類別時所建立的接聽程式。

(繼承來源 TraceListener)
WriteIndent()

將縮排寫入當您實作這個類別時所建立的接聽程式,並且將 NeedIndent 屬性重設為 false

(繼承來源 TraceListener)
WriteLine(Object)

將物件的 ToString() 方法的值寫入當您實作 TraceListener 類別時所建立的接聽程式,後面跟隨著行結束字元。

(繼承來源 TraceListener)
WriteLine(Object, String)

將分類名稱和物件的 ToString() 方法的值寫入當您實作 TraceListener 類別時所建立的接聽程式,後面跟隨著行結束字元。

(繼承來源 TraceListener)
WriteLine(String)

將訊息寫入這個執行個體的 Writer,後面接著行結束字元。 預設行結束字元為後面接著換行符號 (\r\n) 的歸位字元。

WriteLine(String, String)

將分類名稱和訊息寫入當您實作 TraceListener 類別時所建立的接聽程式,後面跟隨著行結束字元。

(繼承來源 TraceListener)

適用於

另請參閱