Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
TextWriterTraceListener Constructor (TextWriter)

TextWriterTraceListener Constructor (TextWriter)

Initializes a new instance of the TextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output.

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

'Declaration
Public Sub New ( _
	writer As TextWriter _
)
'Usage
Dim writer As TextWriter

Dim instance As New TextWriterTraceListener(writer)
public TextWriterTraceListener (
	TextWriter writer
)
public function TextWriterTraceListener (
	writer : TextWriter
)

Parameters

writer

A TextWriter that receives the output from the TextWriterTraceListener.

Exception typeCondition

ArgumentNullException

The writer is a null reference (Nothing in Visual Basic).

This constructor initializes the Name property to an empty string ("").

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(TextWriter) constructor. The example creates a StreamWriter, then references the StreamWriter when it creates the TextWriterTraceListener, which it then adds to the TraceListenerCollection. The example writes a message to all TraceListener objects in the TraceListenerCollection, then closes this TextWriterTraceListener.

#Const TRACE=True

Imports System
Imports System.IO
Imports System.Diagnostics

Public Class TextWriterTraceListenerSample
   
   Public Shared Sub Main()
      Dim myTextListener As TextWriterTraceListener = Nothing

      ' Create a file for output named TestFile.txt.
      Dim myFileName As String = "TestFile.txt"
      Dim myOutputWriter As 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()

   End Sub
End Class

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

ADD
Show:
© 2015 Microsoft