Trace.Flush Metoda

Definicja

Opróżnia bufor wyjściowy i powoduje zapisanie buforowanych danych w obiekcie 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 ()
Atrybuty

Przykłady

W poniższym przykładzie zostanie utworzona nazwana TextWriterTraceListenermyTextListener. myTextListener używa wywołania StreamWritermyOutputWriter do zapisu w pliku o nazwie TestFile.txt. Przykład tworzy plik, strumień i moduł zapisywania tekstu, zapisuje jeden wiersz tekstu w pliku, a następnie opróżnia i zamyka dane wyjściowe.

// 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

Uwagi

Opróżnienie strumienia nie spowoduje opróżnienia jego kodera bazowego, chyba że jawnie wywołasz Flush lub Close. Ustawienie AutoFlush oznacza true , że dane zostaną opróżnione z buforu do strumienia, ale stan kodera nie zostanie opróżniony. Dzięki temu koder może zachować stan (częściowe znaki), aby można było poprawnie zakodować następny blok znaków. Ten scenariusz ma wpływ na utF8 i UTF7, gdzie niektóre znaki mogą być zakodowane tylko po otrzymaniu sąsiadującego znaku lub znaków.

Dotyczy

Zobacz też