Debug.Close 方法

定義

清除輸出緩衝區,然後對每個 Listeners 呼叫 Close 方法。

C#
[System.Diagnostics.Conditional("DEBUG")]
public static void Close ();
屬性

範例

下列範例會 TextWriterTraceListener 建立名為 myTextListener的 。 myTextListener StreamWriter使用名為 myOutputWriter 的 ,寫入名為 的TestFile.txt檔案。 此範例會建立檔案、數據流和文字寫入器、將一行文字寫入檔案,然後排清並關閉輸出。

C#
// 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();
        }
    }
}

備註

當輸出移至檔案時,請使用這個方法,例如 。TextWriterTraceListener

除非您明確呼叫 FlushClose,否則清除數據流不會排清其基礎編碼器。 設定 AutoFlushtrue 表示數據會從緩衝區排清到數據流,但不會排清編碼器狀態。 這可讓編碼器保持其狀態 (部分字元) ,以便正確地編碼下一個字元區塊。 此案例會影響UTF8和UTF7,其中某些字元只能在編碼器收到相鄰字元或字元之後進行編碼。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱