TextWriterTraceListener 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 TextWriterTraceListener 類別的新執行個體。
多載
TextWriterTraceListener() |
使用 TextWriterTraceListener 作為輸出接收者,初始化 TextWriter 類別的新執行個體。 |
TextWriterTraceListener(Stream) |
使用資料流做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener(TextWriter) |
使用指定的寫入器當做追蹤或偵錯之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener(String) |
使用檔案做為偵錯和追蹤之輸出的接收者,來初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener(Stream, String) |
使用資料流做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener(TextWriter, String) |
使用指定的寫入器做為追蹤或偵錯之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener(String, String) |
使用檔案做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 TextWriterTraceListener 類別的新執行個體。 |
TextWriterTraceListener()
使用 TextWriterTraceListener 作為輸出接收者,初始化 TextWriter 類別的新執行個體。
public:
TextWriterTraceListener();
public TextWriterTraceListener ();
Public Sub New ()
範例
下列範例會 TextWriterTraceListener 使用 建 TextWriterTraceListener() 構函式建立 。 它會將 Writer 屬性設定為主控台輸出,然後將 新增 TextWriterTraceListener 至 TraceListenerCollection。 它會在兩個區段中寫入訊息,然後關閉 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) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(Stream)
使用資料流做為偵錯和追蹤之輸出的接收者,來初始化 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 屬性初始化為空字串 (“”) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(TextWriter)
使用指定的寫入器當做追蹤或偵錯之輸出的接收者,來初始化 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。
例外狀況
writer 為 null
。
範例
下列程式代碼範例會 TextWriterTraceListener 使用 建 TextWriterTraceListener(TextWriter) 構函式建立 。 此範例會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 屬性初始化為空字串 (“”) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(String)
使用檔案做為偵錯和追蹤之輸出的接收者,來初始化 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 使用 建 TextWriterTraceListener(String) 構函式建立 ,然後將它新增至 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 屬性初始化為空字串 (“”) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(Stream, String)
使用資料流做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 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
備註
如果 name
參數為 null
,這個建構函式會將 Name 屬性初始化為 name
參數或空字串 (“”“”) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(TextWriter, String)
使用指定的寫入器做為追蹤或偵錯之輸出的接收者,以指定的名稱初始化 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
新執行個體的名稱。
例外狀況
writer 為 null
。
範例
下列程式代碼範例會 TextWriterTraceListener 使用 建 TextWriterTraceListener(TextWriter, String) 構函式建立 。 此範例會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
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace
適用於
TextWriterTraceListener(String, String)
使用檔案做為偵錯和追蹤之輸出的接收者,以指定的名稱初始化 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 使用 建 TextWriterTraceListener(String, String) 構函式建立 ,然後將它新增至 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
備註
如果 name
參數為 null
,這個建構函式會將 Name 屬性初始化為 name
參數或空字串 (“”“”) 。
另請參閱
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- EventLogTraceListener
- TextWriterTraceListener
- Stream
- TextWriter
- Debug
- Trace