2 // System.Diagnostics.TraceListener.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original
10 // (C) 2002 Jonathan Pryor
14 using System.Diagnostics;
16 namespace System.Diagnostics {
19 /// Provides the abstract base class for the listeners who monitor
20 /// trace and debug output
22 public abstract class TraceListener : MarshalByRefObject, IDisposable {
24 private int indentLevel = 0;
25 private int indentSize = 4;
26 private string name = null;
27 private bool needIndent = false;
30 /// Initializes a new instance of the <see cref="TraceListener">
31 /// TraceListener</see> class.
33 protected TraceListener () : this ("")
38 /// Initializes a new instance of the <see cref="TraceListener">
39 /// TraceListener</see> class using the specified name as the listener.
41 protected TraceListener (string name)
47 /// Gets or sets the indent level.
50 /// The indent level. The default is zero.
52 public int IndentLevel {
53 get {return indentLevel;}
54 set {indentLevel = value;}
58 /// Gets or sets the number of spaces in an indent.
61 /// The number of spaces in an indent. The default is four spaces.
63 public int IndentSize {
64 get {return indentSize;}
65 set {indentSize = value;}
69 /// Gets or sets a name for this
70 /// <see cref="TraceListener">TraceListener</see>.
73 /// A name for this <see cref="TraceListener">TraceListener</see>.
74 /// The default is the empty string ("")
76 public virtual string Name {
82 /// Gets or sets a value indicating whether to indent the output.
85 /// <b>true</b> if the output should be indented; otherwise <b>false</b>.
87 protected bool NeedIndent {
88 get {return needIndent;}
89 set {needIndent = value;}
93 /// When overridden in a derived class, closes the output stream so it
94 /// no longer receives tracing or debugging output.
96 public virtual void Close ()
102 /// Releases all resources used by the i
103 /// <see cref="TraceListener">TraceListener</see>.
105 public void Dispose ()
108 GC.SuppressFinalize (this);
112 /// Releases the unmanaged resources used by the
113 /// <see cref="TraceListener">TraceListener</see> and optionally
114 /// releases the managed resources.
116 /// <param name="disposing">
117 /// <b>true</b> to release both managed and unmanaged resources;
118 /// <b>false</b> to release only unmanaged resources.
120 protected virtual void Dispose (bool disposing)
125 /// Emits an error message to the listener you create when you
126 /// implement the <see cref="TraceListener">TraceListener</see> class.
128 /// <param name="message">
129 /// A message to emit.
131 public virtual void Fail (string message)
137 /// Emits an error message, and a detailed error message to the listener
138 /// you create when you implement the
139 /// <see cref="TraceListener">TraceListener</see> class.
141 /// <param name="message">
142 /// A message to emit.
144 /// <param name="detailMessage">
145 /// A detailed message to emit.
147 public virtual void Fail (string message, string detailMessage)
149 WriteLine ("---- DEBUG ASSERTION FAILED ----");
150 WriteLine ("---- Assert Short Message ----");
152 WriteLine ("---- Assert Long Message ----");
153 WriteLine (detailMessage);
158 /// When overridden in a derived class, flushes the output buffer.
160 public virtual void Flush ()
165 /// Writes the value of the object's
166 /// <see cref="System.Object.ToString">ToString</see>
167 /// method to the listener you create when you implement the
168 /// <see cref="TraceListener">TraceListener</see> class.
171 /// An <see cref="System.Object">Object</see> whose fully qualified
172 /// class name you want to write.
174 public virtual void Write (object o)
176 Write (o.ToString());
180 /// When overridden in a derived class, writes the specified message to
181 /// the listener you create in the derived class.
183 /// <param name="message">
184 /// A message to write.
186 public abstract void Write (string message);
189 /// Writes a category name and the value of the object's
190 /// <see cref="System.Object.ToString">ToString</see>
191 /// method to the listener you create when you implement the
192 /// <see cref="TraceListener">TraceListener</see> class.
195 /// An <see cref="System.Object">Object</see> whose fully qualified
196 /// class name you wish to write.
198 /// <param name="category">
199 /// A category name used to organize the output.
201 public virtual void Write (object o, string category)
203 Write (o.ToString(), category);
207 /// Writes a category name and a message to the listener you create when
208 /// you implement the <see cref="TraceListener">TraceListener</see> class.
210 /// <param name="message">
211 /// A message to write.
213 /// <param name="category">
214 /// A category name used to organize the output.
216 public virtual void Write (string message, string category)
218 Write (category + ": " + message);
222 /// Writes the indent to the listener you create when you implement
223 /// this class, and resets the <see cref="NeedIndent">NeedIndent</see>
224 /// Property to <b>false</b>.
226 protected virtual void WriteIndent ()
228 // Must set NeedIndent to false before Write; otherwise, we get endless
229 // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
231 String indent = new String (' ', IndentLevel*IndentSize);
236 /// Writes the value of the object's
237 /// <see cref="System.Object.ToString">ToString</see>
238 /// method to the listener you create when you implement the
239 /// <see cref="TraceListener">TraceListener</see> class, followed
240 /// by a line terminator.
243 /// An <see cref="System.Object">Object</see> whose fully qualified
244 /// class name you want to write.
246 public virtual void WriteLine (object o)
248 WriteLine (o.ToString());
252 /// When overridden in a derived class, writes the specified message to
253 /// the listener you create in the derived class, followed by a
256 /// <param name="message">
257 /// A message to write.
259 public abstract void WriteLine (string message);
262 /// Writes a category name and the value of the object's
263 /// <see cref="System.Object.ToString">ToString</see>
264 /// method to the listener you create when you implement the
265 /// <see cref="TraceListener">TraceListener</see> class, followed by a
269 /// An <see cref="System.Object">Object</see> whose fully qualified
270 /// class name you wish to write.
272 /// <param name="category">
273 /// A category name used to organize the output.
275 public virtual void WriteLine (object o, string category)
277 WriteLine (o.ToString(), category);
281 /// Writes a category name and a message to the listener you create when
282 /// you implement the <see cref="TraceListener">TraceListener</see> class,
283 /// followed by a line terminator.
285 /// <param name="message">
286 /// A message to write.
288 /// <param name="category">
289 /// A category name used to organize the output.
291 public virtual void WriteLine (string message, string category)
293 WriteLine (category + ": " + message);