Slight edit to copyright statement. Added Diagnostics utility and extension methods...
[lwes-dotnet/github-mirror.git] / Org.Lwes / Diagnostics.cs
bloba779d1a3dc7c9148ca981bc15a6492b64d09c917
1 #region Header
3 //
4 // This file is part of the LWES .NET Binding (LWES.net)
5 //
6 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
7 // original .NET implementation
8 //
9 // LWES.net is free software: you can redistribute it and/or modify
10 // it under the terms of the Lesser GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // LWES.net is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the Lesser GNU General Public License
20 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
23 #endregion Header
25 namespace Org.Lwes
27 using System;
28 using System.Collections.Generic;
29 using System.Diagnostics;
30 using System.Security;
32 using Org.Lwes.Config;
33 using Org.Lwes.Trace;
34 using Org.Lwes.Trace.Filters;
36 public static class Diagnostics
38 #region Fields
40 public static readonly string CDiagnosticsConfigSectionName = "diagnostics";
42 public static int DefaultTraceEventID = 0;
44 static Guid __traceProcessGuid = Guid.NewGuid();
45 static String __traceEnvironment;
46 static String __traceComponent;
47 static String __traceApplication;
48 static String __traceDefaultSource;
49 static int __traceOsProcess = -1;
50 private static Object __lock = new Object();
51 private static Dictionary<string, ITraceSourceFilter> __filters = new Dictionary<string, ITraceSourceFilter>();
53 #endregion Fields
55 #region Constructors
57 static Diagnostics()
59 LwesConfigurationSection config = LwesConfigurationSection.Current;
60 DiagnosticsConfigurationElement diag = config.Diagnostics ?? new DiagnosticsConfigurationElement();
62 __traceEnvironment = diag.Environment;
63 __traceComponent = diag.Component;
64 __traceDefaultSource = diag.DefaultTraceSource;
65 __traceApplication = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
66 try
68 __traceOsProcess = Process.GetCurrentProcess().Id;
70 catch (SecurityException)
71 { // Not run with fulltrust, can't get the process Id.
75 #endregion Constructors
77 #region Methods
79 public static void TraceData<T>(this T source, TraceEventType eventType, object data)
81 Diagnostics<T>.TraceData(eventType, DefaultTraceEventID, data);
84 public static void TraceData<T>(this T source, TraceEventType eventType, Func<object[]> dataGenerator)
86 if (Diagnostics<T>.ShouldTrace(eventType))
88 Diagnostics<T>.TraceData(eventType, DefaultTraceEventID, dataGenerator());
92 public static void TraceData<T>(this T source, TraceEventType eventType, int id, object data)
94 Diagnostics<T>.TraceData(eventType, id, data);
97 public static void TraceData<T>(this T source, TraceEventType eventType, params object[] data)
99 Diagnostics<T>.TraceData(eventType, DefaultTraceEventID, data);
102 public static void TraceData<T>(this T source, TraceEventType eventType, int id, params object[] data)
104 Diagnostics<T>.TraceData(eventType, id, data);
107 public static void TraceError<T>(this T source, string message)
109 Diagnostics<T>.TraceEvent(TraceEventType.Error, DefaultTraceEventID, message);
112 public static void TraceError<T>(this T source, int id, string message)
114 Diagnostics<T>.TraceEvent(TraceEventType.Error, id, message);
117 public static void TraceError<T>(this T source, string format, params object[] args)
119 Diagnostics<T>.TraceEvent(TraceEventType.Error, DefaultTraceEventID, format, args);
122 public static void TraceError<T>(this T source, int id, string format, params object[] args)
124 Diagnostics<T>.TraceEvent(TraceEventType.Error, id, format, args);
127 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id)
129 Diagnostics<T>.TraceEvent(eventType, id);
132 public static void TraceEvent<T>(this T source, TraceEventType eventType, string message)
134 Diagnostics<T>.TraceEvent(eventType, DefaultTraceEventID, message);
137 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string message)
139 Diagnostics<T>.TraceEvent(eventType, id, message);
142 public static void TraceEvent<T>(this T source, TraceEventType eventType, string format, params object[] args)
144 Diagnostics<T>.TraceEvent(eventType, DefaultTraceEventID, format, args);
147 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string format, params object[] args)
149 Diagnostics<T>.TraceEvent(eventType, id, format, args);
152 public static void TraceInformation<T>(this T source, string message)
154 Diagnostics<T>.TraceEvent(TraceEventType.Information, DefaultTraceEventID, message);
157 public static void TraceInformation<T>(this T source, string format, params object[] args)
159 Diagnostics<T>.TraceEvent(TraceEventType.Information, DefaultTraceEventID, format, args);
162 public static void TraceTransfer<T>(this T source, string message, Guid relatedActivityId)
164 Diagnostics<T>.TraceTransfer(DefaultTraceEventID, message, relatedActivityId);
167 public static void TraceTransfer<T>(this T source, int id, string message, Guid relatedActivityId)
169 Diagnostics<T>.TraceTransfer(id, message, relatedActivityId);
172 public static void TraceVerbose<T>(this T source, string message)
174 Diagnostics<T>.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, message);
177 public static void TraceVerbose<T>(this T source, int id, string message)
179 Diagnostics<T>.TraceEvent(TraceEventType.Verbose, id, message);
182 public static void TraceVerbose<T>(this T source, string format, params object[] args)
184 Diagnostics<T>.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, format, args);
187 public static void TraceVerbose<T>(this T source, int id, string format, params object[] args)
189 Diagnostics<T>.TraceEvent(TraceEventType.Verbose, id, format, args);
192 public static void TraceWarning<T>(this T source, string message)
194 Diagnostics<T>.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, message);
197 public static void TraceWarning<T>(this T source, int id, string message)
199 Diagnostics<T>.TraceEvent(TraceEventType.Warning, id, message);
202 public static void TraceWarning<T>(this T source, string format, params object[] args)
204 Diagnostics<T>.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, format, args);
207 public static void TraceWarning<T>(this T source, int id, string format, params object[] args)
209 Diagnostics<T>.TraceEvent(TraceEventType.Warning, id, format, args);
212 internal static ITraceSourceFilter DiscoverSourceFilter<T>()
214 ITraceSourceFilter result;
215 string key = String.Intern(typeof(T).Namespace);
217 lock (__lock)
219 if (!__filters.TryGetValue(key, out result))
221 Stack<string> keys = new Stack<string>();
222 TraceSource src = new TraceSource(key);
223 keys.Push(key);
224 while (src.Switch.DisplayName == key)
226 int i = key.LastIndexOf('.');
227 if (i <= 0) break;
228 key = String.Intern(key.Substring(0, i));
229 if (__filters.TryGetValue(key, out result))
231 src = result.Source;
232 break;
234 keys.Push(key);
235 src = new TraceSource(key);
237 switch (src.Switch.Level)
239 case SourceLevels.ActivityTracing:
240 case SourceLevels.All:
241 result = new AllSourceFilter(src);
242 break;
243 case SourceLevels.Critical:
244 result = new CriticalSourceFilter(src);
245 break;
246 case SourceLevels.Error:
247 result = new ErrorSourceFilter(src);
248 break;
249 case SourceLevels.Information:
250 result = new InformationSourceFilter(src);
251 break;
252 case SourceLevels.Verbose:
253 result = new VerboseSourceFilter(src);
254 break;
255 case SourceLevels.Warning:
256 result = new WarningSourceFilter(src);
257 break;
258 case SourceLevels.Off:
259 default:
260 result = new NullSourceFilter(src);
261 break;
263 foreach (string kk in keys)
265 __filters.Add(kk, result);
269 return result;
272 #endregion Methods
275 public static class Diagnostics<T>
277 #region Fields
279 const int CDefaultTraceEventID = 0;
281 private static ITraceSourceFilter __filter;
283 #endregion Fields
285 #region Constructors
287 static Diagnostics()
289 __filter = Diagnostics.DiscoverSourceFilter<T>();
292 #endregion Constructors
294 #region Properties
296 public static TraceSource Source
298 get { return __filter.Source; }
301 internal static ITraceSourceFilter Filter
303 get { return __filter; }
306 #endregion Properties
308 #region Methods
310 public static bool ShouldTrace(TraceEventType eventType)
312 return __filter.ShouldTrace(eventType);
315 public static void TraceData(TraceEventType eventType, object data)
317 if (ShouldTrace(eventType))
318 __filter.TraceData(eventType, CDefaultTraceEventID, data);
321 public static void TraceData(TraceEventType eventType, int id, object data)
323 if (ShouldTrace(eventType))
324 __filter.TraceData(eventType, id, data);
327 public static void TraceData(TraceEventType eventType, params object[] data)
329 if (ShouldTrace(eventType))
330 __filter.TraceData(eventType, CDefaultTraceEventID, data);
333 public static void TraceData(TraceEventType eventType, int id, params object[] data)
335 if (ShouldTrace(eventType))
336 __filter.TraceData(eventType, id, data);
339 public static void TraceError(string message)
341 if (ShouldTrace(TraceEventType.Error))
342 __filter.TraceEvent(TraceEventType.Error, CDefaultTraceEventID, message);
345 public static void TraceError(int id, string message)
347 if (ShouldTrace(TraceEventType.Error))
348 __filter.TraceEvent(TraceEventType.Error, id, message);
351 public static void TraceError(string format, params object[] args)
353 if (ShouldTrace(TraceEventType.Error))
354 __filter.TraceEvent(TraceEventType.Error, CDefaultTraceEventID, format, args);
357 public static void TraceError(int id, string format, params object[] args)
359 if (ShouldTrace(TraceEventType.Error))
360 __filter.TraceEvent(TraceEventType.Error, id, format, args);
363 public static void TraceEvent(TraceEventType eventType, int id)
365 if (ShouldTrace(eventType))
366 __filter.TraceEvent(eventType, id);
369 public static void TraceEvent(TraceEventType eventType, string message)
371 if (ShouldTrace(eventType))
372 __filter.TraceEvent(eventType, CDefaultTraceEventID, message);
375 public static void TraceEvent(TraceEventType eventType, int id, string message)
377 if (ShouldTrace(eventType))
378 __filter.TraceEvent(eventType, id, message);
381 public static void TraceEvent(TraceEventType eventType, string format, params object[] args)
383 if (ShouldTrace(eventType))
384 __filter.TraceEvent(eventType, CDefaultTraceEventID, format, args);
387 public static void TraceEvent(TraceEventType eventType, int id, string format, params object[] args)
389 if (ShouldTrace(eventType))
390 __filter.TraceEvent(eventType, id, format, args);
393 public static void TraceInformation(string message)
395 if (ShouldTrace(TraceEventType.Information))
396 __filter.TraceEvent(TraceEventType.Information, CDefaultTraceEventID, message);
399 public static void TraceInformation(string format, params object[] args)
401 if (ShouldTrace(TraceEventType.Information))
402 __filter.TraceEvent(TraceEventType.Information, CDefaultTraceEventID, format, args);
405 public static void TraceTransfer(string message, Guid relatedActivityId)
407 if (ShouldTrace(TraceEventType.Transfer))
408 __filter.TraceTransfer(CDefaultTraceEventID, message, relatedActivityId);
411 public static void TraceTransfer(int id, string message, Guid relatedActivityId)
413 if (ShouldTrace(TraceEventType.Transfer))
414 __filter.TraceTransfer(id, message, relatedActivityId);
417 public static void TraceVerbose(string message)
419 if (ShouldTrace(TraceEventType.Verbose))
420 __filter.TraceEvent(TraceEventType.Verbose, CDefaultTraceEventID, message);
423 public static void TraceVerbose(int id, string message)
425 if (ShouldTrace(TraceEventType.Verbose))
426 __filter.TraceEvent(TraceEventType.Verbose, id, message);
429 public static void TraceVerbose(string format, params object[] args)
431 if (ShouldTrace(TraceEventType.Verbose))
432 __filter.TraceEvent(TraceEventType.Verbose, CDefaultTraceEventID, format, args);
435 public static void TraceVerbose(int id, string format, params object[] args)
437 if (ShouldTrace(TraceEventType.Verbose))
438 __filter.TraceEvent(TraceEventType.Verbose, id, format, args);
441 public static void TraceWarning(string message)
443 if (ShouldTrace(TraceEventType.Warning))
444 __filter.TraceEvent(TraceEventType.Warning, CDefaultTraceEventID, message);
447 public static void TraceWarning(int id, string message)
449 if (ShouldTrace(TraceEventType.Warning))
450 __filter.TraceEvent(TraceEventType.Warning, id, message);
453 public static void TraceWarning(string format, params object[] args)
455 if (ShouldTrace(TraceEventType.Warning))
456 __filter.TraceEvent(TraceEventType.Warning, CDefaultTraceEventID, format, args);
459 public static void TraceWarning(int id, string format, params object[] args)
461 if (ShouldTrace(TraceEventType.Warning))
462 __filter.TraceEvent(TraceEventType.Warning, id, format, args);
465 #endregion Methods