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 Diagnostics
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
83 public static void TraceData
<T
>(this T source
, TraceEventType eventType
, object data
)
86 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
89 public static void TraceData
<T
>(this T source
, TraceEventType eventType
, Func
<object[]> dataGenerator
)
92 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, dataGenerator());
95 public static void TraceData
<T
>(this T source
, TraceEventType eventType
, int id
, object data
)
98 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
101 public static void TraceData
<T
>(this T source
, TraceEventType eventType
, params object[] data
)
104 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
107 public static void TraceData
<T
>(this T source
, TraceEventType eventType
, int id
, params object[] data
)
110 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
113 public static void TraceData(Type sourceType
, TraceEventType eventType
, object data
)
115 if (sourceType
== null) throw new ArgumentNullException("sourceType");
117 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
118 if (f
.ShouldTrace(eventType
))
120 f
.TraceData(eventType
, DefaultTraceEventID
, data
);
124 public static void TraceData(Type sourceType
, TraceEventType eventType
, Func
<object[]> dataGenerator
)
126 if (sourceType
== null) throw new ArgumentNullException("sourceType");
128 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
129 if (f
.ShouldTrace(eventType
))
131 f
.TraceData(eventType
, DefaultTraceEventID
, dataGenerator());
135 public static void TraceData(Type sourceType
, TraceEventType eventType
, int id
, object data
)
137 if (sourceType
== null) throw new ArgumentNullException("sourceType");
139 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
140 if (f
.ShouldTrace(eventType
))
142 f
.TraceData(eventType
, id
, data
);
146 public static void TraceData(Type sourceType
, TraceEventType eventType
, params object[] data
)
148 if (sourceType
== null) throw new ArgumentNullException("sourceType");
150 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
151 if (f
.ShouldTrace(eventType
))
153 f
.TraceData(eventType
, DefaultTraceEventID
, data
);
157 public static void TraceData(Type sourceType
, TraceEventType eventType
, int id
, params object[] data
)
159 if (sourceType
== null) throw new ArgumentNullException("sourceType");
161 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
162 if (f
.ShouldTrace(eventType
))
164 f
.TraceData(eventType
, id
, data
);
168 public static void TraceError
<T
>(this T source
, string message
)
171 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Error
, DefaultTraceEventID
, message
);
174 public static void TraceError
<T
>(this T source
, int id
, string message
)
177 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Error
, id
, message
);
180 public static void TraceError
<T
>(this T source
, string format
, params object[] args
)
183 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Error
, DefaultTraceEventID
, format
, args
);
186 public static void TraceError
<T
>(this T source
, int id
, string format
, params object[] args
)
189 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Error
, id
, format
, args
);
192 public static void TraceError(Type sourceType
, string message
)
194 if (sourceType
== null) throw new ArgumentNullException("sourceType");
196 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
197 if (f
.ShouldTrace(TraceEventType
.Error
))
199 f
.TraceEvent(TraceEventType
.Error
, DefaultTraceEventID
, message
);
203 public static void TraceError(Type sourceType
, int id
, string message
)
205 if (sourceType
== null) throw new ArgumentNullException("sourceType");
207 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
208 if (f
.ShouldTrace(TraceEventType
.Error
))
210 f
.TraceEvent(TraceEventType
.Error
, id
, message
);
214 public static void TraceError(Type sourceType
, string format
, params object[] args
)
216 if (sourceType
== null) throw new ArgumentNullException("sourceType");
218 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
219 if (f
.ShouldTrace(TraceEventType
.Error
))
221 f
.TraceEvent(TraceEventType
.Error
, DefaultTraceEventID
, format
, args
);
225 public static void TraceError(Type sourceType
, int id
, string format
, params object[] args
)
227 if (sourceType
== null) throw new ArgumentNullException("sourceType");
229 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
230 if (f
.ShouldTrace(TraceEventType
.Error
))
232 f
.TraceEvent(TraceEventType
.Error
, id
, format
, args
);
236 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, int id
)
239 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
);
242 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string message
)
245 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, DefaultTraceEventID
, message
);
248 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, int id
, string message
)
251 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
, message
);
254 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string format
, params object[] args
)
257 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, DefaultTraceEventID
, format
, args
);
260 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, int id
, string format
, params object[] args
)
263 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
, format
, args
);
266 public static void TraceEvent(Type sourceType
, TraceEventType eventType
, int id
)
268 if (sourceType
== null) throw new ArgumentNullException("sourceType");
270 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
271 if (f
.ShouldTrace(eventType
))
273 f
.TraceEvent(eventType
, id
);
277 public static void TraceEvent(Type sourceType
, TraceEventType eventType
, string message
)
279 if (sourceType
== null) throw new ArgumentNullException("sourceType");
281 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
282 if (f
.ShouldTrace(eventType
))
284 f
.TraceEvent(eventType
, DefaultTraceEventID
, message
);
288 public static void TraceEvent(Type sourceType
, TraceEventType eventType
, int id
, string message
)
290 if (sourceType
== null) throw new ArgumentNullException("sourceType");
292 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
293 if (f
.ShouldTrace(eventType
))
295 f
.TraceEvent(eventType
, id
, message
);
299 public static void TraceEvent(Type sourceType
, TraceEventType eventType
, string format
, params object[] args
)
301 if (sourceType
== null) throw new ArgumentNullException("sourceType");
303 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
304 if (f
.ShouldTrace(eventType
))
306 f
.TraceEvent(eventType
, DefaultTraceEventID
, format
, args
);
310 public static void TraceEvent(Type sourceType
, TraceEventType eventType
, int id
, string format
, params object[] args
)
312 if (sourceType
== null) throw new ArgumentNullException("sourceType");
314 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
315 if (f
.ShouldTrace(eventType
))
317 f
.TraceEvent(eventType
, id
, format
, args
);
321 public static void TraceInformation
<T
>(this T source
, string message
)
324 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, message
);
327 public static void TraceInformation
<T
>(this T source
, string format
, params object[] args
)
330 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, format
, args
);
333 public static void TraceInformation(Type sourceType
, string message
)
335 if (sourceType
== null) throw new ArgumentNullException("sourceType");
337 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
338 if (f
.ShouldTrace(TraceEventType
.Information
))
340 f
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, message
);
344 public static void TraceInformation(Type sourceType
, string format
, params object[] args
)
346 if (sourceType
== null) throw new ArgumentNullException("sourceType");
348 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
349 if (f
.ShouldTrace(TraceEventType
.Information
))
351 f
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, format
, args
);
355 public static void TraceTransfer
<T
>(this T source
, string message
, Guid relatedActivityId
)
358 TraceAdapter
<T
>.Filter
.TraceTransfer(DefaultTraceEventID
, message
, relatedActivityId
);
361 public static void TraceTransfer
<T
>(this T source
, int id
, string message
, Guid relatedActivityId
)
364 TraceAdapter
<T
>.Filter
.TraceTransfer(id
, message
, relatedActivityId
);
367 public static void TraceTransfer(Type sourceType
, string message
, Guid relatedActivityId
)
369 if (sourceType
== null) throw new ArgumentNullException("sourceType");
371 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
372 if (f
.ShouldTrace(TraceEventType
.Transfer
))
374 f
.TraceTransfer(DefaultTraceEventID
, message
, relatedActivityId
);
378 public static void TraceTransfer(Type sourceType
, int id
, string message
, Guid relatedActivityId
)
380 if (sourceType
== null) throw new ArgumentNullException("sourceType");
382 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
383 if (f
.ShouldTrace(TraceEventType
.Transfer
))
385 f
.TraceTransfer(id
, message
, relatedActivityId
);
389 public static void TraceVerbose
<T
>(this T source
, string message
)
392 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, message
);
395 public static void TraceVerbose
<T
>(this T source
, int id
, string message
)
398 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, id
, message
);
401 public static void TraceVerbose
<T
>(this T source
, string format
, params object[] args
)
404 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, format
, args
);
407 public static void TraceVerbose
<T
>(this T source
, int id
, string format
, params object[] args
)
410 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, id
, format
, args
);
413 public static void TraceVerbose(Type sourceType
, string message
)
415 if (sourceType
== null) throw new ArgumentNullException("sourceType");
417 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
418 if (f
.ShouldTrace(TraceEventType
.Verbose
))
420 f
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, message
);
424 public static void TraceVerbose(Type sourceType
, int id
, string message
)
426 if (sourceType
== null) throw new ArgumentNullException("sourceType");
428 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
429 if (f
.ShouldTrace(TraceEventType
.Verbose
))
431 f
.TraceEvent(TraceEventType
.Verbose
, id
, message
);
435 public static void TraceVerbose(Type sourceType
, string format
, params object[] args
)
437 if (sourceType
== null) throw new ArgumentNullException("sourceType");
439 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
440 if (f
.ShouldTrace(TraceEventType
.Verbose
))
442 f
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, format
, args
);
446 public static void TraceVerbose(Type sourceType
, int id
, string format
, params object[] args
)
448 if (sourceType
== null) throw new ArgumentNullException("sourceType");
450 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
451 if (f
.ShouldTrace(TraceEventType
.Verbose
))
453 f
.TraceEvent(TraceEventType
.Verbose
, id
, format
, args
);
457 public static void TraceWarning
<T
>(this T source
, string message
)
460 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, message
);
463 public static void TraceWarning
<T
>(this T source
, int id
, string message
)
466 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, id
, message
);
469 public static void TraceWarning
<T
>(this T source
, string format
, params object[] args
)
472 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, format
, args
);
475 public static void TraceWarning
<T
>(this T source
, int id
, string format
, params object[] args
)
478 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, id
, format
, args
);
481 public static void TraceWarning(Type sourceType
, string message
)
483 if (sourceType
== null) throw new ArgumentNullException("sourceType");
485 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
486 if (f
.ShouldTrace(TraceEventType
.Warning
))
488 f
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, message
);
492 public static void TraceWarning(Type sourceType
, int id
, string message
)
494 if (sourceType
== null) throw new ArgumentNullException("sourceType");
496 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
497 if (f
.ShouldTrace(TraceEventType
.Warning
))
499 f
.TraceEvent(TraceEventType
.Warning
, id
, message
);
503 public static void TraceWarning(Type sourceType
, string format
, params object[] args
)
505 if (sourceType
== null) throw new ArgumentNullException("sourceType");
507 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
508 if (f
.ShouldTrace(TraceEventType
.Warning
))
510 f
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, format
, args
);
514 public static void TraceWarning(Type sourceType
, int id
, string format
, params object[] args
)
516 if (sourceType
== null) throw new ArgumentNullException("sourceType");
518 ITraceSourceFilter f
= AcquireSourceFilter(sourceType
);
519 if (f
.ShouldTrace(TraceEventType
.Warning
))
521 f
.TraceEvent(TraceEventType
.Warning
, id
, format
, args
);
525 internal static ITraceSourceFilter
AcquireSourceFilter(Type t
)
527 if (t
== null) throw new ArgumentNullException("t");
529 ITraceSourceFilter result
;
530 string key
= String
.Intern(t
.Namespace
);
534 if (!__filters
.TryGetValue(key
, out result
))
536 Stack
<string> keys
= new Stack
<string>();
537 TraceSource src
= new TraceSource(key
);
539 while (src
.Switch
.DisplayName
== key
)
541 int i
= key
.LastIndexOf('.');
543 key
= String
.Intern(key
.Substring(0, i
));
544 if (__filters
.TryGetValue(key
, out result
))
550 src
= new TraceSource(key
);
552 switch (src
.Switch
.Level
)
554 case SourceLevels
.ActivityTracing
:
555 case SourceLevels
.All
:
556 result
= new AllSourceFilter(src
);
558 case SourceLevels
.Critical
:
559 result
= new CriticalSourceFilter(src
);
561 case SourceLevels
.Error
:
562 result
= new ErrorSourceFilter(src
);
564 case SourceLevels
.Information
:
565 result
= new InformationSourceFilter(src
);
567 case SourceLevels
.Verbose
:
568 result
= new VerboseSourceFilter(src
);
570 case SourceLevels
.Warning
:
571 result
= new WarningSourceFilter(src
);
573 case SourceLevels
.Off
:
575 result
= new NullSourceFilter(src
);
578 foreach (string kk
in keys
)
580 __filters
.Add(kk
, result
);
591 internal static class TraceAdapter
<T
>
596 private static ITraceSourceFilter __filter
;
602 static TraceAdapter()
604 __filter
= Diagnostics
.AcquireSourceFilter(typeof(T
));
607 #endregion Constructors
611 internal static ITraceSourceFilter Filter
613 get { return __filter; }
616 #endregion Properties
619 #endregion Nested Types