public class TextWriterTraceListener : TraceListener
|
This class also provide methods to TextWriterTraceListener.Close the TextWriterTraceListener.Writer so that it no longer receives tracing or debugging output, to TextWriterTraceListener.Flush the output buffer for the TextWriterTraceListener.Writer, and to TextWriterTraceListener.Write a message to the TextWriterTraceListener.Writer.
You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.
To set the level of your listener, edit the configuration file that corresponds to the name of your application. Within this file, you can add a listener, set its type and set its parameter, remove a listener, or clear all the listeners previously set by the application. The configuration file should be formatted like the following example:
<configuration> <system.diagnostics> <switches> <add name="MagicTraceSwitch" value="3" /> </switches> <trace autoflush="false" indentsize="4"> <listeners> <add name="myListener" type="System.Diagnostics.TextWriterTraceListener,System" initializeData="c:\myListener.log" /> <remove type="System.Diagnostics.DefaultTraceListener,System"/> </listeners> </trace> </system.diagnostics> </configuration>
myTextListener
, uses a StreamWriter called
myOutputWriter
to write to a file named
TestFile.txt
. The second instance,
myWriter
, outputs to the console screen.First the example creates a file for output. Then it creates the StreamWriter for the first text writer, assigns it the output file, and adds it to the Trace.Listeners. Next it creates the second text writer, which it also add to the Trace.Listeners. Then, the code outputs one line of text to the file and the console screen, and two lines to the console screen. Finally, the example flushes the output buffer and closes the buffer.
After running this sample, you can open the
TestFile.txt
file to see the output.
public static int Main(string[] args) { // Create a file for output named TestFile.txt. Stream myFile = File.Create("TestFile.txt"); /* Create a new text writer using the output stream, and add it to * the trace listeners. */ TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile); Trace.Listeners.Add(myTextListener); /* Create a text writer that writes to the console screen, and add * it to the trace listeners */ TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Trace.Listeners.Add(myWriter); // Write output to the file and to the console screen. Trace.Write("Test output "); // Write only to the console screen. myWriter.WriteLine("Write only to the console screen."); // Flush and close the output. myWriter.Flush(); myWriter.Close(); return 0; }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the TextWriterTraceListener class with TextWriter as the output recipient. |
ctor #2 | Overloaded:.ctor(Stream stream) Initializes a new instance of the TextWriterTraceListener class, using the stream as the recipient of the debugging and tracing output. |
ctor #3 | Overloaded:.ctor(string fileName) Initializes a new instance of the TextWriterTraceListener class, using the file as the recipient of the debugging and tracing output. |
ctor #4 | Overloaded:.ctor(TextWriter writer) Initializes a new instance of the TextWriterTraceListener class using the specified writer as recipient of the tracing or debugging output. |
ctor #5 | Overloaded:.ctor(Stream stream, string name) Initializes a new instance of the TextWriterTraceListener class with the specified name, using the stream as the recipient of the debugging and tracing output. |
ctor #6 | Overloaded:.ctor(string fileName, string name) Initializes a new instance of the TextWriterTraceListener class with the specified name, using the file as the recipient of the debugging and tracing output. |
ctor #7 | Overloaded:.ctor(TextWriter writer, string name) Initializes a new instance of the TextWriterTraceListener class with the specified name, using the specified writer as recipient of the tracing or debugging output. |
IndentLevel (inherited from System.Diagnostics.TraceListener) |
Read-write See base class member description: System.Diagnostics.TraceListener.IndentLevel Gets or sets the indent level. |
IndentSize (inherited from System.Diagnostics.TraceListener) |
Read-write See base class member description: System.Diagnostics.TraceListener.IndentSize Gets or sets the number of spaces in an indent. |
Name (inherited from System.Diagnostics.TraceListener) |
Read-write See base class member description: System.Diagnostics.TraceListener.Name Gets or sets a name for this TraceListener. |
Writer | Read-write Gets or sets the text writer that receives the tracing or debugging output. |
Close | Overridden: Closes the TextWriterTraceListener.Writer so that it no longer receives tracing or debugging output. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
Dispose (inherited from System.Diagnostics.TraceListener) |
Overloaded:Dispose() See base class member description: System.Diagnostics.TraceListener.DisposeReleases all resources used by the TraceListener. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Fail (inherited from System.Diagnostics.TraceListener) |
Overloaded:Fail(string message) See base class member description: System.Diagnostics.TraceListener.FailEmits an error message to the listener you create when you implement the TraceListener class. |
Fail (inherited from System.Diagnostics.TraceListener) |
Overloaded:Fail(string message, string detailMessage) See base class member description: System.Diagnostics.TraceListener.FailEmits an error message, and a detailed error message to the listener you create when you implement the TraceListener class. |
Flush | Overridden: Flushes the output buffer for the TextWriterTraceListener.Writer. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Write (inherited from System.Diagnostics.TraceListener) |
Overloaded:Write(object o) See base class member description: System.Diagnostics.TraceListener.WriteWrites the value of the object's Object.ToString method to the listener you create when you implement the TraceListener class. |
Write | Overloaded:Write(string message) Overridden: Writes a message to this instance's TextWriterTraceListener.Writer. |
Write (inherited from System.Diagnostics.TraceListener) |
Overloaded:Write(object o, string category) See base class member description: System.Diagnostics.TraceListener.WriteWrites a category name and the value of the object's Object.ToString method to the listener you create when you implement the TraceListener class. |
Write (inherited from System.Diagnostics.TraceListener) |
Overloaded:Write(string message, string category) See base class member description: System.Diagnostics.TraceListener.WriteWrites a category name and a message to the listener you create when you implement the TraceListener class. |
WriteLine (inherited from System.Diagnostics.TraceListener) |
Overloaded:WriteLine(object o) See base class member description: System.Diagnostics.TraceListener.WriteLineWrites the value of the object's Object.ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator. |
WriteLine | Overloaded:WriteLine(string message) Overridden: Writes a message to this instance's TextWriterTraceListener.Writer followed by a line terminator. The default line terminator is a carriage return followed by a line feed (\r\n). |
WriteLine (inherited from System.Diagnostics.TraceListener) |
Overloaded:WriteLine(object o, string category) See base class member description: System.Diagnostics.TraceListener.WriteLineWrites a category name and the value of the object's Object.ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator. |
WriteLine (inherited from System.Diagnostics.TraceListener) |
Overloaded:WriteLine(string message, string category) See base class member description: System.Diagnostics.TraceListener.WriteLineWrites a category name and a message to the listener you create when you implement the TraceListener class, followed by a line terminator. |
NeedIndent (inherited from System.Diagnostics.TraceListener) |
Read-write See base class member description: System.Diagnostics.TraceListener.NeedIndent Gets or sets a value indicating whether to indent the output. |
Dispose | Overloaded:Dispose(bool disposing) Overridden: Disposes this TextWriterTraceListener object. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
WriteIndent (inherited from System.Diagnostics.TraceListener) |
See base class member description: System.Diagnostics.TraceListener.WriteIndent Writes the indent to the listener you create when you implement this class, and resets the TraceListener.NeedIndent property to false. |
Hierarchy:
public TextWriterTraceListener(); |
public TextWriterTraceListener( |
stream
Exception Type | Condition |
---|---|
ArgumentNullException | The stream is null. |
public TextWriterTraceListener( |
fileName
Exception Type | Condition |
---|---|
ArgumentNullException | The file is null. |
public TextWriterTraceListener( |
writer
Exception Type | Condition |
---|---|
ArgumentNullException | The writer is null. |
stream
name
Exception Type | Condition |
---|---|
ArgumentNullException | The stream is null. |
fileName
name
Exception Type | Condition |
---|---|
ArgumentNullException | The stream is null. |
public TextWriterTraceListener( |
writer
name
Exception Type | Condition |
---|---|
ArgumentNullException | The writer is null. |
public int IndentLevel {get; set;}
|
public int IndentSize {get; set;}
|
public virtual string Name {get; set;}
|
protected bool NeedIndent {get; set;}
|
public TextWriter Writer {get; set;}
|
TextWriterTraceListener myWriter = new TextWriterTraceListener(); myWriter.Writer = System.Console.Out; Trace.Listeners.Add(myWriter);
public override void Close(); |
myTextListener
, which uses a StreamWriter called
myOutputWriter
to write to a file named
TestFile.txt
. The example creates the file, stream, and text writer, writes one line of text to the file, and then flushes and closes the stream.public static void Main(string[] args) { // Create a file for output named TestFile.txt. String myFileName = "TestFile.txt"; if(!File.Exists(myFileName)) { Stream myFile = File.Create(myFileName); // If the file cannot be created, exit the application. if(myFile == null) { Console.WriteLine("Could not create file"); Application.Exit(); } } // Assign output file to the output stream. StreamWriter myOutputWriter; myOutputWriter = File.AppendText(myFileName); /* Create a new text writer using the output stream and * add it to the trace listeners. */ TextWriterTraceListener myTextListener = new TextWriterTraceListener(myOutputWriter); Trace.Listeners.Add(myTextListener); // Write output to the file. Trace.WriteLine("Test output"); // Flush and close the output stream. myOutputWriter.Flush(); myOutputWriter.Close(); }
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public void Dispose(); |
protected override void Dispose( |
disposing
public virtual void Fail( |
message
message
detailMessage
~TextWriterTraceListener(); |
public override void Flush(); |
myTextListener
, which uses a StreamWriter called
myOutputWriter
to write to a file named
TestFile.txt
. The example creates the file, stream, and text writer, writes one line of text to the file, and then flushes and closes the stream.public static void Main(string[] args) { // Create a file for output named TestFile.txt. String myFileName = "TestFile.txt"; if(!File.Exists(myFileName)) { Stream myFile = File.Create(myFileName); // If the file cannot be created, exit the application. if(myFile == null) { Console.WriteLine("Could not create file"); Application.Exit(); } } // Assign output file to the output stream. StreamWriter myOutputWriter; myOutputWriter = File.AppendText(myFileName); /* Create a new text writer using the output stream and * add it to the trace listeners. */ TextWriterTraceListener myTextListener = new TextWriterTraceListener(myOutputWriter); Trace.Listeners.Add(myTextListener); // Write output to the file. Trace.WriteLine("Test output"); // Flush and close the output stream. myOutputWriter.Flush(); myOutputWriter.Close(); }
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public virtual string ToString(); |
public virtual void Write( |
o
public override void Write( |
message
myWriter
to write to the console screen. The example writes two lines to the console screen. Note the second write appears on the same line as the first write. The example then flushes and closes the stream.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 console screen. "); myWriter.WriteLine("Again, write to the Console screen."); // Flush and close the output. myWriter.Flush(); myWriter.Close(); }
o
category
message
category
protected virtual void WriteIndent(); |
public virtual void WriteLine( |
o
public override void WriteLine( |
message
myWriter
to write to the console screen. The example writes two lines to the console screen. Note the second write appears on the same line as the first write. The example then flushes and closes the stream.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(); }
o
category
message
category