2 // System.Diagnostics.TextWriterTraceListener.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original
10 // (C) 2002 Jonathan Pryor
15 using System.Diagnostics;
17 namespace System.Diagnostics {
20 /// Directs tracing or debugging output to a <see cref="System.IO.TextWriter">
21 /// TextWriter</see> or to a <see cref="System.IO.Stream">Stream</see>,
22 /// such as <see cref="System.Console.Out">Console.Out</see> or
23 /// <see cref="System.IO.FileStream">FileStream</see>.
25 public class TextWriterTraceListener : TraceListener {
27 private TextWriter writer;
30 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
31 /// TextWriterTraceListener</see> class with
32 /// <see cref="System.IO.TextWriter">TextWriter</see>
33 /// as the output recipient.
35 public TextWriterTraceListener () : base ("TextWriter")
40 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
41 /// TextWriterTraceListener</see> class, using the stream as the output
42 /// recipient of the debugging and tracing output.
44 /// <param name="stream">
45 /// A <see cref="System.IO.Stream">Stream</see> that represents the stream
46 /// the <see cref="TextWriterTraceListener">TextWriterTraceListener</see>
49 /// <exception cref="System.ArgumentNullException">
50 /// The stream is a null reference.
52 public TextWriterTraceListener (Stream stream)
58 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
59 /// TextWriterTraceListener</see> class, using the file as the recipient
60 /// of the debugging and tracing output.
62 /// <param name="fileName">
63 /// The name of the file the <see cref="TextWriterTraceListener">
64 /// TextWriterTraceListener</see> writes to.
66 /// <exception cref="System.ArgumentNullException">
67 /// The fileName is null.
69 public TextWriterTraceListener (string fileName)
75 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
76 /// TextWriterTraceListener</see> class using the specified writer as the
77 /// recipient of the tracing or debugging output.
79 /// <param name="writer">
80 /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives
82 /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.
84 /// <exception cref="System.ArgumentNullException">
85 /// The writer is a null reference
87 public TextWriterTraceListener (TextWriter writer)
93 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
94 /// TextWriterTraceListener</see> class with the specified name, using the
95 /// stream as the recipient of the tracing or debugging output.
97 /// <param name="stream">
98 /// A <see cref="System.IO.Stream">Stream</see> that represents the stream
99 /// the <see cref="TextWriterTraceListener">TextWriterTraceListener</see>
102 /// <param name="name">
103 /// The name of the new instance
105 /// <exception cref="System.ArgumentNullException">
106 /// The stream is a null reference
108 public TextWriterTraceListener (Stream stream, string name)
109 : base (name != null ? name : "")
112 throw new ArgumentNullException ("stream");
113 writer = new StreamWriter (stream);
117 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
118 /// TextWriterTraceListener</see> class with the specified name, using the
119 /// file as the recipient of the tracing or debugging output.
121 /// <param name="fileName">
122 /// The name of the file the <see cref="TextWriterTraceListener">
123 /// TextWriterTraceListener</see> writes to.
125 /// <param name="name">
126 /// The name of the new instance
128 /// <exception cref="System.ArgumentNullException">
129 /// The file is a null reference.
131 public TextWriterTraceListener (string fileName, string name)
132 : base (name != null ? name : "")
134 if (fileName == null)
135 throw new ArgumentNullException ("fileName");
136 writer = new StreamWriter (File.OpenWrite (fileName));
140 /// Initializes a new instance of the <see cref="TextWriterTraceListener">
141 /// TextWriterTraceListener</see> class with the specified name, using
142 /// the specified writer as the recipient of the tracing or
143 /// debugging output.
145 /// <param name="writer">
146 /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives
147 /// the output from the
148 /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.
150 /// <param name="name">
151 /// The name of the new instance.
153 /// <exception cref="System.ArgumentNullException">
154 /// The writer is a null reference.
156 public TextWriterTraceListener (TextWriter writer, string name)
157 : base (name != null ? name : "")
160 throw new ArgumentNullException ("writer");
161 this.writer = writer;
165 /// Gets or sets the writer that receives the debugging or tracing output.
168 /// A <see cref="System.IO.TextWriter">TextWriter</see> that represents
169 /// the writer that receives the tracing or debugging output.
171 public TextWriter Writer {
173 set {writer = value;}
177 /// Closes the <see cref="System.IO.Writer">Writer</see> so that it no
178 /// longer receives tracing or debugging output.
180 public override void Close ()
182 if (writer != null) {
189 protected override void Dispose (bool disposing)
196 /// Flushes the output buffer for the
197 /// <see cref="System.IO.Writer">Writer</see>.
199 public override void Flush ()
205 /// Writes a message to this instance's
206 /// <see cref="System.IO.Writer">Writer</see>.
208 /// <param name="message">
209 /// A message to write.
211 public override void Write (string message)
215 writer.Write (message);
219 /// Writes a message to this instance's
220 /// <see cref="System.IO.Writer">Writer</see>
221 /// followed by a line terminator.
223 /// <param name="message">
224 /// A message to write.
226 public override void WriteLine (string message)
230 writer.WriteLine (message);