13 private static int s_LastCollectionCount;
23 private volatile string sourceName;
25 internal volatile bool _initCalled;
34 if (attributes ==
null)
44 public string Name => sourceName;
67 return internalSwitch;
77 internalSwitch = value;
105 if (name.Length == 0)
110 switchLevel = defaultLevel;
113 _pruneCachedTraceSources();
118 private static void _pruneCachedTraceSources()
125 for (
int i = 0; i < tracesources.
Count; i++)
128 if (traceSource !=
null)
130 list.Add(tracesources[i]);
133 if (list.Count < tracesources.
Count)
135 tracesources.
Clear();
139 s_LastCollectionCount = GC.CollectionCount(2);
144 private void Initialize()
152 SourceElementsCollection sources = DiagnosticsConfiguration.Sources;
155 SourceElement sourceElement = sources[sourceName];
156 if (sourceElement !=
null)
158 if (!
string.IsNullOrEmpty(sourceElement.SwitchName))
160 CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
164 CreateSwitch(sourceElement.SwitchType, sourceName);
165 if (!
string.IsNullOrEmpty(sourceElement.SwitchValue))
170 listeners = sourceElement.Listeners.GetRuntimeObject();
173 attributes.ReplaceHashtable(sourceElement.Attributes);
190 private void NoConfigInit()
192 internalSwitch =
new SourceSwitch(sourceName, switchLevel.ToString());
193 listeners =
new TraceListenerCollection();
194 listeners.Add(
new DefaultTraceListener());
202 if (listeners !=
null)
204 lock (TraceInternal.critSec)
218 if (listeners !=
null)
220 if (TraceInternal.UseGlobalLock)
222 lock (TraceInternal.critSec)
257 internal static void RefreshAll()
261 _pruneCachedTraceSources();
262 for (
int i = 0; i < tracesources.
Count; i++)
269 internal void Refresh()
276 SourceElementsCollection sources = DiagnosticsConfiguration.Sources;
281 SourceElement sourceElement = sources[
Name];
282 if (sourceElement !=
null)
284 if ((
string.IsNullOrEmpty(sourceElement.SwitchType) && internalSwitch.GetType() != typeof(SourceSwitch)) || sourceElement.SwitchType != internalSwitch.GetType().AssemblyQualifiedName)
286 if (!
string.IsNullOrEmpty(sourceElement.SwitchName))
288 CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
292 CreateSwitch(sourceElement.SwitchType,
Name);
293 if (!
string.IsNullOrEmpty(sourceElement.SwitchValue))
299 else if (!
string.IsNullOrEmpty(sourceElement.SwitchName))
301 if (sourceElement.SwitchName != internalSwitch.
DisplayName)
303 CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
307 internalSwitch.Refresh();
310 else if (!
string.IsNullOrEmpty(sourceElement.SwitchValue))
318 TraceListenerCollection traceListenerCollection =
new TraceListenerCollection();
319 foreach (ListenerElement listener
in sourceElement.Listeners)
321 TraceListener traceListener = listeners[listener.Name];
322 if (traceListener !=
null)
324 traceListenerCollection.Add(listener.RefreshRuntimeObject(traceListener));
328 traceListenerCollection.Add(listener.GetRuntimeObject());
333 attributes.ReplaceHashtable(sourceElement.Attributes);
334 listeners = traceListenerCollection;
338 internalSwitch.
Level = switchLevel;
353 if (!internalSwitch.
ShouldTrace(eventType) || listeners ==
null)
357 if (TraceInternal.UseGlobalLock)
359 lock (TraceInternal.critSec)
361 for (
int i = 0; i < listeners.Count; i++)
367 traceListener.
Flush();
373 for (
int j = 0; j < listeners.Count; j++)
378 lock (traceListener2)
383 traceListener2.
Flush();
391 traceListener2.
Flush();
401 [Conditional(
"TRACE")]
406 if (!internalSwitch.
ShouldTrace(eventType) || listeners ==
null)
410 if (TraceInternal.UseGlobalLock)
412 lock (TraceInternal.critSec)
414 for (
int i = 0; i < listeners.Count; i++)
417 traceListener.
TraceEvent(eventCache,
Name, eventType,
id, message);
420 traceListener.
Flush();
426 for (
int j = 0; j < listeners.Count; j++)
431 lock (traceListener2)
433 traceListener2.
TraceEvent(eventCache,
Name, eventType,
id, message);
436 traceListener2.
Flush();
441 traceListener2.
TraceEvent(eventCache,
Name, eventType,
id, message);
444 traceListener2.
Flush();
459 [Conditional(
"TRACE")]
464 if (!internalSwitch.
ShouldTrace(eventType) || listeners ==
null)
468 if (TraceInternal.UseGlobalLock)
470 lock (TraceInternal.critSec)
472 for (
int i = 0; i < listeners.Count; i++)
475 traceListener.
TraceEvent(eventCache,
Name, eventType,
id, format, args);
478 traceListener.
Flush();
484 for (
int j = 0; j < listeners.Count; j++)
489 lock (traceListener2)
491 traceListener2.
TraceEvent(eventCache,
Name, eventType,
id, format, args);
494 traceListener2.
Flush();
499 traceListener2.
TraceEvent(eventCache,
Name, eventType,
id, format, args);
502 traceListener2.
Flush();
512 [Conditional(
"TRACE")]
517 if (!internalSwitch.
ShouldTrace(eventType) || listeners ==
null)
521 if (TraceInternal.UseGlobalLock)
523 lock (TraceInternal.critSec)
525 for (
int i = 0; i < listeners.Count; i++)
528 traceListener.
TraceData(eventCache,
Name, eventType,
id, data);
531 traceListener.
Flush();
537 for (
int j = 0; j < listeners.Count; j++)
542 lock (traceListener2)
544 traceListener2.
TraceData(eventCache,
Name, eventType,
id, data);
547 traceListener2.
Flush();
552 traceListener2.
TraceData(eventCache,
Name, eventType,
id, data);
555 traceListener2.
Flush();
565 [Conditional(
"TRACE")]
570 if (!internalSwitch.
ShouldTrace(eventType) || listeners ==
null)
574 if (TraceInternal.UseGlobalLock)
576 lock (TraceInternal.critSec)
578 for (
int i = 0; i < listeners.Count; i++)
581 traceListener.
TraceData(eventCache,
Name, eventType,
id, data);
584 traceListener.
Flush();
590 for (
int j = 0; j < listeners.Count; j++)
595 lock (traceListener2)
597 traceListener2.
TraceData(eventCache,
Name, eventType,
id, data);
600 traceListener2.
Flush();
605 traceListener2.
TraceData(eventCache,
Name, eventType,
id, data);
608 traceListener2.
Flush();
616 [Conditional(
"TRACE")]
630 [Conditional(
"TRACE")]
640 [Conditional(
"TRACE")]
649 if (TraceInternal.UseGlobalLock)
651 lock (TraceInternal.critSec)
653 for (
int i = 0; i < listeners.Count; i++)
659 traceListener.
Flush();
665 for (
int j = 0; j < listeners.Count; j++)
670 lock (traceListener2)
675 traceListener2.
Flush();
683 traceListener2.
Flush();
688 private void CreateSwitch(
string typename,
string name)
690 if (!
string.IsNullOrEmpty(
typename))
696 internalSwitch =
new SourceSwitch(name, switchLevel.ToString());
Provides a thread-safe list of T:System.Diagnostics.TraceListener objects.
virtual void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
Writes trace and event information to the listener specific output.
void TraceEvent(TraceEventType eventType, int id, string format, params object[] args)
Writes a trace event to the trace listeners in the P:System.Diagnostics.TraceSource....
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
Provides trace event data specific to a thread and a process.
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
void TraceEvent(TraceEventType eventType, int id)
Writes a trace event message to the trace listeners in the P:System.Diagnostics.TraceSource....
Provides the abstract base class for the listeners who monitor trace and debug output.
void TraceTransfer(int id, string message, Guid relatedActivityId)
Writes a trace transfer message to the trace listeners in the P:System.Diagnostics....
void TraceInformation(string message)
Writes an informational message to the trace listeners in the P:System.Diagnostics....
virtual void Close()
When overridden in a derived class, closes the output stream so it no longer receives tracing or debu...
void TraceData(TraceEventType eventType, int id, params object[] data)
Writes trace data to the trace listeners in the P:System.Diagnostics.TraceSource.Listeners collection...
Provides a set of methods and properties that help you trace the execution of your code....
Implements a hash table with the key and the value strongly typed to be strings rather than objects.
StringDictionary Attributes
Gets the custom switch attributes defined in the application configuration file.
void TraceData(TraceEventType eventType, int id, object data)
Writes trace data to the trace listeners in the P:System.Diagnostics.TraceSource.Listeners collection...
void Flush()
Flushes all the trace listeners in the trace listener collection.
bool ShouldTrace(TraceEventType eventType)
Determines if trace listeners should be called, based on the trace event type.
Represents a weak reference, which references an object while still allowing that object to be reclai...
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Provides a set of methods and properties that enable applications to trace the execution of code and ...
virtual void Flush()
When overridden in a derived class, flushes the output buffer.
void Close()
Closes all the trace listeners in the trace listener collection.
string Name
Gets the name of the trace source.
virtual internal string [] GetSupportedAttributes()
Gets the custom attributes supported by the trace source.
virtual void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
Writes trace information, a message, a related activity identity and event information to the listene...
void AddRange(IEnumerable< T > collection)
Adds the elements of the specified collection to the end of the T:System.Collections....
TraceEventType
Identifies the type of event that has caused the trace.
Controls the system garbage collector, a service that automatically reclaims unused memory.
The exception that is thrown when one of the arguments provided to a method is not valid.
static bool AutoFlush
Gets or sets whether M:System.Diagnostics.Trace.Flush should be called on the P:System....
TraceListenerCollection Listeners
Gets the collection of trace listeners for the trace source.
void Clear()
Removes all elements from the T:System.Collections.Generic.List`1.
Attribute can be applied to an enumeration.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
TraceSource(string name, SourceLevels defaultLevel)
Initializes a new instance of the T:System.Diagnostics.TraceSource class, using the specified name fo...
void TraceEvent(TraceEventType eventType, int id, string message)
Writes a trace event message to the trace listeners in the P:System.Diagnostics.TraceSource....
TraceSource(string name)
Initializes a new instance of the T:System.Diagnostics.TraceSource class, using the specified name fo...
SourceLevels Level
Gets or sets the level of the switch.
static int CollectionCount(int generation)
Returns the number of times garbage collection has occurred for the specified generation of objects.
void TrimExcess()
Sets the capacity to the actual number of elements in the T:System.Collections.Generic....
SourceLevels
Specifies the levels of trace messages filtered by the source switch and event type filter.
Provides a multilevel switch to control tracing and debug output without recompiling your code.
A conditional operation, such as a > b ? a : b in C# or If(a > b, a, b) in Visual Basic.
SecurityPermissionFlag
Specifies access flags for the security permission object.
Does not allow any events through.
Provides an abstract base class to create new debugging and tracing switches.
string DisplayName
Gets a name used to identify the switch.
void TraceInformation(string format, params object[] args)
Writes an informational message to the trace listeners in the P:System.Diagnostics....
virtual bool IsThreadSafe
Gets a value indicating whether the trace listener is thread safe.
virtual void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
Writes trace information, a data object and event information to the listener specific output.