// // System.Diagnostics.TextWriterTraceListener.cs // // Authors: // Jonathan Pryor (jonpryor@vt.edu) // // Comments from John R. Hicks original // implementation. // // (C) 2002 Jonathan Pryor // using System; using System.IO; using System.Diagnostics; namespace System.Diagnostics { /// /// Directs tracing or debugging output to a /// TextWriter or to a Stream, /// such as Console.Out or /// FileStream. /// public class TextWriterTraceListener : TraceListener { private TextWriter writer; /// /// Initializes a new instance of the /// TextWriterTraceListener class with /// TextWriter /// as the output recipient. /// public TextWriterTraceListener () : base ("TextWriter") { } /// /// Initializes a new instance of the /// TextWriterTraceListener class, using the stream as the output /// recipient of the debugging and tracing output. /// /// /// A Stream that represents the stream /// the TextWriterTraceListener /// writes to. /// /// /// The stream is a null reference. /// public TextWriterTraceListener (Stream stream) : this (stream, "") { } /// /// Initializes a new instance of the /// TextWriterTraceListener class, using the file as the recipient /// of the debugging and tracing output. /// /// /// The name of the file the /// TextWriterTraceListener writes to. /// /// /// The fileName is null. /// public TextWriterTraceListener (string fileName) : this (fileName, "") { } /// /// Initializes a new instance of the /// TextWriterTraceListener class using the specified writer as the /// recipient of the tracing or debugging output. /// /// /// A TextWriter that receives /// output from the /// TextWriterTraceListener. /// /// /// The writer is a null reference /// public TextWriterTraceListener (TextWriter writer) : this (writer, "") { } /// /// Initializes a new instance of the /// TextWriterTraceListener class with the specified name, using the /// stream as the recipient of the tracing or debugging output. /// /// /// A Stream that represents the stream /// the TextWriterTraceListener /// writes to. /// /// /// The name of the new instance /// /// /// The stream is a null reference /// public TextWriterTraceListener (Stream stream, string name) : base (name != null ? name : "") { if (stream == null) throw new ArgumentNullException ("stream"); writer = new StreamWriter (stream); } /// /// Initializes a new instance of the /// TextWriterTraceListener class with the specified name, using the /// file as the recipient of the tracing or debugging output. /// /// /// The name of the file the /// TextWriterTraceListener writes to. /// /// /// The name of the new instance /// /// /// The file is a null reference. /// public TextWriterTraceListener (string fileName, string name) : base (name != null ? name : "") { if (fileName == null) throw new ArgumentNullException ("fileName"); writer = new StreamWriter (File.OpenWrite (fileName)); } /// /// Initializes a new instance of the /// TextWriterTraceListener class with the specified name, using /// the specified writer as the recipient of the tracing or /// debugging output. /// /// /// A TextWriter that receives /// the output from the /// TextWriterTraceListener. /// /// /// The name of the new instance. /// /// /// The writer is a null reference. /// public TextWriterTraceListener (TextWriter writer, string name) : base (name != null ? name : "") { if (writer == null) throw new ArgumentNullException ("writer"); this.writer = writer; } /// /// Gets or sets the writer that receives the debugging or tracing output. /// /// /// A TextWriter that represents /// the writer that receives the tracing or debugging output. /// public TextWriter Writer { get {return writer;} set {writer = value;} } /// /// Closes the Writer so that it no /// longer receives tracing or debugging output. /// public override void Close () { if (writer != null) { writer.Flush (); writer.Close (); writer = null; } } protected override void Dispose (bool disposing) { if (disposing) Close (); } /// /// Flushes the output buffer for the /// Writer. /// public override void Flush () { writer.Flush (); } /// /// Writes a message to this instance's /// Writer. /// /// /// A message to write. /// public override void Write (string message) { if (NeedIndent) WriteIndent (); writer.Write (message); } /// /// Writes a message to this instance's /// Writer /// followed by a line terminator. /// /// /// A message to write. /// public override void WriteLine (string message) { if (NeedIndent) WriteIndent (); writer.WriteLine (message); NeedIndent = true; } } }