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 ();
52 public TraceImplSettings ()
54 Listeners.Add (new DefaultTraceListener () { IndentSize = this.IndentSize });
60 static class TraceImpl {
63 private static object initLock = new object ();
66 private static bool autoFlush;
69 private static int indentLevel;
72 private static int indentSize;
75 static TraceListenerCollection listeners = new TraceListenerCollection (true);
77 static TraceListenerCollection listeners;
80 static bool use_global_lock;
81 static CorrelationManager correlation_manager = new CorrelationManager ();
83 public static bool AutoFlush {
94 public static int IndentLevel {
100 lock (ListenersSyncRoot) {
103 foreach (TraceListener t in Listeners) {
104 t.IndentLevel = indentLevel;
110 public static int IndentSize {
116 lock (ListenersSyncRoot) {
119 foreach (TraceListener t in Listeners) {
120 t.IndentSize = indentSize;
126 public static TraceListenerCollection Listeners {
134 private static object ListenersSyncRoot {
136 return ((ICollection) Listeners).SyncRoot;
140 public static CorrelationManager CorrelationManager {
143 return correlation_manager;
147 [MonoLimitation ("the property exists but it does nothing.")]
148 public static bool UseGlobalLock {
151 return use_global_lock;
155 use_global_lock = value;
159 // Initialize the world.
161 // This logically belongs in the static constructor (as it only needs
162 // to be done once), except for one thing: if the .config file has a
163 // syntax error, .NET throws a ConfigurationException. If we read the
164 // .config file in the static ctor, we throw a ConfigurationException
165 // from the static ctor, which results in a TypeLoadException. Oops.
166 // Reading the .config file here will allow the static ctor to
167 // complete successfully, allowing us to throw a normal
168 // ConfigurationException should the .config file contain an error.
170 // There are also some ordering issues.
172 // DiagnosticsConfigurationHandler doesn't store values within TraceImpl,
173 // but instead stores values it reads from the .config file within a
174 // TraceImplSettings object (accessible via the TraceImplSettings.Key key
175 // in the IDictionary returned).
176 private static void InitOnce ()
179 if (initLock != null) {
181 if (listeners == null) {
182 IDictionary d = DiagnosticsConfiguration.Settings;
183 TraceImplSettings s = (TraceImplSettings) d [TraceImplSettings.Key];
185 d.Remove (TraceImplSettings.Key);
187 autoFlush = s.AutoFlush;
188 // indentLevel = s.IndentLevel;
189 indentSize = s.IndentSize;
190 listeners = s.Listeners;
198 public static void Assert (bool condition)
204 public static void Assert (bool condition, string message)
210 public static void Assert (bool condition, string message,
211 string detailMessage)
214 Fail (message, detailMessage);
217 public static void Close ()
219 lock (ListenersSyncRoot) {
220 foreach (TraceListener listener in Listeners) {
226 // FIXME: From testing .NET, this method should display a dialog
227 //(it probably depends on the listener)p
229 public static void Fail (string message)
231 lock (ListenersSyncRoot) {
232 foreach (TraceListener listener in Listeners) {
233 listener.Fail (message);
238 // FIXME: From testing .NET, this method should display a dialog
239 // (it probably depends on the listener)p
241 public static void Fail (string message, string detailMessage)
243 lock (ListenersSyncRoot) {
244 foreach (TraceListener listener in Listeners) {
245 listener.Fail (message, detailMessage);
250 public static void Flush ()
252 lock (ListenersSyncRoot) {
253 foreach (TraceListener listener in Listeners){
259 public static void Indent ()
264 public static void Unindent ()
269 public static void Write (object value)
271 lock (ListenersSyncRoot) {
272 foreach (TraceListener listener in Listeners) {
273 listener.Write (value);
281 public static void Write (string message)
283 lock (ListenersSyncRoot) {
284 foreach (TraceListener listener in Listeners) {
285 listener.Write (message);
293 public static void Write (object value, string category)
295 lock (ListenersSyncRoot) {
296 foreach (TraceListener listener in Listeners) {
297 listener.Write (value, category);
305 public static void Write (string message, string category)
307 lock (ListenersSyncRoot) {
308 foreach (TraceListener listener in Listeners) {
309 listener.Write (message, category);
317 public static void WriteIf (bool condition, object value)
323 public static void WriteIf (bool condition, string message)
329 public static void WriteIf (bool condition, object value,
333 Write (value, category);
336 public static void WriteIf (bool condition, string message,
340 Write (message, category);
343 public static void WriteLine (object value)
345 lock (ListenersSyncRoot) {
346 foreach (TraceListener listener in Listeners) {
347 listener.WriteLine (value);
355 public static void WriteLine (string message)
357 lock (ListenersSyncRoot) {
358 foreach (TraceListener listener in Listeners) {
359 listener.WriteLine (message);
367 public static void WriteLine (object value, string category)
369 lock (ListenersSyncRoot) {
370 foreach (TraceListener listener in Listeners) {
371 listener.WriteLine (value, category);
379 public static void WriteLine (string message, string category)
381 lock (ListenersSyncRoot) {
382 foreach (TraceListener listener in Listeners) {
383 listener.WriteLine (message, category);
391 public static void WriteLineIf (bool condition, object value)
397 public static void WriteLineIf (bool condition, string message)
403 public static void WriteLineIf (bool condition, object value,
407 WriteLine (value, category);
410 public static void WriteLineIf (bool condition, string message,
414 WriteLine (message, category);