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/>.
25 namespace Org
.Lwes
.Trace
28 using System
.Collections
.Generic
;
29 using System
.Diagnostics
;
30 using System
.Security
;
32 using Org
.Lwes
.Config
;
33 using Org
.Lwes
.Trace
.Filters
;
36 /// Diagnostics utility.
38 public static class Traceable
43 /// Trace ID used when none was given.
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
>();
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
;
71 __traceOsProcess
= Process
.GetCurrentProcess().Id
;
73 catch (SecurityException
)
74 { // Not run with fulltrust, can't get the process Id.
78 #endregion Constructors
83 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
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
)
92 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
96 /// Writes trace data on behalf of a source object using the specified event type, event identifier, and trace data.
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
)
106 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, dataGenerator());
110 /// Traces data for a source.
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
)
120 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
124 /// Traces data for a source.
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
)
133 TraceAdapter
<T
>.Filter
.TraceData(eventType
, DefaultTraceEventID
, data
);
137 /// Traces data for a source.
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
)
147 TraceAdapter
<T
>.Filter
.TraceData(eventType
, id
, data
);
151 /// Traces data for a source type (for tracing within static methods).
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
);
168 /// Traces data for a source type (for tracing within static methods).
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());
186 /// Traces data for a source type (for tracing within static methods).
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
);
204 /// Traces data for a source type (for tracing within static methods).
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
);
221 /// Traces data for a source type (for tracing within static methods).
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
)
241 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
);
244 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string message
)
247 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, DefaultTraceEventID
, message
);
250 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, int id
, string message
)
253 TraceAdapter
<T
>.Filter
.TraceEvent(eventType
, id
, message
);
256 public static void TraceEvent
<T
>(this T source
, TraceEventType eventType
, string format
, params object[] args
)
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
)
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
)
326 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Information
, DefaultTraceEventID
, message
);
329 public static void TraceInformation
<T
>(this T source
, string format
, params object[] args
)
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
)
360 TraceAdapter
<T
>.Filter
.TraceTransfer(DefaultTraceEventID
, message
, relatedActivityId
);
363 public static void TraceTransfer
<T
>(this T source
, int id
, string message
, Guid relatedActivityId
)
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
)
394 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, DefaultTraceEventID
, message
);
397 public static void TraceVerbose
<T
>(this T source
, int id
, string message
)
400 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Verbose
, id
, message
);
403 public static void TraceVerbose
<T
>(this T source
, string format
, params object[] args
)
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
)
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
)
462 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, DefaultTraceEventID
, message
);
465 public static void TraceWarning
<T
>(this T source
, int id
, string message
)
468 TraceAdapter
<T
>.Filter
.TraceEvent(TraceEventType
.Warning
, id
, message
);
471 public static void TraceWarning
<T
>(this T source
, string format
, params object[] args
)
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
)
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
);
536 if (!__filters
.TryGetValue(key
, out result
))
538 Stack
<string> keys
= new Stack
<string>();
539 TraceSource src
= new TraceSource(key
);
541 while (src
.Switch
.DisplayName
== key
)
543 int i
= key
.LastIndexOf('.');
545 key
= String
.Intern(key
.Substring(0, i
));
546 if (__filters
.TryGetValue(key
, out result
))
552 src
= new TraceSource(key
);
554 switch (src
.Switch
.Level
)
556 case SourceLevels
.ActivityTracing
:
557 case SourceLevels
.All
:
558 result
= new AllSourceFilter(src
);
560 case SourceLevels
.Critical
:
561 result
= new CriticalSourceFilter(src
);
563 case SourceLevels
.Error
:
564 result
= new ErrorSourceFilter(src
);
566 case SourceLevels
.Information
:
567 result
= new InformationSourceFilter(src
);
569 case SourceLevels
.Verbose
:
570 result
= new VerboseSourceFilter(src
);
572 case SourceLevels
.Warning
:
573 result
= new WarningSourceFilter(src
);
575 case SourceLevels
.Off
:
577 result
= new NullSourceFilter(src
);
580 foreach (string kk
in keys
)
582 __filters
.Add(kk
, result
);
593 internal static class TraceAdapter
<T
>
598 private static ITraceSourceFilter __filter
;
604 static TraceAdapter()
606 __filter
= Traceable
.AcquireSourceFilter(typeof(T
));
609 #endregion Constructors
613 internal static ITraceSourceFilter Filter
615 get { return __filter; }
618 #endregion Properties
621 #endregion Nested Types