4 // This file is part of the LWES .NET Binding (LWES.net)
6 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
7 // original .NET implementation
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/>.
28 using System
.Collections
.Generic
;
29 using System
.Diagnostics
;
30 using System
.Security
;
32 using Org
.Lwes
.Config
;
34 using Org
.Lwes
.Trace
.Filters
;
37 /// Diagnostics utility.
39 public static class Traceable
44 /// Trace ID used when none was given.
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
>();
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
;
72 __traceOsProcess
= Process
.GetCurrentProcess().Id
;
74 catch (SecurityException
)
75 { // Not run with fulltrust, can't get the process Id.
79 #endregion Constructors
84 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
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
)
93 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
97 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
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
)
107 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, dataGenerator());
111 /// Traces data for a source.
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
)
121 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
125 /// Traces data for a source.
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
)
134 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
138 /// Traces data for a source.
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
)
148 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
152 /// Traces data for a source type (for tracing within static methods).
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
);
169 /// Traces data for a source type (for tracing within static methods).
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());
187 /// Traces data for a source type (for tracing within static methods).
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
);
205 /// Traces data for a source type (for tracing within static methods).
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
);
222 /// Traces data for a source type (for tracing within static methods).
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
)
242 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
);
245 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string message
)
248 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, DefaultTraceEventID
, message
);
251 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, int id
, string message
)
254 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
, message
);
257 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string format
, params object[] args
)
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
)
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
)
327 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, message
);
330 public static void TraceInformation
<T
>(this T source
, string format
, params object[] args
)
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
)
361 TraceAdapter
<T
>.Filter
.TraceTransfer(DefaultTraceEventID
, message
, relatedActivityId
);
364 public static void TraceTransfer
<T
>(this T source
, int id
, string message
, Guid relatedActivityId
)
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
)
395 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, message
);
398 public static void TraceVerbose
<T
>(this T source
, int id
, string message
)
401 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, id
, message
);
404 public static void TraceVerbose
<T
>(this T source
, string format
, params object[] args
)
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
)
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
)
463 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, message
);
466 public static void TraceWarning
<T
>(this T source
, int id
, string message
)
469 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, id
, message
);
472 public static void TraceWarning
<T
>(this T source
, string format
, params object[] args
)
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
)
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
);
537 if (!__filters
.TryGetValue(key
, out result
))
539 Stack
<string> keys
= new Stack
<string>();
540 TraceSource src
= new TraceSource(key
);
542 while (src
.Switch
.DisplayName
== key
)
544 int i
= key
.LastIndexOf('.');
546 key
= String
.Intern(key
.Substring(0, i
));
547 if (__filters
.TryGetValue(key
, out result
))
553 src
= new TraceSource(key
);
555 switch (src
.Switch
.Level
)
557 case SourceLevels
.ActivityTracing
:
558 case SourceLevels
.All
:
559 result
= new AllSourceFilter(src
);
561 case SourceLevels
.Critical
:
562 result
= new CriticalSourceFilter(src
);
564 case SourceLevels
.Error
:
565 result
= new ErrorSourceFilter(src
);
567 case SourceLevels
.Information
:
568 result
= new InformationSourceFilter(src
);
570 case SourceLevels
.Verbose
:
571 result
= new VerboseSourceFilter(src
);
573 case SourceLevels
.Warning
:
574 result
= new WarningSourceFilter(src
);
576 case SourceLevels
.Off
:
578 result
= new NullSourceFilter(src
);
581 foreach (string kk
in keys
)
583 __filters
.Add(kk
, result
);
594 internal static class TraceAdapter
<T
>
599 private static ITraceSourceFilter __filter
;
605 static TraceAdapter()
607 __filter
= Traceable
.AcquireSourceFilter(typeof(T
));
610 #endregion Constructors
614 internal static ITraceSourceFilter Filter
616 get { return __filter; }
619 #endregion Properties
622 #endregion Nested Types