TextWriterTraceListener 构造函数

定义

初始化 TextWriterTraceListener 类的新实例。

重载

TextWriterTraceListener()

TextWriter 作为输出接收者来初始化 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(Stream)

用流作为调试和跟踪输出的接收者来初始化 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(TextWriter)

用指定的编写器作为跟踪或调试输出的接收者来初始化 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(String)

用文件作为调试和跟踪输出的接收者来初始化 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(Stream, String)

用流作为调试和跟踪输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(TextWriter, String)

用指定的编写器作为跟踪或调试输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

TextWriterTraceListener(String, String)

用文件作为调试和跟踪输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

TextWriterTraceListener()

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

TextWriter 作为输出接收者来初始化 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener();
public TextWriterTraceListener ();
Public Sub New ()

示例

以下示例使用 TextWriterTraceListener() 构造函数创建 TextWriterTraceListener 。 它将 属性设置为Writer控制台输出,然后将 添加到 TextWriterTraceListenerTraceListenerCollection 它以两个段写入消息,然后关闭 TextWriterTraceListener

void main()
{
   #if defined(TRACE)
   // Create a text writer that writes to the console screen and add
   // it to the trace listeners.
   TextWriterTraceListener^ myWriter = gcnew 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();
   #endif
}
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();
    }
}
Public Class Sample
    
    Public Shared Sub Main()
        ' Create a text writer that writes to the console screen and add
        ' it to the trace listeners 
        Dim myWriter As 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()
    End Sub

End Class

注解

此构造函数使用 TextWriter 流作为跟踪或调试输出的接收方。 其 Name 属性初始化为空字符串 (“”,或 String.Empty) 。

另请参阅

适用于

TextWriterTraceListener(Stream)

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

用流作为调试和跟踪输出的接收者来初始化 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::IO::Stream ^ stream);
public TextWriterTraceListener (System.IO.Stream stream);
new System.Diagnostics.TextWriterTraceListener : System.IO.Stream -> System.Diagnostics.TextWriterTraceListener
Public Sub New (stream As Stream)

参数

stream
Stream

表示 Stream 写入的流的 TextWriterTraceListener

例外

此流为 null

示例

下面的代码示例使用 TextWriterTraceListener(Stream) 构造函数创建 TextWriterTraceListener ,并将其添加到 。TraceListenerCollection 然后,该示例将两条消息写入此 TextWriterTraceListener,并将一条消息写入 TraceListener 中的所有 TraceListenerCollection对象。 最后,它会刷新并关闭 TextWriterTraceListener

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();
        }
    }
}
Imports System.Diagnostics
Imports System.IO

Module TWTLConStreamMod

    ' args(0) is the specification of the trace log file.
    Sub Main(ByVal args() As String)

        ' Verify that a parameter was entered.
        If args.Length = 0 Then
            Console.WriteLine("Enter a trace file specification.")

        Else
            ' Create a stream object.
            Dim traceStream As FileStream
            Try
                traceStream = New FileStream( _
                    args(0), FileMode.Append, FileAccess.Write)
            Catch ex As Exception
                Console.WriteLine( _
                    "Error creating FileStream for trace file ""{0}"":" & _
                    vbCrLf & "{1}", args(0), ex.Message)
                Return
            End Try

            ' Create a TextWriterTraceListener object that takes a stream.
            Dim textListener As TextWriterTraceListener
            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: " & _
                vbCrLf & "    """ & args(0) & """")

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

            ' Flush and close the output.
            Trace.Flush()
            textListener.Flush()
            textListener.Close()
        End If
    End Sub
End Module

注解

此构造函数将 Name 属性初始化为空字符串 (“”) 。

另请参阅

适用于

TextWriterTraceListener(TextWriter)

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

用指定的编写器作为跟踪或调试输出的接收者来初始化 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::IO::TextWriter ^ writer);
public TextWriterTraceListener (System.IO.TextWriter writer);
new System.Diagnostics.TextWriterTraceListener : System.IO.TextWriter -> System.Diagnostics.TextWriterTraceListener
Public Sub New (writer As TextWriter)

参数

writer
TextWriter

接收来自 TextWriter 的输出的 TextWriterTraceListener

例外

编写器为 null

示例

下面的代码示例使用 TextWriterTraceListener(TextWriter) 构造函数创建 TextWriterTraceListener 。 该示例创建 一个 StreamWriter,然后在创建 TextWriterTraceListener时引用 StreamWriter ,然后将其添加到 。TraceListenerCollection 该示例将消息 TraceListener 写入 中的所有 TraceListenerCollection对象,然后关闭此 TextWriterTraceListener

#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

void main()
{
   #if defined(TRACE)
   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.
      myTextListener->Flush();
      Trace::Listeners->Remove( myTextListener );
      myTextListener->Close();
   }
   #endif
}
#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();
    }
}
#Const TRACE=True

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

注解

此构造函数将 Name 属性初始化为空字符串 (“”) 。

另请参阅

适用于

TextWriterTraceListener(String)

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

用文件作为调试和跟踪输出的接收者来初始化 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::String ^ fileName);
public TextWriterTraceListener (string? fileName);
public TextWriterTraceListener (string fileName);
new System.Diagnostics.TextWriterTraceListener : string -> System.Diagnostics.TextWriterTraceListener
Public Sub New (fileName As String)

参数

fileName
String

TextWriterTraceListener 写入的文件名。

例外

该文件为 null

示例

下面的代码示例使用 TextWriterTraceListener(String) 构造函数创建 TextWriterTraceListener ,然后将其添加到 。TraceListenerCollection 该示例将两条消息写入此 TextWriterTraceListener,然后将一条消息写入 TraceListener 中的所有 TraceListenerCollection对象。 最后,它会刷新并关闭 TextWriterTraceListener

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();
        }
    }
}
Imports System.Diagnostics

Module TWTLConStringMod

    ' args(0) is the specification of the trace log file.
    Sub Main(ByVal args() As String)

        ' Verify that a parameter was entered.
        If args.Length = 0 Then
            Console.WriteLine("Enter a trace file specification.")

        Else
            ' Create a TextWriterTraceListener object that takes a 
            ' file specification.
            Dim textListener As TextWriterTraceListener
            Try
                textListener = New TextWriterTraceListener(args(0))
                Trace.Listeners.Add(textListener)
            Catch ex As Exception
                Console.WriteLine( _
                    "Error creating TextWriterTraceListener for trace " & _
                    "file ""{0}"":" & vbCrLf & "{1}", args(0), ex.Message)
                Return
            End Try

            ' Write these messages only to the TextWriterTraceListener.
            textListener.WriteLine( _
                "This is trace listener named """ & textListener.Name & """")
            textListener.WriteLine("Trace written to a file: " & _
                vbCrLf & "    """ & args(0) & """")

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

            ' Flush and close the output.
            Trace.Flush()
            textListener.Flush()
            textListener.Close()
        End If
    End Sub
End Module

注解

此构造函数将 Name 属性初始化为空字符串 (“”) 。

另请参阅

适用于

TextWriterTraceListener(Stream, String)

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

用流作为调试和跟踪输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::IO::Stream ^ stream, System::String ^ name);
public TextWriterTraceListener (System.IO.Stream stream, string? name);
public TextWriterTraceListener (System.IO.Stream stream, string name);
new System.Diagnostics.TextWriterTraceListener : System.IO.Stream * string -> System.Diagnostics.TextWriterTraceListener
Public Sub New (stream As Stream, name As String)

参数

stream
Stream

表示 Stream 写入的流的 TextWriterTraceListener

name
String

新实例的名称。

例外

此流为 null

示例

下面的代码示例使用 TextWriterTraceListener(Stream, String) 构造函数创建 TextWriterTraceListener ,并将其添加到 。TraceListenerCollection 然后,该示例将两条消息写入此 TextWriterTraceListener ,并将一条消息写入 TraceListener 到 中的所有 TraceListenerCollection对象。 最后,它会刷新并关闭 TextWriterTraceListener

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();
        }
    }
}
Imports System.Diagnostics
Imports System.IO

Module TWTLConStreamNameMod

    Const LISTENER_NAME As String = "myStreamListener"

    ' args(0) is the specification of the trace log file.
    Sub Main(ByVal args() As String)

        ' Verify that a parameter was entered.
        If args.Length = 0 Then
            Console.WriteLine("Enter a trace file specification.")

        Else
            ' Create a stream object.
            Dim traceStream As FileStream
            Try
                traceStream = New FileStream( _
                    args(0), FileMode.Append, FileAccess.Write)
            Catch ex As Exception
                Console.WriteLine( _
                    "Error creating FileStream for trace file ""{0}"":" & _
                    vbCrLf & "{1}", args(0), ex.Message)
                Return
            End Try

            ' Create a TextWriterTraceListener object that takes a stream.
            Dim textListener As TextWriterTraceListener
            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: " & _
                vbCrLf & "    """ & args(0) & """")

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

            ' Flush and close the output.
            Trace.Flush()
            textListener.Flush()
            textListener.Close()
        End If
    End Sub
End Module

注解

如果nullname参数为 ,则此构造函数将 属性初始化Namename 参数或空字符串 (“”) 。

另请参阅

适用于

TextWriterTraceListener(TextWriter, String)

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

用指定的编写器作为跟踪或调试输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::IO::TextWriter ^ writer, System::String ^ name);
public TextWriterTraceListener (System.IO.TextWriter writer, string? name);
public TextWriterTraceListener (System.IO.TextWriter writer, string name);
new System.Diagnostics.TextWriterTraceListener : System.IO.TextWriter * string -> System.Diagnostics.TextWriterTraceListener
Public Sub New (writer As TextWriter, name As String)

参数

writer
TextWriter

接收来自 TextWriter 的输出的 TextWriterTraceListener

name
String

新实例的名称。

例外

编写器为 null

示例

下面的代码示例使用 TextWriterTraceListener(TextWriter, String) 构造函数创建 TextWriterTraceListener 。 该示例创建 一个 StreamWriter,然后在创建 TextWriterTraceListener时引用 StreamWriter ,然后将其添加到 。TraceListenerCollection 该示例将两条消息写入此 TextWriterTraceListener,然后将一条消息写入 TraceListener 中的所有 TraceListenerCollection对象。 最后,它会刷新并关闭 TextWriterTraceListener

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();
        }
    }
}
Imports System.Diagnostics
Imports System.IO

Module TWTLConWriterNameMod

    Const LISTENER_NAME As String = "myWriterListener"

    ' args(0) is the specification of the trace log file.
    Sub Main(ByVal args() As String)

        ' Verify that a parameter was entered.
        If args.Length = 0 Then
            Console.WriteLine("Enter a trace file specification.")

        Else
            ' Create a StreamWriter object that supports appending.
            Dim traceWriter As StreamWriter
            Try
                traceWriter = New StreamWriter(args(0), True)
            Catch ex As Exception
                Console.WriteLine( _
                    "Error creating StreamWriter for trace file ""{0}"":" & _
                    vbCrLf & "{1}", args(0), ex.Message)
                Return
            End Try

            ' Create a TextWriterTraceListener that takes a StreamWriter.
            Dim textListener As TextWriterTraceListener
            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: " & _
                vbCrLf & "    """ & args(0) & """")

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

            ' Flush and close the output.
            Trace.Flush()
            textListener.Flush()
            textListener.Close()
        End If
    End Sub
End Module

另请参阅

适用于

TextWriterTraceListener(String, String)

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

用文件作为调试和跟踪输出的接收者来初始化具有指定名称的 TextWriterTraceListener 类的新实例。

public:
 TextWriterTraceListener(System::String ^ fileName, System::String ^ name);
public TextWriterTraceListener (string? fileName, string? name);
public TextWriterTraceListener (string fileName, string name);
new System.Diagnostics.TextWriterTraceListener : string * string -> System.Diagnostics.TextWriterTraceListener
Public Sub New (fileName As String, name As String)

参数

fileName
String

TextWriterTraceListener 写入的文件名。

name
String

新实例的名称。

例外

此流为 null

示例

下面的代码示例使用 TextWriterTraceListener(String, String) 构造函数创建 TextWriterTraceListener ,然后将其添加到 。TraceListenerCollection 该示例将两条消息写入此 TextWriterTraceListener,然后将一条消息写入 TraceListener 中的所有 TraceListenerCollection对象。 最后,它会刷新并关闭 TextWriterTraceListener

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();
        }
    }
}
Imports System.Diagnostics

Module TWTLConStringNameMod

    Const LISTENER_NAME As String = "myStringListener"

    ' args(0) is the specification of the trace log file.
    Sub Main(ByVal args() As String)

        ' Verify that a parameter was entered.
        If args.Length = 0 Then
            Console.WriteLine("Enter a trace file specification.")

        Else
            ' Create a TextWriterTraceListener object that takes a 
            ' file specification.
            Dim textListener As TextWriterTraceListener
            Try
                textListener = _
                    New TextWriterTraceListener(args(0), LISTENER_NAME)
                Trace.Listeners.Add(textListener)
            Catch ex As Exception
                Console.WriteLine( _
                    "Error creating TextWriterTraceListener for trace " & _
                    "file ""{0}"":" & vbCrLf & "{1}", args(0), ex.Message)
                Return
            End Try

            ' Write these messages only to this TextWriterTraceListener.
            textListener.WriteLine( _
                "This is trace listener named """ & textListener.Name & """")
            textListener.WriteLine("Trace written to a file: " & _
                vbCrLf & "    """ & args(0) & """")

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

            ' Flush and close the output.
            Trace.Flush()
            textListener.Flush()
            textListener.Close()
        End If
    End Sub
End Module

注解

如果nullname参数为 ,则此构造函数将 属性初始化Namename 参数或空字符串 (“”) 。

另请参阅

适用于