Added LwesBinaryInstaller0.1.486.19070.msi
[lwes-dotnet/github-mirror.git] / Org.Lwes / Trace / Traceable.cs
blobc762e791578881604e8b32dc68b458d5a3539b3a
1 #region Header
3 //
4 // This file is part of the LWES .NET Binding (LWES.net)
5 //
6 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
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 // Lesser 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.Trace
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.Filters;
35 /// <summary>
36 /// Diagnostics utility.
37 /// </summary>
38 public static class Traceable
40 #region Fields
42 /// <summary>
43 /// Trace ID used when none was given.
44 /// </summary>
45 public static int DefaultTraceEventID = 0;
47 static Guid __traceProcessGuid = Guid.NewGuid();
48 static String __traceEnvironment;
49 static String __traceComponent;
50 static String __traceApplication;
51 static String __traceDefaultSource;
52 static int __traceOsProcess = -1;
53 private static Object __lock = new Object();
54 private static Dictionary<string, ITraceSourceFilter> __filters = new Dictionary<string, ITraceSourceFilter>();
56 #endregion Fields
58 #region Constructors
60 static Traceable()
62 LwesConfigurationSection config = LwesConfigurationSection.Current;
63 DiagnosticsConfigurationElement diag = config.Diagnostics ?? new DiagnosticsConfigurationElement();
65 __traceEnvironment = diag.Environment;
66 __traceComponent = diag.Component;
67 __traceDefaultSource = diag.DefaultTraceSource;
68 __traceApplication = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
69 try
71 __traceOsProcess = Process.GetCurrentProcess().Id;
73 catch (SecurityException)
74 { // Not run with fulltrust, can't get the process Id.
78 #endregion Constructors
80 #region Methods
82 /// <summary>
83 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
84 /// </summary>
85 /// <typeparam name="T">source type T</typeparam>
86 /// <param name="source">source object</param>
87 /// <param name="eventType">event type</param>
88 /// <param name="data">trace data</param>
89 public static void TraceData<T>(this T source, TraceEventType eventType, object data)
90 where T : ITraceable
92 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, data);
95 /// <summary>
96 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
97 /// </summary>
98 /// <typeparam name="T">source type T</typeparam>
99 /// <param name="source">source object</param>
100 /// <param name="eventType">event type</param>
101 /// <param name="dataGenerator">delegate that generates trace data; only called if the
102 /// <paramref name="eventType"/> is currently being traced</param>
103 public static void TraceData<T>(this T source, TraceEventType eventType, Func<object[]> dataGenerator)
104 where T : ITraceable
106 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, dataGenerator());
109 /// <summary>
110 /// Traces data for a source.
111 /// </summary>
112 /// <typeparam name="T">source type T</typeparam>
113 /// <param name="source">the source of the data</param>
114 /// <param name="eventType">type of trace</param>
115 /// <param name="id">an integer ID for the event</param>
116 /// <param name="data">the data</param>
117 public static void TraceData<T>(this T source, TraceEventType eventType, int id, object data)
118 where T : ITraceable
120 TraceAdapter<T>.Filter.TraceData(eventType, id, data);
123 /// <summary>
124 /// Traces data for a source.
125 /// </summary>
126 /// <typeparam name="T">source type T</typeparam>
127 /// <param name="source">the source of the data</param>
128 /// <param name="eventType">type of trace</param>
129 /// <param name="data">the data</param>
130 public static void TraceData<T>(this T source, TraceEventType eventType, params object[] data)
131 where T : ITraceable
133 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, data);
136 /// <summary>
137 /// Traces data for a source.
138 /// </summary>
139 /// <typeparam name="T">source type T</typeparam>
140 /// <param name="source">the source of the data</param>
141 /// <param name="eventType">type of trace</param>
142 /// <param name="id">an integer ID for the event</param>
143 /// <param name="data">the data</param>
144 public static void TraceData<T>(this T source, TraceEventType eventType, int id, params object[] data)
145 where T : ITraceable
147 TraceAdapter<T>.Filter.TraceData(eventType, id, data);
150 /// <summary>
151 /// Traces data for a source type (for tracing within static methods).
152 /// </summary>
153 /// <param name="sourceType">source type</param>
154 /// <param name="eventType">type of trace</param>
155 /// <param name="data">the data</param>
156 public static void TraceData(Type sourceType, TraceEventType eventType, object data)
158 if (sourceType == null) throw new ArgumentNullException("sourceType");
160 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
161 if (f.ShouldTrace(eventType))
163 f.TraceData(eventType, DefaultTraceEventID, data);
167 /// <summary>
168 /// Traces data for a source type (for tracing within static methods).
169 /// </summary>
170 /// <param name="sourceType">source type</param>
171 /// <param name="eventType">type of trace</param>
172 /// <param name="dataGenerator">delegate that generates data; only called if the
173 /// <paramref name="eventType"/> is currently being traced</param>
174 public static void TraceData(Type sourceType, TraceEventType eventType, Func<object[]> dataGenerator)
176 if (sourceType == null) throw new ArgumentNullException("sourceType");
178 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
179 if (f.ShouldTrace(eventType))
181 f.TraceData(eventType, DefaultTraceEventID, dataGenerator());
185 /// <summary>
186 /// Traces data for a source type (for tracing within static methods).
187 /// </summary>
188 /// <param name="sourceType">source type</param>
189 /// <param name="eventType">type of trace</param>
190 /// <param name="id">an integer ID for the event</param>
191 /// <param name="data">the data</param>
192 public static void TraceData(Type sourceType, TraceEventType eventType, int id, object data)
194 if (sourceType == null) throw new ArgumentNullException("sourceType");
196 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
197 if (f.ShouldTrace(eventType))
199 f.TraceData(eventType, id, data);
203 /// <summary>
204 /// Traces data for a source type (for tracing within static methods).
205 /// </summary>
206 /// <param name="sourceType">source type</param>
207 /// <param name="eventType">type of trace</param>
208 /// <param name="data">the data</param>
209 public static void TraceData(Type sourceType, TraceEventType eventType, params object[] data)
211 if (sourceType == null) throw new ArgumentNullException("sourceType");
213 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
214 if (f.ShouldTrace(eventType))
216 f.TraceData(eventType, DefaultTraceEventID, data);
220 /// <summary>
221 /// Traces data for a source type (for tracing within static methods).
222 /// </summary>
223 /// <param name="sourceType">source type</param>
224 /// <param name="eventType">type of trace</param>
225 /// <param name="id">an integer ID for the event</param>
226 /// <param name="data">the data</param>
227 public static void TraceData(Type sourceType, TraceEventType eventType, int id, params object[] data)
229 if (sourceType == null) throw new ArgumentNullException("sourceType");
231 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
232 if (f.ShouldTrace(eventType))
234 f.TraceData(eventType, id, data);
238 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id)
239 where T : ITraceable
241 TraceAdapter<T>.Filter.TraceEvent(eventType, id);
244 public static void TraceEvent<T>(this T source, TraceEventType eventType, string message)
245 where T : ITraceable
247 TraceAdapter<T>.Filter.TraceEvent(eventType, DefaultTraceEventID, message);
250 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string message)
251 where T : ITraceable
253 TraceAdapter<T>.Filter.TraceEvent(eventType, id, message);
256 public static void TraceEvent<T>(this T source, TraceEventType eventType, string format, params object[] args)
257 where T : ITraceable
259 TraceAdapter<T>.Filter.TraceEvent(eventType, DefaultTraceEventID, format, args);
262 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string format, params object[] args)
263 where T : ITraceable
265 TraceAdapter<T>.Filter.TraceEvent(eventType, id, format, args);
268 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id)
270 if (sourceType == null) throw new ArgumentNullException("sourceType");
272 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
273 if (f.ShouldTrace(eventType))
275 f.TraceEvent(eventType, id);
279 public static void TraceEvent(Type sourceType, TraceEventType eventType, string message)
281 if (sourceType == null) throw new ArgumentNullException("sourceType");
283 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
284 if (f.ShouldTrace(eventType))
286 f.TraceEvent(eventType, DefaultTraceEventID, message);
290 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id, string message)
292 if (sourceType == null) throw new ArgumentNullException("sourceType");
294 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
295 if (f.ShouldTrace(eventType))
297 f.TraceEvent(eventType, id, message);
301 public static void TraceEvent(Type sourceType, TraceEventType eventType, string format, params object[] args)
303 if (sourceType == null) throw new ArgumentNullException("sourceType");
305 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
306 if (f.ShouldTrace(eventType))
308 f.TraceEvent(eventType, DefaultTraceEventID, format, args);
312 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id, string format, params object[] args)
314 if (sourceType == null) throw new ArgumentNullException("sourceType");
316 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
317 if (f.ShouldTrace(eventType))
319 f.TraceEvent(eventType, id, format, args);
323 public static void TraceInformation<T>(this T source, string message)
324 where T : ITraceable
326 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Information, DefaultTraceEventID, message);
329 public static void TraceInformation<T>(this T source, string format, params object[] args)
330 where T : ITraceable
332 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Information, DefaultTraceEventID, format, args);
335 public static void TraceInformation(Type sourceType, string message)
337 if (sourceType == null) throw new ArgumentNullException("sourceType");
339 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
340 if (f.ShouldTrace(TraceEventType.Information))
342 f.TraceEvent(TraceEventType.Information, DefaultTraceEventID, message);
346 public static void TraceInformation(Type sourceType, string format, params object[] args)
348 if (sourceType == null) throw new ArgumentNullException("sourceType");
350 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
351 if (f.ShouldTrace(TraceEventType.Information))
353 f.TraceEvent(TraceEventType.Information, DefaultTraceEventID, format, args);
357 public static void TraceTransfer<T>(this T source, string message, Guid relatedActivityId)
358 where T : ITraceable
360 TraceAdapter<T>.Filter.TraceTransfer(DefaultTraceEventID, message, relatedActivityId);
363 public static void TraceTransfer<T>(this T source, int id, string message, Guid relatedActivityId)
364 where T : ITraceable
366 TraceAdapter<T>.Filter.TraceTransfer(id, message, relatedActivityId);
369 public static void TraceTransfer(Type sourceType, string message, Guid relatedActivityId)
371 if (sourceType == null) throw new ArgumentNullException("sourceType");
373 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
374 if (f.ShouldTrace(TraceEventType.Transfer))
376 f.TraceTransfer(DefaultTraceEventID, message, relatedActivityId);
380 public static void TraceTransfer(Type sourceType, int id, string message, Guid relatedActivityId)
382 if (sourceType == null) throw new ArgumentNullException("sourceType");
384 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
385 if (f.ShouldTrace(TraceEventType.Transfer))
387 f.TraceTransfer(id, message, relatedActivityId);
391 public static void TraceVerbose<T>(this T source, string message)
392 where T : ITraceable
394 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, message);
397 public static void TraceVerbose<T>(this T source, int id, string message)
398 where T : ITraceable
400 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, id, message);
403 public static void TraceVerbose<T>(this T source, string format, params object[] args)
404 where T : ITraceable
406 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, format, args);
409 public static void TraceVerbose<T>(this T source, int id, string format, params object[] args)
410 where T : ITraceable
412 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, id, format, args);
415 public static void TraceVerbose(Type sourceType, string message)
417 if (sourceType == null) throw new ArgumentNullException("sourceType");
419 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
420 if (f.ShouldTrace(TraceEventType.Verbose))
422 f.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, message);
426 public static void TraceVerbose(Type sourceType, int id, string message)
428 if (sourceType == null) throw new ArgumentNullException("sourceType");
430 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
431 if (f.ShouldTrace(TraceEventType.Verbose))
433 f.TraceEvent(TraceEventType.Verbose, id, message);
437 public static void TraceVerbose(Type sourceType, string format, params object[] args)
439 if (sourceType == null) throw new ArgumentNullException("sourceType");
441 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
442 if (f.ShouldTrace(TraceEventType.Verbose))
444 f.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, format, args);
448 public static void TraceVerbose(Type sourceType, int id, string format, params object[] args)
450 if (sourceType == null) throw new ArgumentNullException("sourceType");
452 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
453 if (f.ShouldTrace(TraceEventType.Verbose))
455 f.TraceEvent(TraceEventType.Verbose, id, format, args);
459 public static void TraceWarning<T>(this T source, string message)
460 where T : ITraceable
462 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, message);
465 public static void TraceWarning<T>(this T source, int id, string message)
466 where T : ITraceable
468 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, id, message);
471 public static void TraceWarning<T>(this T source, string format, params object[] args)
472 where T : ITraceable
474 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, format, args);
477 public static void TraceWarning<T>(this T source, int id, string format, params object[] args)
478 where T : ITraceable
480 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, id, format, args);
483 public static void TraceWarning(Type sourceType, string message)
485 if (sourceType == null) throw new ArgumentNullException("sourceType");
487 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
488 if (f.ShouldTrace(TraceEventType.Warning))
490 f.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, message);
494 public static void TraceWarning(Type sourceType, int id, string message)
496 if (sourceType == null) throw new ArgumentNullException("sourceType");
498 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
499 if (f.ShouldTrace(TraceEventType.Warning))
501 f.TraceEvent(TraceEventType.Warning, id, message);
505 public static void TraceWarning(Type sourceType, string format, params object[] args)
507 if (sourceType == null) throw new ArgumentNullException("sourceType");
509 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
510 if (f.ShouldTrace(TraceEventType.Warning))
512 f.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, format, args);
516 public static void TraceWarning(Type sourceType, int id, string format, params object[] args)
518 if (sourceType == null) throw new ArgumentNullException("sourceType");
520 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
521 if (f.ShouldTrace(TraceEventType.Warning))
523 f.TraceEvent(TraceEventType.Warning, id, format, args);
527 internal static ITraceSourceFilter AcquireSourceFilter(Type t)
529 if (t == null) throw new ArgumentNullException("t");
531 ITraceSourceFilter result;
532 string key = String.Intern(t.Namespace);
534 lock (__lock)
536 if (!__filters.TryGetValue(key, out result))
538 Stack<string> keys = new Stack<string>();
539 TraceSource src = new TraceSource(key);
540 keys.Push(key);
541 while (src.Switch.DisplayName == key)
543 int i = key.LastIndexOf('.');
544 if (i <= 0) break;
545 key = String.Intern(key.Substring(0, i));
546 if (__filters.TryGetValue(key, out result))
548 src = result.Source;
549 break;
551 keys.Push(key);
552 src = new TraceSource(key);
554 switch (src.Switch.Level)
556 case SourceLevels.ActivityTracing:
557 case SourceLevels.All:
558 result = new AllSourceFilter(src);
559 break;
560 case SourceLevels.Critical:
561 result = new CriticalSourceFilter(src);
562 break;
563 case SourceLevels.Error:
564 result = new ErrorSourceFilter(src);
565 break;
566 case SourceLevels.Information:
567 result = new InformationSourceFilter(src);
568 break;
569 case SourceLevels.Verbose:
570 result = new VerboseSourceFilter(src);
571 break;
572 case SourceLevels.Warning:
573 result = new WarningSourceFilter(src);
574 break;
575 case SourceLevels.Off:
576 default:
577 result = new NullSourceFilter(src);
578 break;
580 foreach (string kk in keys)
582 __filters.Add(kk, result);
586 return result;
589 #endregion Methods
591 #region Nested Types
593 internal static class TraceAdapter<T>
594 where T : ITraceable
596 #region Fields
598 private static ITraceSourceFilter __filter;
600 #endregion Fields
602 #region Constructors
604 static TraceAdapter()
606 __filter = Traceable.AcquireSourceFilter(typeof(T));
609 #endregion Constructors
611 #region Properties
613 internal static ITraceSourceFilter Filter
615 get { return __filter; }
618 #endregion Properties
621 #endregion Nested Types