Trace.Flush 方法

定義

清除輸出緩衝區,讓緩衝的資料寫入 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 ()
屬性

範例

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

// Specify /DTRACE when compiling.

#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

void main()
{
     #if defined(TRACE)
    // Create a file for output named TestFile.txt.
    FileStream^ myFileStream = 
        gcnew FileStream( "TestFile.txt", FileMode::Append );
   
    // Create a new text writer using the output stream 
    // and add it to the trace listeners.
    TextWriterTraceListener^ myTextListener = 
        gcnew 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();
    #endif
}
// 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

備註

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

適用於

另請參閱