Trace.Flush 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
清除輸出緩衝區,並使緩衝資料寫入 Listeners。
public:
static void Flush();
[System.Diagnostics.Conditional("TRACE")]
public static void Flush();
[<System.Diagnostics.Conditional("TRACE")>]
static member Flush : unit -> unit
Public Shared Sub Flush ()
- 屬性
範例
下列範例會建立名為 TextWriterTraceListenermyTextListener。
myTextListener 使用 一個 StreamWriter 被呼叫 myOutputWriter 來寫入一個名為 TestFile.txt的檔案。 範例中建立檔案、串流與文字編入器,寫入一行文字到檔案,然後清除並關閉輸出。
// Specify /d:TRACE when compiling.
using System;
using System.IO;
using System.Diagnostics;
class Test
{
static void Main()
{
// Create a file for output named TestFile.txt.
using (FileStream myFileStream =
new FileStream("TestFile.txt", FileMode.Append))
{
// Create a new text writer using the output stream
// and add it to the trace listeners.
TextWriterTraceListener myTextListener =
new TextWriterTraceListener(myFileStream);
Trace.Listeners.Add(myTextListener);
// Write output to the file.
Trace.WriteLine("Test output");
// Flush and close the output stream.
Trace.Flush();
Trace.Close();
}
}
}
' Specify /d:TRACE=True when compiling.
Imports System.IO
Imports System.Diagnostics
Class Test
Shared Sub Main()
' Create a file for output named TestFile.txt.
Using myFileStream As New FileStream("TestFile.txt", FileMode.Append)
' Create a new text writer using the output stream
' and add it to the trace listeners.
Dim myTextListener As New TextWriterTraceListener(myFileStream)
Trace.Listeners.Add(myTextListener)
' Write output to the file.
Trace.WriteLine("Test output")
' Flush and close the output stream.
Trace.Flush()
Trace.Close()
End Using
End Sub
End Class
備註
除非你明確呼叫 Flush 或 Close,否則 flush 串流不會沖走其底層編碼器。 設定 AutoFlush 為 true 表示資料會從緩衝區沖洗到串流,但編碼器狀態不會被沖洗。 這讓編碼器能保留其狀態(部分字元),以便正確編碼下一個字元區塊。 此情境影響 UTF8 與 UTF7,因為某些字元只能在編碼器接收相鄰字元後再編碼。