Debug.Close 方法

定義

排清輸出緩衝區,然後在每個 Close上呼叫 Listeners 方法。

public:
 static void Close();
[System.Diagnostics.Conditional("DEBUG")]
public static void Close();
[<System.Diagnostics.Conditional("DEBUG")>]
static member Close : unit -> unit
Public Shared Sub Close ()
屬性

範例

下列範例會建立名為 TextWriterTraceListenermyTextListenermyTextListener 使用 一個 StreamWriter 被呼叫 myOutputWriter 來寫入一個名為 TestFile.txt的檔案。 範例中建立了檔案、串流和文字寫入器,先寫入一行文字到檔案,然後清除並關閉輸出。

// Specify /d:DEBUG when compiling.

using System;
using System.IO;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        // Create a new stream object for an output file named TestFile.txt.
        using (FileStream myFileStream =
            new FileStream("TestFile.txt", FileMode.Append))
        {
            // Add the stream object to the trace listeners.
            TextWriterTraceListener myTextListener =
                new TextWriterTraceListener(myFileStream);
            Debug.Listeners.Add(myTextListener);

            // Write output to the file.
            Debug.WriteLine("Test output");

            // Flush and close the output stream.
            Debug.Flush();
            Debug.Close();
        }
    }
}
' Specify /d:DEBUG=True when compiling.

Imports System.IO
Imports System.Diagnostics

Class Test
    
    Shared Sub Main()
    
        ' Create a new stream object for an output file named TestFile.txt.
        Using myFileStream As New FileStream("TestFile.txt", FileMode.Append)
        
            ' Add the stream object to the trace listeners. 
            Dim myTextListener As New TextWriterTraceListener(myFileStream)
            Debug.Listeners.Add(myTextListener)
            
            ' Write output to the file.
            Debug.WriteLine("Test output")
            
            ' Flush and close the output stream.
            Debug.Flush()
            Debug.Close()
        
        End Using
        
    End Sub

End Class

備註

當輸出輸出到檔案時,例如輸出到檔案,使用此方法。TextWriterTraceListener

除非你明確呼叫 FlushClose,否則 flush 串流不會沖走其底層編碼器。 設定 AutoFlushtrue 表示資料會從緩衝區沖洗到串流,但編碼器狀態不會被沖洗。 這讓編碼器能保留其狀態(部分字元),以便正確編碼下一個字元區塊。 此情境影響 UTF8 與 UTF7,因為某些字元只能在編碼器接收相鄰字元後再編碼。

適用於

另請參閱