2 // System.Diagnostics.TraceListener.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation
9 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
11 // (C) 2002 Jonathan Pryor
12 // (C) 2007 Novell, Inc.
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.Runtime.InteropServices;
40 using System.Diagnostics;
42 namespace System.Diagnostics {
44 public abstract class TraceListener : MarshalByRefObject, IDisposable {
47 private int indentLevel = 0;
50 private int indentSize = 4;
53 private StringDictionary attributes = new StringDictionary ();
56 private TraceFilter filter;
59 private TraceOptions options;
62 private bool needIndent = true;
64 protected TraceListener () : this ("")
68 protected TraceListener (string name)
73 public int IndentLevel {
74 get {return indentLevel;}
75 set {indentLevel = value;}
78 public int IndentSize {
79 get {return indentSize;}
80 set {indentSize = value;}
83 public virtual string Name {
88 protected bool NeedIndent {
89 get {return needIndent;}
90 set {needIndent = value;}
93 [MonoLimitation ("This property exists but is never considered.")]
94 public virtual bool IsThreadSafe {
98 public virtual void Close ()
103 public void Dispose ()
106 GC.SuppressFinalize (this);
109 protected virtual void Dispose (bool disposing)
113 public virtual void Fail (string message)
115 Fail (message, null);
118 public virtual void Fail (string message, string detailMessage)
120 WriteLine ("---- DEBUG ASSERTION FAILED ----");
121 WriteLine ("---- Assert Short Message ----");
123 if (detailMessage != null) {
124 WriteLine ("---- Assert Long Message ----");
125 WriteLine (detailMessage);
131 public virtual void Flush ()
135 public virtual void Write (object o)
137 Write (o.ToString());
140 public abstract void Write (string message);
142 public virtual void Write (object o, string category)
144 Write (o.ToString(), category);
147 public virtual void Write (string message, string category)
149 Write (category + ": " + message);
152 protected virtual void WriteIndent ()
154 // Must set NeedIndent to false before Write; otherwise, we get endless
155 // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
157 String indent = new String (' ', IndentLevel*IndentSize);
161 public virtual void WriteLine (object o)
163 WriteLine (o.ToString());
166 public abstract void WriteLine (string message);
168 public virtual void WriteLine (object o, string category)
170 WriteLine (o.ToString(), category);
173 public virtual void WriteLine (string message, string category)
175 WriteLine (category + ": " + message);
178 internal static string FormatArray (ICollection list, string joiner)
180 string [] arr = new string [list.Count];
182 foreach (object o in list)
183 arr [i++] = o != null ? o.ToString () : String.Empty;
184 return String.Join (joiner, arr);
189 public virtual void TraceData (TraceEventCache eventCache, string source,
190 TraceEventType eventType, int id, object data)
192 if (Filter != null &&
193 !Filter.ShouldTrace (eventCache, source, eventType,
194 id, null, null, data, null))
197 WriteLine (String.Format ("{0} {1}: {2} : {3}", source, eventType, id, data));
199 if (eventCache == null)
202 if ((TraceOutputOptions & TraceOptions.ProcessId) != 0)
203 WriteLine (" ProcessId=" + eventCache.ProcessId);
204 if ((TraceOutputOptions & TraceOptions.LogicalOperationStack) != 0)
205 WriteLine (" LogicalOperationStack=" + FormatArray (eventCache.LogicalOperationStack, ", "));
206 if ((TraceOutputOptions & TraceOptions.ThreadId) != 0)
207 WriteLine (" ThreadId=" + eventCache.ThreadId);
208 if ((TraceOutputOptions & TraceOptions.DateTime) != 0)
209 WriteLine (" DateTime=" + eventCache.DateTime.ToString ("o"));
210 if ((TraceOutputOptions & TraceOptions.Timestamp) != 0)
211 WriteLine (" Timestamp=" + eventCache.Timestamp);
212 if ((TraceOutputOptions & TraceOptions.Callstack) != 0)
213 WriteLine (" Callstack=" + eventCache.Callstack);
217 public virtual void TraceData (TraceEventCache eventCache, string source,
218 TraceEventType eventType, int id, params object [] data)
220 if (Filter != null &&
221 !Filter.ShouldTrace (eventCache, source, eventType,
222 id, null, null, null, data))
225 TraceData (eventCache, source, eventType, id, FormatArray (data, " "));
229 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id)
231 TraceEvent (eventCache, source, eventType, id, null);
235 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType,
236 int id, string message)
238 TraceData (eventCache, source, eventType, id, message);
242 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object [] args)
244 TraceEvent (eventCache, source, eventType, id, String.Format (format, args));
248 public virtual void TraceTransfer (TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
250 TraceEvent (eventCache, source, TraceEventType.Transfer, id, String.Format ("{0}, relatedActivityId={1}", message, relatedActivityId));
254 protected internal virtual string [] GetSupportedAttributes ()
259 public StringDictionary Attributes {
260 get { return attributes; }
264 [ComVisibleAttribute (false)]
265 public TraceFilter Filter {
266 get { return filter; }
267 set { filter = value; }
271 [ComVisibleAttribute (false)]
272 public TraceOptions TraceOutputOptions {
273 get { return options; }
274 set { options = value; }