2 // System.Diagnostics.TraceImpl.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2002, 2005 Jonathan Pryor
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Configuration;
36 using System.Threading;
38 namespace System.Diagnostics {
41 internal class TraceImplSettings {
42 public const string Key = ".__TraceInfoSettingsKey__.";
44 // Disable warning that AutoFlush is not used
45 #pragma warning disable 649
46 public bool AutoFlush;
47 #pragma warning restore
48 //public int IndentLevel;
49 public int IndentSize = 4;
50 public TraceListenerCollection Listeners = new TraceListenerCollection (false);
52 public TraceImplSettings ()
54 Listeners.Add (new DefaultTraceListener (), this);
59 static class TraceImpl {
62 private static object initLock = new object ();
65 private static bool autoFlush;
68 private static int indentLevel;
71 private static int indentSize;
74 static TraceListenerCollection listeners = new TraceListenerCollection (true);
76 static TraceListenerCollection listeners;
79 static bool use_global_lock;
80 static CorrelationManager correlation_manager = new CorrelationManager ();
82 public static bool AutoFlush {
93 public static int IndentLevel {
99 lock (ListenersSyncRoot) {
102 foreach (TraceListener t in Listeners) {
103 t.IndentLevel = indentLevel;
109 public static int IndentSize {
115 lock (ListenersSyncRoot) {
118 foreach (TraceListener t in Listeners) {
119 t.IndentSize = indentSize;
125 public static TraceListenerCollection Listeners {
133 private static object ListenersSyncRoot {
135 return ((ICollection) Listeners).SyncRoot;
139 public static CorrelationManager CorrelationManager {
142 return correlation_manager;
146 [MonoLimitation ("the property exists but it does nothing.")]
147 public static bool UseGlobalLock {
150 return use_global_lock;
154 use_global_lock = value;
158 // Initialize the world.
160 // This logically belongs in the static constructor (as it only needs
161 // to be done once), except for one thing: if the .config file has a
162 // syntax error, .NET throws a ConfigurationException. If we read the
163 // .config file in the static ctor, we throw a ConfigurationException
164 // from the static ctor, which results in a TypeLoadException. Oops.
165 // Reading the .config file here will allow the static ctor to
166 // complete successfully, allowing us to throw a normal
167 // ConfigurationException should the .config file contain an error.
169 // There are also some ordering issues.
171 // DiagnosticsConfigurationHandler doesn't store values within TraceImpl,
172 // but instead stores values it reads from the .config file within a
173 // TraceImplSettings object (accessible via the TraceImplSettings.Key key
174 // in the IDictionary returned).
175 private static void InitOnce ()
178 if (initLock != null) {
180 if (listeners == null) {
181 IDictionary d = DiagnosticsConfiguration.Settings;
182 TraceImplSettings s = (TraceImplSettings) d [TraceImplSettings.Key];
184 d.Remove (TraceImplSettings.Key);
186 autoFlush = s.AutoFlush;
187 // indentLevel = s.IndentLevel;
188 indentSize = s.IndentSize;
189 listeners = s.Listeners;
197 public static void Assert (bool condition)
203 public static void Assert (bool condition, string message)
209 public static void Assert (bool condition, string message,
210 string detailMessage)
213 Fail (message, detailMessage);
216 public static void Close ()
218 lock (ListenersSyncRoot) {
219 foreach (TraceListener listener in Listeners) {
225 // FIXME: From testing .NET, this method should display a dialog
226 //(it probably depends on the listener)p
228 public static void Fail (string message)
230 lock (ListenersSyncRoot) {
231 foreach (TraceListener listener in Listeners) {
232 listener.Fail (message);
237 // FIXME: From testing .NET, this method should display a dialog
238 // (it probably depends on the listener)p
240 public static void Fail (string message, string detailMessage)
242 lock (ListenersSyncRoot) {
243 foreach (TraceListener listener in Listeners) {
244 listener.Fail (message, detailMessage);
249 public static void Flush ()
251 lock (ListenersSyncRoot) {
252 foreach (TraceListener listener in Listeners){
258 public static void Indent ()
263 public static void Unindent ()
268 public static void Write (object value)
270 lock (ListenersSyncRoot) {
271 foreach (TraceListener listener in Listeners) {
272 listener.Write (value);
280 public static void Write (string message)
282 lock (ListenersSyncRoot) {
283 foreach (TraceListener listener in Listeners) {
284 listener.Write (message);
292 public static void Write (object value, string category)
294 lock (ListenersSyncRoot) {
295 foreach (TraceListener listener in Listeners) {
296 listener.Write (value, category);
304 public static void Write (string message, string category)
306 lock (ListenersSyncRoot) {
307 foreach (TraceListener listener in Listeners) {
308 listener.Write (message, category);
316 public static void WriteIf (bool condition, object value)
322 public static void WriteIf (bool condition, string message)
328 public static void WriteIf (bool condition, object value,
332 Write (value, category);
335 public static void WriteIf (bool condition, string message,
339 Write (message, category);
342 public static void WriteLine (object value)
344 lock (ListenersSyncRoot) {
345 foreach (TraceListener listener in Listeners) {
346 listener.WriteLine (value);
354 public static void WriteLine (string message)
356 lock (ListenersSyncRoot) {
357 foreach (TraceListener listener in Listeners) {
358 listener.WriteLine (message);
366 public static void WriteLine (object value, string category)
368 lock (ListenersSyncRoot) {
369 foreach (TraceListener listener in Listeners) {
370 listener.WriteLine (value, category);
378 public static void WriteLine (string message, string category)
380 lock (ListenersSyncRoot) {
381 foreach (TraceListener listener in Listeners) {
382 listener.WriteLine (message, category);
390 public static void WriteLineIf (bool condition, object value)
396 public static void WriteLineIf (bool condition, string message)
402 public static void WriteLineIf (bool condition, object value,
406 WriteLine (value, category);
409 public static void WriteLineIf (bool condition, string message,
413 WriteLine (message, category);