TextWriterTraceListener Constructors

Definition

Initializes a new instance of the TextWriterTraceListener class.

Overloads

TextWriterTraceListener()

Initializes a new instance of the TextWriterTraceListener class with TextWriter as the output recipient.

TextWriterTraceListener(Stream)

Initializes a new instance of the TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output.

TextWriterTraceListener(TextWriter)

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

TextWriterTraceListener(String)

Initializes a new instance of the TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output.

TextWriterTraceListener(Stream, String)

Initializes a new instance of the TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output.

TextWriterTraceListener(TextWriter, String)

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

TextWriterTraceListener(String, String)

Initializes a new instance of the TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.

TextWriterTraceListener()

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

Initializes a new instance of the TextWriterTraceListener class with TextWriter as the output recipient.

C#
public TextWriterTraceListener();

Examples

The following example creates a TextWriterTraceListener using the TextWriterTraceListener() constructor. It sets the Writer property to console output, and then adds the TextWriterTraceListener to the TraceListenerCollection. It writes a message in two segments, and then closes the TextWriterTraceListener.

C#
public class Sample
{
    public static void Main(string[] args)
    {
        /* Create a text writer that writes to the console screen and add
         * it to the trace listeners */
        TextWriterTraceListener myWriter = new TextWriterTraceListener();
        myWriter.Writer = System.Console.Out;
        Trace.Listeners.Add(myWriter);

        // Write the output to the console screen.
        myWriter.Write("Write to the Console screen. ");
        myWriter.WriteLine("Again, write to console screen.");

        // Flush and close the output.
        myWriter.Flush();
        myWriter.Close();
    }
}

Remarks

This constructor uses the TextWriter stream as the recipient of the tracing or debugging output. Its Name property is initialized to an empty string ("", or String.Empty).

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(Stream)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

Initializes a new instance of the TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output.

C#
public TextWriterTraceListener(System.IO.Stream stream);

Parameters

stream
Stream

A Stream that represents the stream the TextWriterTraceListener writes to.

Exceptions

The stream is null.

Examples

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(Stream) constructor and adds it to the TraceListenerCollection. The example then writes two messages to this TextWriterTraceListener, and writes a message to all TraceListener objects in the TraceListenerCollection. Finally, it flushes and closes the TextWriterTraceListener.

C#
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic;

class TWTLConStreamMod
{

    // args(0) is the specification of the trace log file.
    public static void Main(string[] args)
    {

        // Verify that a parameter was entered.
        if (args.Length==0)
        {
            Console.WriteLine("Enter a trace file specification.");
        }
        else
        {
            // Create a stream object.
            FileStream traceStream;
            try
            {
                traceStream = new FileStream(args[0], FileMode.Append, FileAccess.Write);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error creating FileStream for trace file \"{0}\":" +
                    "\r\n{1}", args[0], ex.Message);
                return;
            }

            // Create a TextWriterTraceListener object that takes a stream.
            TextWriterTraceListener textListener;
            textListener = new TextWriterTraceListener(traceStream);
            Trace.Listeners.Add(textListener);

            // Write these messages only to this TextWriterTraceListener.
            textListener.WriteLine("This is trace listener named \""+ textListener.Name+"\"");
            textListener.WriteLine("Trace written through a stream to: " +
                "\r\n    \""+args[0]+"\"");

            // Write a message to all trace listeners.
            Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now));

            // Flush and close the output.
            Trace.Flush();
            textListener.Flush();
            textListener.Close();
        }
    }
}

Remarks

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(TextWriter)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

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

C#
public TextWriterTraceListener(System.IO.TextWriter writer);

Parameters

writer
TextWriter

A TextWriter that receives the output from the TextWriterTraceListener.

Exceptions

The writer is null.

Examples

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.

C#
#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();
    }
}

Remarks

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(String)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

Initializes a new instance of the TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output.

C#
public TextWriterTraceListener(string? fileName);
C#
public TextWriterTraceListener(string fileName);

Parameters

fileName
String

The name of the file the TextWriterTraceListener writes to.

Exceptions

The file is null.

Examples

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(String) constructor, then adds it to the TraceListenerCollection. The example writes two messages to this TextWriterTraceListener, then writes a message to all TraceListener objects in the TraceListenerCollection. Finally, it flushes and closes the TextWriterTraceListener.

C#
using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class TWTLConStringMod
{

    // args(0) is the specification of the trace log file.
    public static void Main(string[] args)
    {

        // Verify that a parameter was entered.
        if (args.Length==0)
        {
            Console.WriteLine("Enter a trace file specification.");
        }
        else
        {
            // Create a TextWriterTraceListener object that takes a
            // file specification.
            TextWriterTraceListener textListener;
            try
            {
                textListener = new TextWriterTraceListener(args[0]);
                Trace.Listeners.Add(textListener);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error creating TextWriterTraceListener for trace " +
                    "file \"{0}\":\r\n{1}", args[0], ex.Message);
                return;
            }

            // Write these messages only to the TextWriterTraceListener.
            textListener.WriteLine("This is trace listener named \""+textListener.Name+"\"");
            textListener.WriteLine("Trace written to a file: " +
                "\r\n    \""+args[0]+"\"");

            // Write a message to all trace listeners.
            Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now));

            // Flush and close the output.
            Trace.Flush();
            textListener.Flush();
            textListener.Close();
        }
    }
}

Remarks

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(Stream, String)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

Initializes a new instance of the TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output.

C#
public TextWriterTraceListener(System.IO.Stream stream, string? name);
C#
public TextWriterTraceListener(System.IO.Stream stream, string name);

Parameters

stream
Stream

A Stream that represents the stream the TextWriterTraceListener writes to.

name
String

The name of the new instance.

Exceptions

The stream is null.

Examples

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(Stream, String) constructor and adds it to the TraceListenerCollection. The example then writes two messages to this TextWriterTraceListener and writes a message to all TraceListener objects in the TraceListenerCollection. Finally, it flushes and closes the TextWriterTraceListener.

C#
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic;

class TWTLConStreamNameMod
{

    const string LISTENER_NAME = "myStreamListener";

    // args(0) is the specification of the trace log file.
    public static void Main(string[] args)
    {

        // Verify that a parameter was entered.
        if (args.Length==0)
        {
            Console.WriteLine("Enter a trace file specification.");
        }
        else
        {
            // Create a stream object.
            FileStream traceStream;
            try
            {
                traceStream = new FileStream(args[0], FileMode.Append, FileAccess.Write);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error creating FileStream for trace file \"{0}\":" +
                    "\r\n{1}", args[0], ex.Message);
                return;
            }

            // Create a TextWriterTraceListener object that takes a stream.
            TextWriterTraceListener textListener;
            textListener = new TextWriterTraceListener(traceStream, LISTENER_NAME);
            Trace.Listeners.Add(textListener);

            // Write these messages only to the TextWriterTraceListener.
            textListener.WriteLine("This is trace listener named \""+textListener.Name+"\"");
            textListener.WriteLine("Trace written through a stream to: " +
                "\r\n    \""+args[0]+"\"");

            // Write a message to all trace listeners.
            Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now));

            // Flush and close the output.
            Trace.Flush();
            textListener.Flush();
            textListener.Close();
        }
    }
}

Remarks

This constructor initializes the Name property to the name parameter or to an empty string (""), if the name parameter is null.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(TextWriter, String)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

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

C#
public TextWriterTraceListener(System.IO.TextWriter writer, string? name);
C#
public TextWriterTraceListener(System.IO.TextWriter writer, string name);

Parameters

writer
TextWriter

A TextWriter that receives the output from the TextWriterTraceListener.

name
String

The name of the new instance.

Exceptions

The writer is null.

Examples

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(TextWriter, String) 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 two messages to this TextWriterTraceListener, then writes a message to all TraceListener objects in the TraceListenerCollection. Finally, it flushes and closes the TextWriterTraceListener.

C#
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic;

class TWTLConWriterNameMod
{

    const string LISTENER_NAME = "myWriterListener";

    // args(0) is the specification of the trace log file.
    public static void Main(string[] args)
    {

        // Verify that a parameter was entered.
        if (args.Length==0)
        {
            Console.WriteLine("Enter a trace file specification.");
        }
        else
        {
            // Create a StreamWriter object that supports appending.
            StreamWriter traceWriter;
            try
            {
                traceWriter = new StreamWriter(args[0], true);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error creating StreamWriter for trace file \"{0}\":" +
                    "\r\n{1}", args[0], ex.Message);
                return;
            }

            // Create a TextWriterTraceListener that takes a StreamWriter.
            TextWriterTraceListener textListener;
            textListener = new TextWriterTraceListener(traceWriter, LISTENER_NAME);
            Trace.Listeners.Add(textListener);

            // Write these messages only to this TextWriterTraceListener.
            textListener.WriteLine("This is trace listener named \""+textListener.Name+"\"");
            textListener.WriteLine("Trace written through a stream to: " +
                "\r\n    \""+args[0]+"\"");

            // Write a message to all trace listeners.
            Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now));

            // Flush and close the output.
            Trace.Flush();
            textListener.Flush();
            textListener.Close();
        }
    }
}

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TextWriterTraceListener(String, String)

Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs
Source:
TextWriterTraceListener.cs

Initializes a new instance of the TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output.

C#
public TextWriterTraceListener(string? fileName, string? name);
C#
public TextWriterTraceListener(string fileName, string name);

Parameters

fileName
String

The name of the file the TextWriterTraceListener writes to.

name
String

The name of the new instance.

Exceptions

The stream is null.

Examples

The following code example creates a TextWriterTraceListener using the TextWriterTraceListener(String, String) constructor, then adds it to the TraceListenerCollection. The example writes two messages to this TextWriterTraceListener, then writes a message to all TraceListener objects in the TraceListenerCollection. Finally, it flushes and closes the TextWriterTraceListener.

C#
using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class TWTLConStringNameMod
{

    const string LISTENER_NAME = "myStringListener";

    // args(0) is the specification of the trace log file.
    public static void Main(string[] args)
    {

        // Verify that a parameter was entered.
        if (args.Length==0)
        {
            Console.WriteLine("Enter a trace file specification.");
        }
        else
        {
            // Create a TextWriterTraceListener object that takes a
            // file specification.
            TextWriterTraceListener textListener;
            try
            {
                textListener = new TextWriterTraceListener(args[0], LISTENER_NAME);
                Trace.Listeners.Add(textListener);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error creating TextWriterTraceListener for trace " +
                    "file \"{0}\":\r\n{1}", args[0], ex.Message);
                return;
            }

            // Write these messages only to this TextWriterTraceListener.
            textListener.WriteLine("This is trace listener named \""+textListener.Name+"\"");
            textListener.WriteLine("Trace written to a file: " +
                "\r\n    \""+args[0]+"\"");

            // Write a message to all trace listeners.
            Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now));

            // Flush and close the output.
            Trace.Flush();
            textListener.Flush();
            textListener.Close();
        }
    }
}

Remarks

This constructor initializes the Name property to the name parameter or to an empty string (""), if the name parameter is null.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1