Export (0) Print
Expand All

TextWriterTraceListener.Close Method

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

[Visual Basic]
Overrides Public Sub Close()
[C#]
public override void Close();
[C++]
public: void Close();
[JScript]
public override function Close();

Remarks

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

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Shared Sub Main()
    ' Create a file for output named TestFile.txt.
    Dim myFileName As String = "TestFile.txt"
    If Not File.Exists(myFileName) Then
        Dim myFile As Stream = File.Create(myFileName)
        ' If the file cannot be created, exit the application.
        If myFile Is Nothing Then
            Console.WriteLine("Could not create file")
            Application.Exit()
        End If
    End If
    
    ' Assign output file to the output stream.
    Dim myOutputWriter As StreamWriter
    myOutputWriter = File.AppendText(myFileName)
    
    ' Create a new text writer using the output stream and
    ' add it to the trace listeners. 
    Dim myTextListener As New TextWriterTraceListener(myOutputWriter)
    Trace.Listeners.Add(myTextListener)
    
    ' Write output to the file.
    Trace.WriteLine("Test output")
    
    ' Flush and close the output stream.
    myOutputWriter.Flush()
    myOutputWriter.Close()
End Sub


[C#] 
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.Listeners.Add(myTextListener);
 
    // Write output to the file.
    Trace.WriteLine("Test output");
 
    // Flush and close the output stream.
    myOutputWriter.Flush();
    myOutputWriter.Close();
 }
 

[C++] 
int main() {
    // Create a file for output named TestFile.txt.
    String* myFileName = S"TestFile.txt";
    if(!File::Exists(myFileName)) {
       Stream* myFile = File::Create(myFileName);
       // If the file cannot be created, exit the application.
       if(myFile == 0) {
          Console::WriteLine(S"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::Listeners->Add(myTextListener);
 
    // Write output to the file.
    Trace::WriteLine(S"Test output");
 
    // Flush and close the output stream.
    myOutputWriter->Flush();
    myOutputWriter->Close();
}
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TextWriterTraceListener Class | TextWriterTraceListener Members | System.Diagnostics Namespace | TraceListener | DefaultTraceListener | EventLogTraceListener | TextWriterTraceListener | Stream | TextWriter | Debug | Trace

Show:
© 2014 Microsoft