Added IDataReceiverSink and refactored to separate the EventSink from the GarbageData...
[lwes-dotnet/github-mirror.git] / Org.Lwes / Trace / Traceable.cs
blobc1ca75c7f35050a57ee04dc1ed8a77352b1b38d3
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
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 /// <summary>
37 /// Diagnostics utility.
38 /// </summary>
39 public static class Traceable
41 #region Fields
43 /// <summary>
44 /// Trace ID used when none was given.
45 /// </summary>
46 public static int DefaultTraceEventID = 0;
48 static Guid __traceProcessGuid = Guid.NewGuid();
49 static String __traceEnvironment;
50 static String __traceComponent;
51 static String __traceApplication;
52 static String __traceDefaultSource;
53 static int __traceOsProcess = -1;
54 private static Object __lock = new Object();
55 private static Dictionary<string, ITraceSourceFilter> __filters = new Dictionary<string, ITraceSourceFilter>();
57 #endregion Fields
59 #region Constructors
61 static Traceable()
63 LwesConfigurationSection config = LwesConfigurationSection.Current;
64 DiagnosticsConfigurationElement diag = config.Diagnostics ?? new DiagnosticsConfigurationElement();
66 __traceEnvironment = diag.Environment;
67 __traceComponent = diag.Component;
68 __traceDefaultSource = diag.DefaultTraceSource;
69 __traceApplication = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
70 try
72 __traceOsProcess = Process.GetCurrentProcess().Id;
74 catch (SecurityException)
75 { // Not run with fulltrust, can't get the process Id.
79 #endregion Constructors
81 #region Methods
83 /// <summary>
84 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
85 /// </summary>
86 /// <typeparam name="T">source type T</typeparam>
87 /// <param name="source">source object</param>
88 /// <param name="eventType">event type</param>
89 /// <param name="data">trace data</param>
90 public static void TraceData<T>(this T source, TraceEventType eventType, object data)
91 where T : ITraceable
93 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, data);
96 /// <summary>
97 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
98 /// </summary>
99 /// <typeparam name="T">source type T</typeparam>
100 /// <param name="source">source object</param>
101 /// <param name="eventType">event type</param>
102 /// <param name="dataGenerator">delegate that generates trace data; only called if the
103 /// <paramref name="eventType"/> is currently being traced</param>
104 public static void TraceData<T>(this T source, TraceEventType eventType, Func<object[]> dataGenerator)
105 where T : ITraceable
107 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, dataGenerator());
110 /// <summary>
111 /// Traces data for a source.
112 /// </summary>
113 /// <typeparam name="T">source type T</typeparam>
114 /// <param name="source">the source of the data</param>
115 /// <param name="eventType">type of trace</param>
116 /// <param name="id">an integer ID for the event</param>
117 /// <param name="data">the data</param>
118 public static void TraceData<T>(this T source, TraceEventType eventType, int id, object data)
119 where T : ITraceable
121 TraceAdapter<T>.Filter.TraceData(eventType, id, data);
124 /// <summary>
125 /// Traces data for a source.
126 /// </summary>
127 /// <typeparam name="T">source type T</typeparam>
128 /// <param name="source">the source of the data</param>
129 /// <param name="eventType">type of trace</param>
130 /// <param name="data">the data</param>
131 public static void TraceData<T>(this T source, TraceEventType eventType, params object[] data)
132 where T : ITraceable
134 TraceAdapter<T>.Filter.TraceData(eventType, DefaultTraceEventID, data);
137 /// <summary>
138 /// Traces data for a source.
139 /// </summary>
140 /// <typeparam name="T">source type T</typeparam>
141 /// <param name="source">the source of the data</param>
142 /// <param name="eventType">type of trace</param>
143 /// <param name="id">an integer ID for the event</param>
144 /// <param name="data">the data</param>
145 public static void TraceData<T>(this T source, TraceEventType eventType, int id, params object[] data)
146 where T : ITraceable
148 TraceAdapter<T>.Filter.TraceData(eventType, id, data);
151 /// <summary>
152 /// Traces data for a source type (for tracing within static methods).
153 /// </summary>
154 /// <param name="sourceType">source type</param>
155 /// <param name="eventType">type of trace</param>
156 /// <param name="data">the data</param>
157 public static void TraceData(Type sourceType, TraceEventType eventType, object data)
159 if (sourceType == null) throw new ArgumentNullException("sourceType");
161 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
162 if (f.ShouldTrace(eventType))
164 f.TraceData(eventType, DefaultTraceEventID, data);
168 /// <summary>
169 /// Traces data for a source type (for tracing within static methods).
170 /// </summary>
171 /// <param name="sourceType">source type</param>
172 /// <param name="eventType">type of trace</param>
173 /// <param name="dataGenerator">delegate that generates data; only called if the
174 /// <paramref name="eventType"/> is currently being traced</param>
175 public static void TraceData(Type sourceType, TraceEventType eventType, Func<object[]> dataGenerator)
177 if (sourceType == null) throw new ArgumentNullException("sourceType");
179 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
180 if (f.ShouldTrace(eventType))
182 f.TraceData(eventType, DefaultTraceEventID, dataGenerator());
186 /// <summary>
187 /// Traces data for a source type (for tracing within static methods).
188 /// </summary>
189 /// <param name="sourceType">source type</param>
190 /// <param name="eventType">type of trace</param>
191 /// <param name="id">an integer ID for the event</param>
192 /// <param name="data">the data</param>
193 public static void TraceData(Type sourceType, TraceEventType eventType, int id, object data)
195 if (sourceType == null) throw new ArgumentNullException("sourceType");
197 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
198 if (f.ShouldTrace(eventType))
200 f.TraceData(eventType, id, data);
204 /// <summary>
205 /// Traces data for a source type (for tracing within static methods).
206 /// </summary>
207 /// <param name="sourceType">source type</param>
208 /// <param name="eventType">type of trace</param>
209 /// <param name="data">the data</param>
210 public static void TraceData(Type sourceType, TraceEventType eventType, params object[] data)
212 if (sourceType == null) throw new ArgumentNullException("sourceType");
214 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
215 if (f.ShouldTrace(eventType))
217 f.TraceData(eventType, DefaultTraceEventID, data);
221 /// <summary>
222 /// Traces data for a source type (for tracing within static methods).
223 /// </summary>
224 /// <param name="sourceType">source type</param>
225 /// <param name="eventType">type of trace</param>
226 /// <param name="id">an integer ID for the event</param>
227 /// <param name="data">the data</param>
228 public static void TraceData(Type sourceType, TraceEventType eventType, int id, params object[] data)
230 if (sourceType == null) throw new ArgumentNullException("sourceType");
232 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
233 if (f.ShouldTrace(eventType))
235 f.TraceData(eventType, id, data);
239 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id)
240 where T : ITraceable
242 TraceAdapter<T>.Filter.TraceEvent(eventType, id);
245 public static void TraceEvent<T>(this T source, TraceEventType eventType, string message)
246 where T : ITraceable
248 TraceAdapter<T>.Filter.TraceEvent(eventType, DefaultTraceEventID, message);
251 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string message)
252 where T : ITraceable
254 TraceAdapter<T>.Filter.TraceEvent(eventType, id, message);
257 public static void TraceEvent<T>(this T source, TraceEventType eventType, string format, params object[] args)
258 where T : ITraceable
260 TraceAdapter<T>.Filter.TraceEvent(eventType, DefaultTraceEventID, format, args);
263 public static void TraceEvent<T>(this T source, TraceEventType eventType, int id, string format, params object[] args)
264 where T : ITraceable
266 TraceAdapter<T>.Filter.TraceEvent(eventType, id, format, args);
269 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id)
271 if (sourceType == null) throw new ArgumentNullException("sourceType");
273 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
274 if (f.ShouldTrace(eventType))
276 f.TraceEvent(eventType, id);
280 public static void TraceEvent(Type sourceType, TraceEventType eventType, string message)
282 if (sourceType == null) throw new ArgumentNullException("sourceType");
284 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
285 if (f.ShouldTrace(eventType))
287 f.TraceEvent(eventType, DefaultTraceEventID, message);
291 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id, string message)
293 if (sourceType == null) throw new ArgumentNullException("sourceType");
295 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
296 if (f.ShouldTrace(eventType))
298 f.TraceEvent(eventType, id, message);
302 public static void TraceEvent(Type sourceType, TraceEventType eventType, string format, params object[] args)
304 if (sourceType == null) throw new ArgumentNullException("sourceType");
306 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
307 if (f.ShouldTrace(eventType))
309 f.TraceEvent(eventType, DefaultTraceEventID, format, args);
313 public static void TraceEvent(Type sourceType, TraceEventType eventType, int id, string format, params object[] args)
315 if (sourceType == null) throw new ArgumentNullException("sourceType");
317 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
318 if (f.ShouldTrace(eventType))
320 f.TraceEvent(eventType, id, format, args);
324 public static void TraceInformation<T>(this T source, string message)
325 where T : ITraceable
327 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Information, DefaultTraceEventID, message);
330 public static void TraceInformation<T>(this T source, string format, params object[] args)
331 where T : ITraceable
333 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Information, DefaultTraceEventID, format, args);
336 public static void TraceInformation(Type sourceType, string message)
338 if (sourceType == null) throw new ArgumentNullException("sourceType");
340 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
341 if (f.ShouldTrace(TraceEventType.Information))
343 f.TraceEvent(TraceEventType.Information, DefaultTraceEventID, message);
347 public static void TraceInformation(Type sourceType, string format, params object[] args)
349 if (sourceType == null) throw new ArgumentNullException("sourceType");
351 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
352 if (f.ShouldTrace(TraceEventType.Information))
354 f.TraceEvent(TraceEventType.Information, DefaultTraceEventID, format, args);
358 public static void TraceTransfer<T>(this T source, string message, Guid relatedActivityId)
359 where T : ITraceable
361 TraceAdapter<T>.Filter.TraceTransfer(DefaultTraceEventID, message, relatedActivityId);
364 public static void TraceTransfer<T>(this T source, int id, string message, Guid relatedActivityId)
365 where T : ITraceable
367 TraceAdapter<T>.Filter.TraceTransfer(id, message, relatedActivityId);
370 public static void TraceTransfer(Type sourceType, string message, Guid relatedActivityId)
372 if (sourceType == null) throw new ArgumentNullException("sourceType");
374 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
375 if (f.ShouldTrace(TraceEventType.Transfer))
377 f.TraceTransfer(DefaultTraceEventID, message, relatedActivityId);
381 public static void TraceTransfer(Type sourceType, int id, string message, Guid relatedActivityId)
383 if (sourceType == null) throw new ArgumentNullException("sourceType");
385 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
386 if (f.ShouldTrace(TraceEventType.Transfer))
388 f.TraceTransfer(id, message, relatedActivityId);
392 public static void TraceVerbose<T>(this T source, string message)
393 where T : ITraceable
395 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, message);
398 public static void TraceVerbose<T>(this T source, int id, string message)
399 where T : ITraceable
401 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, id, message);
404 public static void TraceVerbose<T>(this T source, string format, params object[] args)
405 where T : ITraceable
407 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, format, args);
410 public static void TraceVerbose<T>(this T source, int id, string format, params object[] args)
411 where T : ITraceable
413 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Verbose, id, format, args);
416 public static void TraceVerbose(Type sourceType, string message)
418 if (sourceType == null) throw new ArgumentNullException("sourceType");
420 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
421 if (f.ShouldTrace(TraceEventType.Verbose))
423 f.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, message);
427 public static void TraceVerbose(Type sourceType, int id, string message)
429 if (sourceType == null) throw new ArgumentNullException("sourceType");
431 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
432 if (f.ShouldTrace(TraceEventType.Verbose))
434 f.TraceEvent(TraceEventType.Verbose, id, message);
438 public static void TraceVerbose(Type sourceType, string format, params object[] args)
440 if (sourceType == null) throw new ArgumentNullException("sourceType");
442 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
443 if (f.ShouldTrace(TraceEventType.Verbose))
445 f.TraceEvent(TraceEventType.Verbose, DefaultTraceEventID, format, args);
449 public static void TraceVerbose(Type sourceType, int id, string format, params object[] args)
451 if (sourceType == null) throw new ArgumentNullException("sourceType");
453 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
454 if (f.ShouldTrace(TraceEventType.Verbose))
456 f.TraceEvent(TraceEventType.Verbose, id, format, args);
460 public static void TraceWarning<T>(this T source, string message)
461 where T : ITraceable
463 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, message);
466 public static void TraceWarning<T>(this T source, int id, string message)
467 where T : ITraceable
469 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, id, message);
472 public static void TraceWarning<T>(this T source, string format, params object[] args)
473 where T : ITraceable
475 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, format, args);
478 public static void TraceWarning<T>(this T source, int id, string format, params object[] args)
479 where T : ITraceable
481 TraceAdapter<T>.Filter.TraceEvent(TraceEventType.Warning, id, format, args);
484 public static void TraceWarning(Type sourceType, string message)
486 if (sourceType == null) throw new ArgumentNullException("sourceType");
488 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
489 if (f.ShouldTrace(TraceEventType.Warning))
491 f.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, message);
495 public static void TraceWarning(Type sourceType, int id, string message)
497 if (sourceType == null) throw new ArgumentNullException("sourceType");
499 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
500 if (f.ShouldTrace(TraceEventType.Warning))
502 f.TraceEvent(TraceEventType.Warning, id, message);
506 public static void TraceWarning(Type sourceType, string format, params object[] args)
508 if (sourceType == null) throw new ArgumentNullException("sourceType");
510 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
511 if (f.ShouldTrace(TraceEventType.Warning))
513 f.TraceEvent(TraceEventType.Warning, DefaultTraceEventID, format, args);
517 public static void TraceWarning(Type sourceType, int id, string format, params object[] args)
519 if (sourceType == null) throw new ArgumentNullException("sourceType");
521 ITraceSourceFilter f = AcquireSourceFilter(sourceType);
522 if (f.ShouldTrace(TraceEventType.Warning))
524 f.TraceEvent(TraceEventType.Warning, id, format, args);
528 internal static ITraceSourceFilter AcquireSourceFilter(Type t)
530 if (t == null) throw new ArgumentNullException("t");
532 ITraceSourceFilter result;
533 string key = String.Intern(t.Namespace);
535 lock (__lock)
537 if (!__filters.TryGetValue(key, out result))
539 Stack<string> keys = new Stack<string>();
540 TraceSource src = new TraceSource(key);
541 keys.Push(key);
542 while (src.Switch.DisplayName == key)
544 int i = key.LastIndexOf('.');
545 if (i <= 0) break;
546 key = String.Intern(key.Substring(0, i));
547 if (__filters.TryGetValue(key, out result))
549 src = result.Source;
550 break;
552 keys.Push(key);
553 src = new TraceSource(key);
555 switch (src.Switch.Level)
557 case SourceLevels.ActivityTracing:
558 case SourceLevels.All:
559 result = new AllSourceFilter(src);
560 break;
561 case SourceLevels.Critical:
562 result = new CriticalSourceFilter(src);
563 break;
564 case SourceLevels.Error:
565 result = new ErrorSourceFilter(src);
566 break;
567 case SourceLevels.Information:
568 result = new InformationSourceFilter(src);
569 break;
570 case SourceLevels.Verbose:
571 result = new VerboseSourceFilter(src);
572 break;
573 case SourceLevels.Warning:
574 result = new WarningSourceFilter(src);
575 break;
576 case SourceLevels.Off:
577 default:
578 result = new NullSourceFilter(src);
579 break;
581 foreach (string kk in keys)
583 __filters.Add(kk, result);
587 return result;
590 #endregion Methods
592 #region Nested Types
594 internal static class TraceAdapter<T>
595 where T : ITraceable
597 #region Fields
599 private static ITraceSourceFilter __filter;
601 #endregion Fields
603 #region Constructors
605 static TraceAdapter()
607 __filter = Traceable.AcquireSourceFilter(typeof(T));
610 #endregion Constructors
612 #region Properties
614 internal static ITraceSourceFilter Filter
616 get { return __filter; }
619 #endregion Properties
622 #endregion Nested Types