TextWriterTraceListener.Close Method

Closes the Writer so that it no longer receives tracing or debugging output.

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

public override void Close ()
public void Close ()
public override function Close ()
Not applicable.

If you want to send debugging or tracing output to a Writer after a call to this method, you must reinitialize the Writer.

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.

#define TRACE

using System;
using System.IO;
using System.Diagnostics;

public class TextWriterTraceListenerSample
{
    public static void Main() 
    {
        TextWriterTraceListener myTextListener = null;

        // Create a file for output named TestFile.txt.
        String myFileName = "TestFile.txt";
        StreamWriter myOutputWriter = new StreamWriter(myFileName, true);

        // Add a TextWriterTraceListener for the file.
        myTextListener = new TextWriterTraceListener(myOutputWriter);
        Trace.Listeners.Add(myTextListener);
      
 
        // Write trace output to all trace listeners.
        Trace.WriteLine(DateTime.Now.ToString() + " - Trace output");
 
        // Remove and close the file writer/trace listener.
        myTextListener.Flush();
        Trace.Listeners.Remove(myTextListener);
        myTextListener.Close();
    }
}

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");
            Application.Exit();
        }
    }

    // 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);
    Trace.get_Listeners().Add(myTextListener);

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

    // Flush and close the output stream.
    myOutputWriter.Flush();
    myOutputWriter.Close();
} //main

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: