Bagikan melalui


TextWriterTraceListener Konstruktor

Definisi

Menginisialisasi instans baru kelas TextWriterTraceListener.

Overload

TextWriterTraceListener()

Menginisialisasi instans TextWriterTraceListener baru kelas sebagai TextWriter penerima output.

TextWriterTraceListener(Stream)

Menginisialisasi instans TextWriterTraceListener baru kelas , menggunakan aliran sebagai penerima output penelusuran kesalahan dan pelacakan.

TextWriterTraceListener(TextWriter)

Menginisialisasi instans TextWriterTraceListener baru kelas menggunakan penulis yang ditentukan sebagai penerima output pelacakan atau penelusuran kesalahan.

TextWriterTraceListener(String)

Menginisialisasi instans TextWriterTraceListener baru kelas , menggunakan file sebagai penerima output penelusuran kesalahan dan pelacakan.

TextWriterTraceListener(Stream, String)

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan aliran sebagai penerima output penelusuran kesalahan dan pelacakan.

TextWriterTraceListener(TextWriter, String)

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan penulis yang ditentukan sebagai penerima output pelacakan atau penelusuran kesalahan.

TextWriterTraceListener(String, String)

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan file sebagai penerima output penelusuran kesalahan dan pelacakan.

TextWriterTraceListener()

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas sebagai TextWriter penerima output.

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

Contoh

Contoh berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener() konstruktor. Ini mengatur properti ke Writer output konsol, lalu menambahkan ke TextWriterTraceListenerTraceListenerCollection. Ini menulis pesan dalam dua segmen, dan kemudian menutup 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

Keterangan

Konstruktor ini menggunakan TextWriter aliran sebagai penerima output pelacakan atau penelusuran kesalahan. Propertinya Name diinisialisasi ke string kosong ("", atau String.Empty).

Lihat juga

Berlaku untuk

TextWriterTraceListener(Stream)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas , menggunakan aliran sebagai penerima output penelusuran kesalahan dan pelacakan.

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)

Parameter

stream
Stream

Stream yang mewakili aliran penulisanTextWriterTraceListener.

Pengecualian

Alirannya adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(Stream) konstruktor dan menambahkannya ke TraceListenerCollection. Contoh kemudian menulis dua pesan ke ini TextWriterTraceListener, dan menulis pesan ke semua TraceListener objek di TraceListenerCollection. Akhirnya, ia menyiram dan menutup 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

Keterangan

Konstruktor ini menginisialisasi Name properti ke string kosong ("").

Lihat juga

Berlaku untuk

TextWriterTraceListener(TextWriter)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas menggunakan penulis yang ditentukan sebagai penerima output pelacakan atau penelusuran kesalahan.

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)

Parameter

writer
TextWriter

TextWriter yang menerima output dari TextWriterTraceListener.

Pengecualian

Penulis adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(TextWriter) konstruktor. Contoh membuat StreamWriter, lalu mereferensikan StreamWriter saat membuat TextWriterTraceListener, yang kemudian ditambahkan ke TraceListenerCollection. Contoh menulis pesan ke semua TraceListener objek di TraceListenerCollection, lalu menutup ini 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

Keterangan

Konstruktor ini menginisialisasi Name properti ke string kosong ("").

Lihat juga

Berlaku untuk

TextWriterTraceListener(String)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas , menggunakan file sebagai penerima output penelusuran kesalahan dan pelacakan.

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)

Parameter

fileName
String

Nama file tujuan penulisan TextWriterTraceListener.

Pengecualian

Filenya adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(String) konstruktor, lalu menambahkannya ke TraceListenerCollection. Contoh menulis dua pesan ke ini TextWriterTraceListener, lalu menulis pesan ke semua TraceListener objek di TraceListenerCollection. Akhirnya, ia menyiram dan menutup 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

Keterangan

Konstruktor ini menginisialisasi Name properti ke string kosong ("").

Lihat juga

Berlaku untuk

TextWriterTraceListener(Stream, String)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan aliran sebagai penerima output penelusuran kesalahan dan pelacakan.

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)

Parameter

stream
Stream

Stream yang mewakili aliran penulisanTextWriterTraceListener.

name
String

Nama instans baru.

Pengecualian

Alirannya adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(Stream, String) konstruktor dan menambahkannya ke TraceListenerCollection. Contoh kemudian menulis dua pesan ke ini TextWriterTraceListener dan menulis pesan ke semua TraceListener objek di TraceListenerCollection. Akhirnya, ia menyiram dan menutup 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

Keterangan

Konstruktor ini menginisialisasi Name properti ke name parameter atau ke string kosong (""), jika name parameternya adalah null.

Lihat juga

Berlaku untuk

TextWriterTraceListener(TextWriter, String)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan penulis yang ditentukan sebagai penerima output pelacakan atau penelusuran kesalahan.

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)

Parameter

writer
TextWriter

TextWriter yang menerima output dari TextWriterTraceListener.

name
String

Nama instans baru.

Pengecualian

Penulis adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(TextWriter, String) konstruktor. Contoh membuat StreamWriter, lalu mereferensikan StreamWriter saat membuat TextWriterTraceListener, yang kemudian ditambahkan ke TraceListenerCollection. Contoh menulis dua pesan ke ini TextWriterTraceListener, lalu menulis pesan ke semua TraceListener objek di TraceListenerCollection. Akhirnya, ia menyiram dan menutup 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

Lihat juga

Berlaku untuk

TextWriterTraceListener(String, String)

Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs
Sumber:
TextWriterTraceListener.cs

Menginisialisasi instans TextWriterTraceListener baru kelas dengan nama yang ditentukan, menggunakan file sebagai penerima output penelusuran kesalahan dan pelacakan.

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)

Parameter

fileName
String

Nama file tujuan penulisan TextWriterTraceListener.

name
String

Nama instans baru.

Pengecualian

Alirannya adalah null.

Contoh

Contoh kode berikut membuat TextWriterTraceListener menggunakan TextWriterTraceListener(String, String) konstruktor, lalu menambahkannya ke TraceListenerCollection. Contoh menulis dua pesan ke ini TextWriterTraceListener, lalu menulis pesan ke semua TraceListener objek di TraceListenerCollection. Akhirnya, ia menyiram dan menutup 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

Keterangan

Konstruktor ini menginisialisasi Name properti ke name parameter atau ke string kosong (""), jika name parameternya adalah null.

Lihat juga

Berlaku untuk