Flush Method
Collapse the table of content
Expand the table of content

TextWriterTraceListener.Flush Method

Flushes the output buffer for the Writer.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

virtual void Flush () override
public void Flush ()
public override function Flush ()

The following example implements a TextWriterTraceListener named myTextListener, which uses a StreamWriter called myOutputWriter to write to a file named TestFile.txt. The example creates the file, stream, and text writer, writes one line of text to the file, and then flushes and closes the stream.

#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
int main()
   TextWriterTraceListener^ myTextListener = nullptr;
   // Create a file for output named TestFile.txt.
   String^ myFileName = "TestFile.txt";
   StreamWriter^ myOutputWriter = gcnew StreamWriter( myFileName,true );
   // Add a TextWriterTraceListener for the file.
   if ( myOutputWriter )
      myTextListener = gcnew TextWriterTraceListener( myOutputWriter );
      Trace::Listeners->Add( myTextListener );

   // Write trace output to all trace listeners.
   Trace::WriteLine( String::Concat( DateTime::Now.ToString(), " - Trace output" ) );
   if ( myTextListener )
      // Remove and close the file writer/trace listener.
      Trace::Listeners->Remove( myTextListener );

public static void main(String[] args)
    // Create a file for output named TestFile.txt.
    String myFileName = "TestFile.txt";
    if (!(File.Exists(myFileName))) {
        Stream myFile = File.Create(myFileName);

        // If the file cannot be created, exit the application.
        if (myFile == null) {
            Console.WriteLine("Could not create file");

    // Assign output file to the output stream.
    StreamWriter myOutputWriter;
    myOutputWriter = File.AppendText(myFileName);

    /* Create a new text writer using the output stream and 
     * add it to the trace listeners.
    TextWriterTraceListener myTextListener = 
        new TextWriterTraceListener(myOutputWriter);

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

    // Flush and close the output stream.
} //main

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft