1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 // This program uses code hyperlinks available as part of the HyperAddin Visual Studio plug-in.
5 // It is available from http://www.codeplex.com/hyperAddin
8 #define FEATURE_MANAGED_ETW
10 #if !ES_BUILD_STANDALONE && !CORECLR && !PROJECTN
11 #define FEATURE_ACTIVITYSAMPLING
12 #endif // !ES_BUILD_STANDALONE
14 #endif // !PLATFORM_UNIX
16 #if ES_BUILD_STANDALONE
17 #define FEATURE_MANAGED_ETW_CHANNELS
18 // #define FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
21 /* DESIGN NOTES DESIGN NOTES DESIGN NOTES DESIGN NOTES */
23 // Over the years EventSource has become more complex and so it is important to understand
24 // the basic structure of the code to insure that it does not grow more complex.
28 // PRINCIPLE: EventSource - ETW decoupling
30 // Conceptually and EventSouce is something takes event logging data from the source methods
31 // To the EventListener that can subscribe them. Note that CONCEPTUALLY EVENTSOURCES DON'T
32 // KNOW ABOUT ETW!. The MODEL of the system is that there is a special EventListern Which
33 // we will call the EtwEventListener, that forwards commands from ETW to EventSources and
34 // listeners to the EventSources and forwards on those events to ETW. THus the model should
35 // be that you DON'T NEED ETW.
37 // Now in actual practice, EventSouce have rather intimate knowledge of ETW and send events
38 // to it directly, but this can be VIEWED AS AN OPTIMIATION.
40 // Basic Event Data Flow:
42 // There are two ways for event Data to enter the system
43 // 1) WriteEvent* and friends. This is called the 'contract' based approach because
44 // you write a method per event which forms a contract that is know at compile time.
45 // In this scheme each event is given an EVENTID (small integer). which is its identity
46 // 2) Write<T> methods. This is called the 'dynamic' approach because new events
47 // can be created on the fly. Event identity is determined by the event NAME, and these
48 // are not quite as efficient at runtime since you have at least a hash table lookup
49 // on every event write.
51 // EventSource-EventListener transfer fully support both ways of writing events (either contract
52 // based (WriteEvent*) or dynamic (Write<T>). Both way fully support the same set of data
53 // types. It is suggested, however, that you use the contract based approach when the event scheme
54 // is known at compile time (that is whenever possible). It is more efficient, but more importantly
55 // it makes the contract very explicit, and centralizes all policy about logging. These are good
56 // things. The Write<T> API is really meant for more ad-hoc
60 // Note that EventSource-EventListeners have a conceptual serialization-deserialization that happens
61 // during the transfer. In particular object identity is not preserved, some objects are morphed,
62 // and not all data types are supported. In particular you can pass
64 // A Valid type to log to an EventSource include
65 // * Primitive data types
66 // * IEnumerable<T> of valid types T (this include arrays) (* New for V4.6)
67 // * Explicitly Opted in class or struct with public property Getters over Valid types. (* New for V4.6)
69 // This set of types is roughly a generalization of JSON support (Basically primitives, bags, and arrays).
71 // Explicitly allowed structs include (* New for V4.6)
72 // * Marked with the EventData attribute
73 // * implicitly defined (e.g the C# new {x = 3, y = 5} syntax)
74 // * KeyValuePair<K,V> (thus dictionaries can be passed since they are an IEnumerable of KeyValuePair)
76 // When classes are returned in an EventListener, what is returned is something that implements
77 // IDictionary<string, T>. Thus when objects are passed to an EventSource they are transformed
78 // into a key-value bag (the IDictionary<string, T>) for consumption in the listener. These
79 // are obvious NOT the original objects.
81 // ETWserialization formats:
83 // As mentioned conceptually EventSource's send data to EventListeners and there is a conceptual
84 // copy/morph of that data as described above. In addition the .NET framework supports a conceptual
85 // ETWListener that will send the data to then ETW stream. If you use this feature, the data needs
86 // to be serialized in a way that ETW supports. ETW supports the following serialization formats
88 // 1) Manifest Based serialization.
89 // 2) SelfDescribing serialization (TraceLogging style in the TraceLogging directory)
91 // A key factor is that the Write<T> method, which support on the fly definition of events, can't
92 // support the manifest based serialization because the manifest needs the schema of all events
93 // to be known before any events are emitted. This implies the following
95 // If you use Write<T> and the output goes to ETW it will use the SelfDescribing format.
96 // If you use the EventSource(string) constructor for an eventSource (in which you don't
97 // create a subclass), the default is also to use Self-Describing serialization. In addition
98 // you can use the EventSoruce(EventSourceSettings) constructor to also explicitly specify
99 // Self-Describing serialization format. These effect the WriteEvent* APIs going to ETW.
101 // Note that none of this ETW serialization logic affects EventListeners. Only the ETW listener.
103 // *************************************************************************************
104 // *** INTERNALS: Event Propagation
106 // Data enters the system either though
108 // 1) A user defined method in the user defined subclass of EventSource which calls
109 // A) A typesafe type specific overload of WriteEvent(ID, ...) e.g. WriteEvent(ID, string, string)
110 // * which calls into the unsafe WriteEventCore(ID COUNT EventData*) WriteEventWithRelatedActivityIdCore()
111 // B) The typesafe overload WriteEvent(ID, object[]) which calls the private helper WriteEventVarargs(ID, Guid* object[])
112 // C) Directly into the unsafe WriteEventCore(ID, COUNT EventData*) or WriteEventWithRelatedActivityIdCore()
114 // All event data eventually flows to one of
115 // * WriteEventWithRelatedActivityIdCore(ID, Guid*, COUNT, EventData*)
116 // * WriteEventVarargs(ID, Guid*, object[])
118 // 2) A call to one of the overloads of Write<T>. All these overloads end up in
119 // * WriteImpl<T>(EventName, Options, Data, Guid*, Guid*)
121 // On output there are the following routines
122 // Writing to all listeners that are NOT ETW, we have the following routines
123 // * WriteToAllListeners(ID, Guid*, COUNT, EventData*)
124 // * WriteToAllListeners(ID, Guid*, object[])
125 // * WriteToAllListeners(NAME, Guid*, EventPayload)
127 // EventPayload is the internal type that implements the IDictionary<string, object> interface
128 // The EventListeners will pass back for serialized classes for nested object, but
129 // WriteToAllListeners(NAME, Guid*, EventPayload) unpacks this uses the fields as if they
130 // were parameters to a method.
132 // The first two are used for the WriteEvent* case, and the later is used for the Write<T> case.
134 // Writing to ETW, Manifest Based
135 // EventProvider.WriteEvent(EventDescriptor, Guid*, COUNT, EventData*)
136 // EventProvider.WriteEvent(EventDescriptor, Guid*, object[])
137 // Writing to ETW, Self-Describing format
138 // WriteMultiMerge(NAME, Options, Types, EventData*)
139 // WriteMultiMerge(NAME, Options, Types, object[])
140 // WriteImpl<T> has logic that knows how to serialize (like WriteMultiMerge) but also knows
143 // All ETW writes eventually call
144 // EventWriteTransfer (native PINVOKE wrapper)
145 // EventWriteTransferWrapper (fixes compat problem if you pass null as the related activityID)
146 // EventProvider.WriteEventRaw - sets last error
147 // EventSource.WriteEventRaw - Does EventSource exception handling logic
150 // EventProvider.WriteEvent(EventDescriptor, Guid*, COUNT, EventData*)
151 // EventProvider.WriteEvent(EventDescriptor, Guid*, object[])
153 // Serialization: We have a bit of a hodge-podge of serializers right now. Only the one for ETW knows
154 // how to deal with nested classes or arrays. I will call this serializer the 'TypeInfo' serializer
155 // since it is the TraceLoggingTypeInfo structure that knows how to do this. Effectively for a type you
156 // can call one of these
157 // WriteMetadata - transforms the type T into serialization meta data blob for that type
158 // WriteObjectData - transforms an object of T into serialization meta data blob for that type
159 // GetData - transforms an object of T into its deserialized form suitable for passing to EventListener.
160 // The first two are used to serialize something for ETW. The second one is used to transform the object
161 // for use by the EventListener. We also have a 'DecodeObject' method that will take a EventData* and
162 // deserialize to pass to an EventListener, but it only works on primitive types (types supported in version V4.5).
164 // It is an important observation that while EventSource does support users directly calling with EventData*
165 // blobs, we ONLY support that for the primitive types (V4.5 level support). Thus while there is a EventData*
166 // path through the system it is only for some types. The object[] path is the more general (but less efficient) path.
168 // TODO There is cleanup needed There should be no divergence until WriteEventRaw.
170 // TODO: We should have a single choke point (right now we always have this parallel EventData* and object[] path. This
171 // was historical (at one point we tried to pass object directly from EventSoruce to EventListener. That was always
172 // fragile and a compatibility headache, but we have finally been forced into the idea that there is always a transformation.
173 // This allows us to use the EventData* form to be the canonical data format in the low level APIs. This also gives us the
174 // opportunity to expose this format to EventListeners in the future.
177 using System.Runtime.CompilerServices;
178 #if FEATURE_ACTIVITYSAMPLING
179 using System.Collections.Concurrent;
181 using System.Collections.Generic;
182 using System.Collections.ObjectModel;
183 using System.Diagnostics;
184 using System.Diagnostics.CodeAnalysis;
185 using System.Globalization;
186 using System.Reflection;
187 using System.Resources;
188 using System.Security;
189 using System.Security.Permissions;
191 using System.Threading;
192 using Microsoft.Win32;
194 #if ES_BUILD_STANDALONE
195 using Environment = Microsoft.Diagnostics.Tracing.Internal.Environment;
196 using EventDescriptor = Microsoft.Diagnostics.Tracing.EventDescriptor;
198 using System.Threading.Tasks;
199 using EventDescriptor = System.Diagnostics.Tracing.EventDescriptor;
202 using Microsoft.Reflection;
204 #if !ES_BUILD_AGAINST_DOTNET_V35
205 using Contract = System.Diagnostics.Contracts.Contract;
207 using Contract = Microsoft.Diagnostics.Contracts.Internal.Contract;
210 #if ES_BUILD_STANDALONE
211 namespace Microsoft.Diagnostics.Tracing
213 namespace System.Diagnostics.Tracing
217 /// This class is meant to be inherited by a user-defined event source in order to define a managed
218 /// ETW provider. Please See DESIGN NOTES above for the internal architecture.
219 /// The minimal definition of an EventSource simply specifies a number of ETW event methods that
220 /// call one of the EventSource.WriteEvent overloads, <see cref="EventSource.WriteEventCore"/>,
221 /// or <see cref="EventSource.WriteEventWithRelatedActivityIdCore"/> to log them. This functionality
222 /// is sufficient for many users.
224 /// To achieve more control over the ETW provider manifest exposed by the event source type, the
225 /// [<see cref="EventAttribute"/>] attributes can be specified for the ETW event methods.
227 /// For very advanced EventSources, it is possible to intercept the commands being given to the
228 /// eventSource and change what filtering is done (see EventListener.EnableEvents and
229 /// <see cref="EventListener.DisableEvents"/>) or cause actions to be performed by the eventSource,
230 /// e.g. dumping a data structure (see EventSource.SendCommand and
231 /// <see cref="EventSource.OnEventCommand"/>).
233 /// The eventSources can be turned on with Windows ETW controllers (e.g. logman), immediately.
234 /// It is also possible to control and intercept the data dispatcher programmatically. See
235 /// <see cref="EventListener"/> for more.
239 /// This is a minimal definition for a custom event source:
241 /// [EventSource(Name="Samples-Demos-Minimal")]
242 /// sealed class MinimalEventSource : EventSource
244 /// public static MinimalEventSource Log = new MinimalEventSource();
245 /// public void Load(long ImageBase, string Name) { WriteEvent(1, ImageBase, Name); }
246 /// public void Unload(long ImageBase) { WriteEvent(2, ImageBase); }
247 /// private MinimalEventSource() {}
251 public partial class EventSource : IDisposable
254 #if FEATURE_EVENTSOURCE_XPLAT
255 private static readonly EventListener persistent_Xplat_Listener = XplatEventLogger.InitializePersistentListener();
256 #endif //FEATURE_EVENTSOURCE_XPLAT
259 /// The human-friendly name of the eventSource. It defaults to the simple name of the class
261 public string Name { get { return m_name; } }
263 /// Every eventSource is assigned a GUID to uniquely identify it to the system.
265 public Guid Guid { get { return m_guid; } }
268 /// Returns true if the eventSource has been enabled at all. This is the prefered test
269 /// to be performed before a relatively expensive EventSource operation.
271 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
272 public bool IsEnabled()
274 return m_eventSourceEnabled;
278 /// Returns true if events with greater than or equal 'level' and have one of 'keywords' set are enabled.
280 /// Note that the result of this function is only an approximation on whether a particular
281 /// event is active or not. It is only meant to be used as way of avoiding expensive
282 /// computation for logging when logging is not on, therefore it sometimes returns false
283 /// positives (but is always accurate when returning false). EventSources are free to
284 /// have additional filtering.
286 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
287 public bool IsEnabled(EventLevel level, EventKeywords keywords)
289 return IsEnabled(level, keywords, EventChannel.None);
293 /// Returns true if events with greater than or equal 'level' and have one of 'keywords' set are enabled, or
294 /// if 'keywords' specifies a channel bit for a channel that is enabled.
296 /// Note that the result of this function only an approximation on whether a particular
297 /// event is active or not. It is only meant to be used as way of avoiding expensive
298 /// computation for logging when logging is not on, therefore it sometimes returns false
299 /// positives (but is always accurate when returning false). EventSources are free to
300 /// have additional filtering.
302 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
303 public bool IsEnabled(EventLevel level, EventKeywords keywords, EventChannel channel)
305 if (!m_eventSourceEnabled)
308 if (!IsEnabledCommon(m_eventSourceEnabled, m_level, m_matchAnyKeyword, level, keywords, channel))
311 #if !FEATURE_ACTIVITYSAMPLING
315 #else // FEATURE_ACTIVITYSAMPLING
319 #if OPTIMIZE_IS_ENABLED
320 //================================================================================
321 // 2013/03/06 - The code below is a possible optimization for IsEnabled(level, kwd)
322 // in case activity tracing/sampling is enabled. The added complexity of this
323 // code however weighs against having it "on" until we know it's really needed.
324 // For now we'll have this #ifdef-ed out in case we see evidence this is needed.
325 //================================================================================
327 // At this point we believe the event is enabled, however we now need to check
328 // if we filter because of activity
330 // Optimization, all activity filters also register a delegate here, so if there
331 // is no delegate, we know there are no activity filters, which means that there
332 // is no additional filtering, which means that we can return true immediately.
333 if (s_activityDying == null)
336 // if there's at least one legacy ETW listener we can't filter this
337 if (m_legacySessions != null && m_legacySessions.Count > 0)
340 // if any event ID that triggers a new activity, or "transfers" activities
341 // is covered by 'keywords' we can't filter this
342 if (unchecked(((long)keywords & m_keywordTriggers)) != 0)
345 // See if all listeners have activity filters that would block the event.
346 for (int perEventSourceSessionId = 0; perEventSourceSessionId < SessionMask.MAX; ++perEventSourceSessionId)
348 EtwSession etwSession = m_etwSessionIdMap[perEventSourceSessionId];
349 if (etwSession == null)
352 ActivityFilter activityFilter = etwSession.m_activityFilter;
353 if (activityFilter == null ||
354 ActivityFilter.GetFilter(activityFilter, this) == null)
356 // No activity filter for ETW, if event is active for ETW, we can't filter.
357 for (int i = 0; i < m_eventData.Length; i++)
358 if (m_eventData[i].EnabledForETW)
361 else if (ActivityFilter.IsCurrentActivityActive(activityFilter))
365 // for regular event listeners
366 var curDispatcher = m_Dispatchers;
367 while (curDispatcher != null)
369 ActivityFilter activityFilter = curDispatcher.m_Listener.m_activityFilter;
370 if (activityFilter == null)
372 // See if any event is enabled.
373 for (int i = 0; i < curDispatcher.m_EventEnabled.Length; i++)
374 if (curDispatcher.m_EventEnabled[i])
377 else if (ActivityFilter.IsCurrentActivityActive(activityFilter))
379 curDispatcher = curDispatcher.m_Next;
382 // Every listener has an activity filter that is blocking writing the event,
383 // thus the event is not enabled.
385 #endif // OPTIMIZE_IS_ENABLED
387 #endif // FEATURE_ACTIVITYSAMPLING
391 /// Returns the settings for the event source instance
393 public EventSourceSettings Settings
395 get { return m_config; }
400 /// Returns the GUID that uniquely identifies the eventSource defined by 'eventSourceType'.
401 /// This API allows you to compute this without actually creating an instance of the EventSource.
402 /// It only needs to reflect over the type.
404 public static Guid GetGuid(Type eventSourceType)
406 if (eventSourceType == null)
407 throw new ArgumentNullException("eventSourceType");
408 Contract.EndContractBlock();
410 EventSourceAttribute attrib = (EventSourceAttribute)GetCustomAttributeHelper(eventSourceType, typeof(EventSourceAttribute));
411 string name = eventSourceType.Name;
414 if (attrib.Guid != null)
417 #if !ES_BUILD_AGAINST_DOTNET_V35
418 if (Guid.TryParse(attrib.Guid, out g))
421 try { return new Guid(attrib.Guid); }
422 catch (Exception) { }
426 if (attrib.Name != null)
431 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeName"), "eventSourceType");
433 return GenerateGuidFromName(name.ToUpperInvariant()); // Make it case insensitive.
436 /// Returns the official ETW Provider name for the eventSource defined by 'eventSourceType'.
437 /// This API allows you to compute this without actually creating an instance of the EventSource.
438 /// It only needs to reflect over the type.
440 public static string GetName(Type eventSourceType)
442 return GetName(eventSourceType, EventManifestOptions.None);
446 /// Returns a string of the XML manifest associated with the eventSourceType. The scheme for this XML is
447 /// documented at in EventManifest Schema http://msdn.microsoft.com/en-us/library/aa384043(VS.85).aspx.
448 /// This is the preferred way of generating a manifest to be embedded in the ETW stream as it is fast and
449 /// the fact that it only includes localized entries for the current UI culture is an acceptable tradeoff.
451 /// <param name="eventSourceType">The type of the event source class for which the manifest is generated</param>
452 /// <param name="assemblyPathToIncludeInManifest">The manifest XML fragment contains the string name of the DLL name in
453 /// which it is embedded. This parameter specifies what name will be used</param>
454 /// <returns>The XML data string</returns>
455 public static string GenerateManifest(Type eventSourceType, string assemblyPathToIncludeInManifest)
457 return GenerateManifest(eventSourceType, assemblyPathToIncludeInManifest, EventManifestOptions.None);
460 /// Returns a string of the XML manifest associated with the eventSourceType. The scheme for this XML is
461 /// documented at in EventManifest Schema http://msdn.microsoft.com/en-us/library/aa384043(VS.85).aspx.
462 /// Pass EventManifestOptions.AllCultures when generating a manifest to be registered on the machine. This
463 /// ensures that the entries in the event log will be "optimally" localized.
465 /// <param name="eventSourceType">The type of the event source class for which the manifest is generated</param>
466 /// <param name="assemblyPathToIncludeInManifest">The manifest XML fragment contains the string name of the DLL name in
467 /// which it is embedded. This parameter specifies what name will be used</param>
468 /// <param name="flags">The flags to customize manifest generation. If flags has bit OnlyIfNeededForRegistration specified
469 /// this returns null when the eventSourceType does not require explicit registration</param>
470 /// <returns>The XML data string or null</returns>
471 public static string GenerateManifest(Type eventSourceType, string assemblyPathToIncludeInManifest, EventManifestOptions flags)
473 if (eventSourceType == null)
474 throw new ArgumentNullException("eventSourceType");
475 Contract.EndContractBlock();
477 byte[] manifestBytes = EventSource.CreateManifestAndDescriptors(eventSourceType, assemblyPathToIncludeInManifest, null, flags);
478 return (manifestBytes == null) ? null : Encoding.UTF8.GetString(manifestBytes, 0, manifestBytes.Length);
481 // EventListener support
483 /// returns a list (IEnumerable) of all sources in the appdomain). EventListeners typically need this.
485 /// <returns></returns>
486 public static IEnumerable<EventSource> GetSources()
488 var ret = new List<EventSource>();
489 lock (EventListener.EventListenersLock)
491 foreach (WeakReference eventSourceRef in EventListener.s_EventSources)
493 EventSource eventSource = eventSourceRef.Target as EventSource;
494 if (eventSource != null && !eventSource.IsDisposed)
495 ret.Add(eventSource);
502 /// Send a command to a particular EventSource identified by 'eventSource'.
503 /// Calling this routine simply forwards the command to the EventSource.OnEventCommand
504 /// callback. What the EventSource does with the command and its arguments are from
505 /// that point EventSource-specific.
507 /// <param name="eventSource">The instance of EventSource to send the command to</param>
508 /// <param name="command">A positive user-defined EventCommand, or EventCommand.SendManifest</param>
509 /// <param name="commandArguments">A set of (name-argument, value-argument) pairs associated with the command</param>
510 public static void SendCommand(EventSource eventSource, EventCommand command, IDictionary<string, string> commandArguments)
512 if (eventSource == null)
513 throw new ArgumentNullException("eventSource");
515 // User-defined EventCommands should not conflict with the reserved commands.
516 if ((int)command <= (int)EventCommand.Update && (int)command != (int)EventCommand.SendManifest)
517 throw new ArgumentException(Environment.GetResourceString("EventSource_InvalidCommand"), "command");
519 eventSource.SendCommand(null, 0, 0, command, true, EventLevel.LogAlways, EventKeywords.None, commandArguments);
522 // ActivityID support (see also WriteEventWithRelatedActivityIdCore)
524 /// When a thread starts work that is on behalf of 'something else' (typically another
525 /// thread or network request) it should mark the thread as working on that other work.
526 /// This API marks the current thread as working on activity 'activityID'. This API
527 /// should be used when the caller knows the thread's current activity (the one being
528 /// overwritten) has completed. Otherwise, callers should prefer the overload that
529 /// return the oldActivityThatWillContinue (below).
531 /// All events created with the EventSource on this thread are also tagged with the
532 /// activity ID of the thread.
534 /// It is common, and good practice after setting the thread to an activity to log an event
535 /// with a 'start' opcode to indicate that precise time/thread where the new activity
538 /// <param name="activityId">A Guid that represents the new activity with which to mark
539 /// the current thread</param>
540 [System.Security.SecuritySafeCritical]
541 public static void SetCurrentThreadActivityId(Guid activityId)
543 #if FEATURE_MANAGED_ETW
544 #if FEATURE_ACTIVITYSAMPLING
545 Guid newId = activityId;
546 #endif // FEATURE_ACTIVITYSAMPLING
547 // We ignore errors to keep with the convention that EventSources do not throw errors.
548 // Note we can't access m_throwOnWrites because this is a static method.
549 if (UnsafeNativeMethods.ManifestEtw.EventActivityIdControl(
550 UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_SET_ID,
551 ref activityId) == 0)
553 #if FEATURE_ACTIVITYSAMPLING
554 var activityDying = s_activityDying;
555 if (activityDying != null && newId != activityId)
557 if (activityId == Guid.Empty)
559 activityId = FallbackActivityId;
561 // OutputDebugString(string.Format("Activity dying: {0} -> {1}", activityId, newId));
562 activityDying(activityId); // This is actually the OLD activity ID.
564 #endif // FEATURE_ACTIVITYSAMPLING
566 #endif // FEATURE_MANAGED_ETW
567 if (TplEtwProvider.Log != null)
568 TplEtwProvider.Log.SetActivityId(activityId);
572 /// When a thread starts work that is on behalf of 'something else' (typically another
573 /// thread or network request) it should mark the thread as working on that other work.
574 /// This API marks the current thread as working on activity 'activityID'. It returns
575 /// whatever activity the thread was previously marked with. There is a convention that
576 /// callers can assume that callees restore this activity mark before the callee returns.
577 /// To encourage this this API returns the old activity, so that it can be restored later.
579 /// All events created with the EventSource on this thread are also tagged with the
580 /// activity ID of the thread.
582 /// It is common, and good practice after setting the thread to an activity to log an event
583 /// with a 'start' opcode to indicate that precise time/thread where the new activity
586 /// <param name="activityId">A Guid that represents the new activity with which to mark
587 /// the current thread</param>
588 /// <param name="oldActivityThatWillContinue">The Guid that represents the current activity
589 /// which will continue at some point in the future, on the current thread</param>
590 [System.Security.SecuritySafeCritical]
591 public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActivityThatWillContinue)
593 oldActivityThatWillContinue = activityId;
594 #if FEATURE_MANAGED_ETW
595 // We ignore errors to keep with the convention that EventSources do not throw errors.
596 // Note we can't access m_throwOnWrites because this is a static method.
597 UnsafeNativeMethods.ManifestEtw.EventActivityIdControl(
598 UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_SET_ID,
599 ref oldActivityThatWillContinue);
600 #endif // FEATURE_MANAGED_ETW
602 // We don't call the activityDying callback here because the caller has declared that
604 if (TplEtwProvider.Log != null)
605 TplEtwProvider.Log.SetActivityId(activityId);
609 /// Retrieves the ETW activity ID associated with the current thread.
611 public static Guid CurrentThreadActivityId
613 [System.Security.SecuritySafeCritical]
616 // We ignore errors to keep with the convention that EventSources do not throw
617 // errors. Note we can't access m_throwOnWrites because this is a static method.
618 Guid retVal = new Guid();
619 #if FEATURE_MANAGED_ETW
620 UnsafeNativeMethods.ManifestEtw.EventActivityIdControl(
621 UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_ID,
623 #endif // FEATURE_MANAGED_ETW
628 #if !ES_BUILD_STANDALONE
630 /// This property allows EventSource code to appropriately handle as "different"
631 /// activities started on different threads that have not had an activity created on them.
633 internal static Guid InternalCurrentThreadActivityId
635 [System.Security.SecurityCritical]
638 Guid retval = CurrentThreadActivityId;
639 if (retval == Guid.Empty)
641 retval = FallbackActivityId;
647 internal static Guid FallbackActivityId
649 [System.Security.SecurityCritical]
652 #pragma warning disable 612, 618
653 // Managed thread IDs are more aggressively re-used than native thread IDs,
654 // so we'll use the latter...
655 return new Guid(unchecked((uint)AppDomain.GetCurrentThreadId()),
656 unchecked((ushort)s_currentPid), unchecked((ushort)(s_currentPid >> 16)),
657 0x94, 0x1b, 0x87, 0xd5, 0xa6, 0x5c, 0x36, 0x64);
658 #pragma warning restore 612, 618
661 #endif // !ES_BUILD_STANDALONE
663 // Error APIs. (We don't throw by default, but you can probe for status)
667 /// 1) Logging is often optional and thus should not generate fatal errors (exceptions)
668 /// 2) EventSources are often initialized in class constructors (which propagate exceptions poorly)
670 /// The event source constructor does not throw exceptions. Instead we remember any exception that
671 /// was generated (it is also logged to Trace.WriteLine).
673 public Exception ConstructionException { get { return m_constructionException; } }
676 /// EventSources can have arbitrary string key-value pairs associated with them called Traits.
677 /// These traits are not interpreted by the EventSource but may be interpreted by EventListeners
678 /// (e.g. like the built in ETW listener). These traits are specififed at EventSource
679 /// construction time and can be retrieved by using this GetTrait API.
681 /// <param name="key">The key to look up in the set of key-value pairs passed to the EventSource constructor</param>
682 /// <returns>The value string associated iwth key. Will return null if there is no such key.</returns>
683 public string GetTrait(string key)
685 if (m_traits != null)
687 for (int i = 0; i < m_traits.Length - 1; i += 2)
689 if (m_traits[i] == key)
690 return m_traits[i + 1];
697 /// Displays the name and GUID for the eventSource for debugging purposes.
699 public override string ToString() { return Environment.GetResourceString("EventSource_ToString", Name, Guid); }
702 /// Fires when a Command (e.g. Enable) comes from a an EventListener.
704 public event EventHandler<EventCommandEventArgs> EventCommandExecuted
710 m_eventCommandExecuted += value;
713 // If we have an EventHandler<EventCommandEventArgs> attached to the EventSource before the first command arrives
714 // It should get a chance to handle the deferred commands.
715 EventCommandEventArgs deferredCommands = m_deferredCommands;
716 while (deferredCommands != null)
718 value(this, deferredCommands);
719 deferredCommands = deferredCommands.nextCommand;
726 m_eventCommandExecuted -= value;
733 /// This is the constructor that most users will use to create their eventSource. It takes
734 /// no parameters. The ETW provider name and GUID of the EventSource are determined by the EventSource
735 /// custom attribute (so you can determine these things declaratively). If the GUID for the eventSource
736 /// is not specified in the EventSourceAttribute (recommended), it is Generated by hashing the name.
737 /// If the ETW provider name of the EventSource is not given, the name of the EventSource class is used as
738 /// the ETW provider name.
740 protected EventSource()
741 : this(EventSourceSettings.EtwManifestEventFormat)
746 /// By default calling the 'WriteEvent' methods do NOT throw on errors (they silently discard the event).
747 /// This is because in most cases users assume logging is not 'precious' and do NOT wish to have logging failures
748 /// crash the program. However for those applications where logging is 'precious' and if it fails the caller
749 /// wishes to react, setting 'throwOnEventWriteErrors' will cause an exception to be thrown if WriteEvent
750 /// fails. Note the fact that EventWrite succeeds does not necessarily mean that the event reached its destination
751 /// only that operation of writing it did not fail. These EventSources will not generate self-describing ETW events.
753 /// For compatibility only use the EventSourceSettings.ThrowOnEventWriteErrors flag instead.
755 // [Obsolete("Use the EventSource(EventSourceSettings) overload")]
756 protected EventSource(bool throwOnEventWriteErrors)
757 : this(EventSourceSettings.EtwManifestEventFormat | (throwOnEventWriteErrors ? EventSourceSettings.ThrowOnEventWriteErrors : 0))
761 /// Construct an EventSource with additional non-default settings (see EventSourceSettings for more)
763 protected EventSource(EventSourceSettings settings) : this(settings, null) { }
766 /// Construct an EventSource with additional non-default settings.
768 /// Also specify a list of key-value pairs called traits (you must pass an even number of strings).
769 /// The first string is the key and the second is the value. These are not interpreted by EventSource
770 /// itself but may be interprated the listeners. Can be fetched with GetTrait(string).
772 /// <param name="settings">See EventSourceSettings for more.</param>
773 /// <param name="traits">A collection of key-value strings (must be an even number).</param>
774 protected EventSource(EventSourceSettings settings, params string[] traits)
776 m_config = ValidateSettings(settings);
777 var myType = this.GetType();
778 Initialize(GetGuid(myType), GetName(myType), traits);
782 /// This method is called when the eventSource is updated by the controller.
784 protected virtual void OnEventCommand(EventCommandEventArgs command) { }
786 #pragma warning disable 1591
787 // optimized for common signatures (no args)
788 [SecuritySafeCritical]
789 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
790 protected unsafe void WriteEvent(int eventId)
792 WriteEventCore(eventId, 0, null);
795 // optimized for common signatures (ints)
796 [SecuritySafeCritical]
797 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
798 protected unsafe void WriteEvent(int eventId, int arg1)
800 if (m_eventSourceEnabled)
802 EventSource.EventData* descrs = stackalloc EventSource.EventData[1];
803 descrs[0].DataPointer = (IntPtr)(&arg1);
805 WriteEventCore(eventId, 1, descrs);
809 [SecuritySafeCritical]
810 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
811 protected unsafe void WriteEvent(int eventId, int arg1, int arg2)
813 if (m_eventSourceEnabled)
815 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
816 descrs[0].DataPointer = (IntPtr)(&arg1);
818 descrs[1].DataPointer = (IntPtr)(&arg2);
820 WriteEventCore(eventId, 2, descrs);
824 [SecuritySafeCritical]
825 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
826 protected unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3)
828 if (m_eventSourceEnabled)
830 EventSource.EventData* descrs = stackalloc EventSource.EventData[3];
831 descrs[0].DataPointer = (IntPtr)(&arg1);
833 descrs[1].DataPointer = (IntPtr)(&arg2);
835 descrs[2].DataPointer = (IntPtr)(&arg3);
837 WriteEventCore(eventId, 3, descrs);
841 // optimized for common signatures (longs)
842 [SecuritySafeCritical]
843 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
844 protected unsafe void WriteEvent(int eventId, long arg1)
846 if (m_eventSourceEnabled)
848 EventSource.EventData* descrs = stackalloc EventSource.EventData[1];
849 descrs[0].DataPointer = (IntPtr)(&arg1);
851 WriteEventCore(eventId, 1, descrs);
855 [SecuritySafeCritical]
856 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
857 protected unsafe void WriteEvent(int eventId, long arg1, long arg2)
859 if (m_eventSourceEnabled)
861 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
862 descrs[0].DataPointer = (IntPtr)(&arg1);
864 descrs[1].DataPointer = (IntPtr)(&arg2);
866 WriteEventCore(eventId, 2, descrs);
870 [SecuritySafeCritical]
871 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
872 protected unsafe void WriteEvent(int eventId, long arg1, long arg2, long arg3)
874 if (m_eventSourceEnabled)
876 EventSource.EventData* descrs = stackalloc EventSource.EventData[3];
877 descrs[0].DataPointer = (IntPtr)(&arg1);
879 descrs[1].DataPointer = (IntPtr)(&arg2);
881 descrs[2].DataPointer = (IntPtr)(&arg3);
883 WriteEventCore(eventId, 3, descrs);
887 // optimized for common signatures (strings)
888 [SecuritySafeCritical]
889 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
890 protected unsafe void WriteEvent(int eventId, string arg1)
892 if (m_eventSourceEnabled)
894 if (arg1 == null) arg1 = "";
895 fixed (char* string1Bytes = arg1)
897 EventSource.EventData* descrs = stackalloc EventSource.EventData[1];
898 descrs[0].DataPointer = (IntPtr)string1Bytes;
899 descrs[0].Size = ((arg1.Length + 1) * 2);
900 WriteEventCore(eventId, 1, descrs);
905 [SecuritySafeCritical]
906 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
907 protected unsafe void WriteEvent(int eventId, string arg1, string arg2)
909 if (m_eventSourceEnabled)
911 if (arg1 == null) arg1 = "";
912 if (arg2 == null) arg2 = "";
913 fixed (char* string1Bytes = arg1)
914 fixed (char* string2Bytes = arg2)
916 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
917 descrs[0].DataPointer = (IntPtr)string1Bytes;
918 descrs[0].Size = ((arg1.Length + 1) * 2);
919 descrs[1].DataPointer = (IntPtr)string2Bytes;
920 descrs[1].Size = ((arg2.Length + 1) * 2);
921 WriteEventCore(eventId, 2, descrs);
926 [SecuritySafeCritical]
927 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
928 protected unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg3)
930 if (m_eventSourceEnabled)
932 if (arg1 == null) arg1 = "";
933 if (arg2 == null) arg2 = "";
934 if (arg3 == null) arg3 = "";
935 fixed (char* string1Bytes = arg1)
936 fixed (char* string2Bytes = arg2)
937 fixed (char* string3Bytes = arg3)
939 EventSource.EventData* descrs = stackalloc EventSource.EventData[3];
940 descrs[0].DataPointer = (IntPtr)string1Bytes;
941 descrs[0].Size = ((arg1.Length + 1) * 2);
942 descrs[1].DataPointer = (IntPtr)string2Bytes;
943 descrs[1].Size = ((arg2.Length + 1) * 2);
944 descrs[2].DataPointer = (IntPtr)string3Bytes;
945 descrs[2].Size = ((arg3.Length + 1) * 2);
946 WriteEventCore(eventId, 3, descrs);
951 // optimized for common signatures (string and ints)
952 [SecuritySafeCritical]
953 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
954 protected unsafe void WriteEvent(int eventId, string arg1, int arg2)
956 if (m_eventSourceEnabled)
958 if (arg1 == null) arg1 = "";
959 fixed (char* string1Bytes = arg1)
961 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
962 descrs[0].DataPointer = (IntPtr)string1Bytes;
963 descrs[0].Size = ((arg1.Length + 1) * 2);
964 descrs[1].DataPointer = (IntPtr)(&arg2);
966 WriteEventCore(eventId, 2, descrs);
971 [SecuritySafeCritical]
972 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
973 protected unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3)
975 if (m_eventSourceEnabled)
977 if (arg1 == null) arg1 = "";
978 fixed (char* string1Bytes = arg1)
980 EventSource.EventData* descrs = stackalloc EventSource.EventData[3];
981 descrs[0].DataPointer = (IntPtr)string1Bytes;
982 descrs[0].Size = ((arg1.Length + 1) * 2);
983 descrs[1].DataPointer = (IntPtr)(&arg2);
985 descrs[2].DataPointer = (IntPtr)(&arg3);
987 WriteEventCore(eventId, 3, descrs);
992 // optimized for common signatures (string and longs)
993 [SecuritySafeCritical]
994 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
995 protected unsafe void WriteEvent(int eventId, string arg1, long arg2)
997 if (m_eventSourceEnabled)
999 if (arg1 == null) arg1 = "";
1000 fixed (char* string1Bytes = arg1)
1002 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1003 descrs[0].DataPointer = (IntPtr)string1Bytes;
1004 descrs[0].Size = ((arg1.Length + 1) * 2);
1005 descrs[1].DataPointer = (IntPtr)(&arg2);
1007 WriteEventCore(eventId, 2, descrs);
1012 // optimized for common signatures (long and string)
1013 [SecuritySafeCritical]
1014 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
1015 protected unsafe void WriteEvent(int eventId, long arg1, string arg2)
1017 if (m_eventSourceEnabled)
1019 if (arg2 == null) arg2 = "";
1020 fixed (char* string2Bytes = arg2)
1022 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1023 descrs[0].DataPointer = (IntPtr)(&arg1);
1025 descrs[1].DataPointer = (IntPtr)string2Bytes;
1026 descrs[1].Size = ((arg2.Length + 1) * 2);
1027 WriteEventCore(eventId, 2, descrs);
1032 // optimized for common signatures (int and string)
1033 [SecuritySafeCritical]
1034 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
1035 protected unsafe void WriteEvent(int eventId, int arg1, string arg2)
1037 if (m_eventSourceEnabled)
1039 if (arg2 == null) arg2 = "";
1040 fixed (char* string2Bytes = arg2)
1042 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1043 descrs[0].DataPointer = (IntPtr)(&arg1);
1045 descrs[1].DataPointer = (IntPtr)string2Bytes;
1046 descrs[1].Size = ((arg2.Length + 1) * 2);
1047 WriteEventCore(eventId, 2, descrs);
1052 [SecuritySafeCritical]
1053 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
1054 protected unsafe void WriteEvent(int eventId, byte[] arg1)
1056 if (m_eventSourceEnabled)
1058 if (arg1 == null) arg1 = new byte[0];
1059 int blobSize = arg1.Length;
1060 fixed (byte* blob = &arg1[0])
1062 EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1063 descrs[0].DataPointer = (IntPtr)(&blobSize);
1065 descrs[1].DataPointer = (IntPtr)blob;
1066 descrs[1].Size = blobSize;
1067 WriteEventCore(eventId, 2, descrs);
1072 [SecuritySafeCritical]
1073 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
1074 protected unsafe void WriteEvent(int eventId, long arg1, byte[] arg2)
1076 if (m_eventSourceEnabled)
1078 if (arg2 == null) arg2 = new byte[0];
1079 int blobSize = arg2.Length;
1080 fixed (byte* blob = &arg2[0])
1082 EventSource.EventData* descrs = stackalloc EventSource.EventData[3];
1083 descrs[0].DataPointer = (IntPtr)(&arg1);
1085 descrs[1].DataPointer = (IntPtr)(&blobSize);
1087 descrs[2].DataPointer = (IntPtr)blob;
1088 descrs[2].Size = blobSize;
1089 WriteEventCore(eventId, 3, descrs);
1094 #pragma warning restore 1591
1097 /// Used to construct the data structure to be passed to the native ETW APIs - EventWrite and EventWriteTransfer.
1099 protected internal struct EventData
1102 /// Address where the one argument lives (if this points to managed memory you must ensure the
1103 /// managed object is pinned.
1105 public IntPtr DataPointer { get { return (IntPtr)m_Ptr; } set { m_Ptr = unchecked((long)value); } }
1107 /// Size of the argument referenced by DataPointer
1109 public int Size { get { return m_Size; } set { m_Size = value; } }
1113 /// Initializes the members of this EventData object to point at a previously-pinned
1114 /// tracelogging-compatible metadata blob.
1116 /// <param name="pointer">Pinned tracelogging-compatible metadata blob.</param>
1117 /// <param name="size">The size of the metadata blob.</param>
1118 /// <param name="reserved">Value for reserved: 2 for per-provider metadata, 1 for per-event metadata</param>
1120 internal unsafe void SetMetadata(byte* pointer, int size, int reserved)
1122 this.m_Ptr = (long)(ulong)(UIntPtr)pointer;
1124 this.m_Reserved = reserved; // Mark this descriptor as containing tracelogging-compatible metadata.
1127 //Important, we pass this structure directly to the Win32 EventWrite API, so this structure must be layed out exactly
1128 // the way EventWrite wants it.
1129 internal long m_Ptr;
1130 internal int m_Size;
1131 #pragma warning disable 0649
1132 internal int m_Reserved; // Used to pad the size to match the Win32 API
1133 #pragma warning restore 0649
1138 /// This routine allows you to create efficient WriteEvent helpers, however the code that you use to
1139 /// do this, while straightforward, is unsafe.
1143 /// protected unsafe void WriteEvent(int eventId, string arg1, long arg2)
1145 /// if (IsEnabled())
1147 /// if (arg2 == null) arg2 = "";
1148 /// fixed (char* string2Bytes = arg2)
1150 /// EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1151 /// descrs[0].DataPointer = (IntPtr)(&arg1);
1152 /// descrs[0].Size = 8;
1153 /// descrs[1].DataPointer = (IntPtr)string2Bytes;
1154 /// descrs[1].Size = ((arg2.Length + 1) * 2);
1155 /// WriteEventCore(eventId, 2, descrs);
1162 [CLSCompliant(false)]
1163 protected unsafe void WriteEventCore(int eventId, int eventDataCount, EventSource.EventData* data)
1165 WriteEventWithRelatedActivityIdCore(eventId, null, eventDataCount, data);
1169 /// This routine allows you to create efficient WriteEventWithRelatedActivityId helpers, however the code
1170 /// that you use to do this, while straightforward, is unsafe. The only difference from
1171 /// <see cref="WriteEventCore"/> is that you pass the relatedActivityId from caller through to this API
1175 /// protected unsafe void WriteEventWithRelatedActivityId(int eventId, Guid relatedActivityId, string arg1, long arg2)
1177 /// if (IsEnabled())
1179 /// if (arg2 == null) arg2 = "";
1180 /// fixed (char* string2Bytes = arg2)
1182 /// EventSource.EventData* descrs = stackalloc EventSource.EventData[2];
1183 /// descrs[0].DataPointer = (IntPtr)(&arg1);
1184 /// descrs[0].Size = 8;
1185 /// descrs[1].DataPointer = (IntPtr)string2Bytes;
1186 /// descrs[1].Size = ((arg2.Length + 1) * 2);
1187 /// WriteEventWithRelatedActivityIdCore(eventId, relatedActivityId, 2, descrs);
1194 [CLSCompliant(false)]
1195 protected unsafe void WriteEventWithRelatedActivityIdCore(int eventId, Guid* relatedActivityId, int eventDataCount, EventSource.EventData* data)
1197 if (m_eventSourceEnabled)
1201 Contract.Assert(m_eventData != null); // You must have initialized this if you enabled the source.
1202 if (relatedActivityId != null)
1203 ValidateEventOpcodeForTransfer(ref m_eventData[eventId]);
1205 #if FEATURE_MANAGED_ETW
1206 if (m_eventData[eventId].EnabledForETW)
1208 EventOpcode opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode;
1209 EventActivityOptions activityOptions = m_eventData[eventId].ActivityOptions;
1210 Guid* pActivityId = null;
1211 Guid activityId = Guid.Empty;
1212 Guid relActivityId = Guid.Empty;
1214 if (opcode != EventOpcode.Info && relatedActivityId == null &&
1215 ((activityOptions & EventActivityOptions.Disable) == 0))
1217 if (opcode == EventOpcode.Start)
1219 m_activityTracker.OnStart(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId, ref relActivityId, m_eventData[eventId].ActivityOptions);
1221 else if (opcode == EventOpcode.Stop)
1223 m_activityTracker.OnStop(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId);
1226 if (activityId != Guid.Empty)
1227 pActivityId = &activityId;
1228 if (relActivityId != Guid.Empty)
1229 relatedActivityId = &relActivityId;
1232 #if FEATURE_ACTIVITYSAMPLING
1233 // this code should be kept in sync with WriteEventVarargs().
1234 SessionMask etwSessions = SessionMask.All;
1235 // only compute etwSessions if there are *any* ETW filters enabled...
1236 if ((ulong)m_curLiveSessions != 0)
1237 etwSessions = GetEtwSessionMask(eventId, relatedActivityId);
1238 // OutputDebugString(string.Format("{0}.WriteEvent(id {1}) -> to sessions {2:x}",
1239 // m_name, m_eventData[eventId].Name, (ulong) etwSessions));
1241 if ((ulong)etwSessions != 0 || m_legacySessions != null && m_legacySessions.Count > 0)
1243 if (!SelfDescribingEvents)
1245 if (etwSessions.IsEqualOrSupersetOf(m_curLiveSessions))
1247 // OutputDebugString(string.Format(" (1) id {0}, kwd {1:x}",
1248 // m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Keywords));
1249 // by default the Descriptor.Keyword will have the perEventSourceSessionId bit
1250 // mask set to 0x0f so, when all ETW sessions want the event we don't need to
1251 // synthesize a new one
1252 if (!m_provider.WriteEvent(ref m_eventData[eventId].Descriptor, pActivityId, relatedActivityId, eventDataCount, (IntPtr)data))
1253 ThrowEventSourceException();
1257 long origKwd = unchecked((long)((ulong)m_eventData[eventId].Descriptor.Keywords & ~(SessionMask.All.ToEventKeywords())));
1258 // OutputDebugString(string.Format(" (2) id {0}, kwd {1:x}",
1259 // m_eventData[eventId].Name, etwSessions.ToEventKeywords() | (ulong) origKwd));
1260 // only some of the ETW sessions will receive this event. Synthesize a new
1261 // Descriptor whose Keywords field will have the appropriate bits set.
1262 // etwSessions might be 0, if there are legacy ETW listeners that want this event
1263 var desc = new EventDescriptor(
1264 m_eventData[eventId].Descriptor.EventId,
1265 m_eventData[eventId].Descriptor.Version,
1266 m_eventData[eventId].Descriptor.Channel,
1267 m_eventData[eventId].Descriptor.Level,
1268 m_eventData[eventId].Descriptor.Opcode,
1269 m_eventData[eventId].Descriptor.Task,
1270 unchecked((long)etwSessions.ToEventKeywords() | origKwd));
1272 if (!m_provider.WriteEvent(ref desc, pActivityId, relatedActivityId, eventDataCount, (IntPtr)data))
1273 ThrowEventSourceException();
1278 TraceLoggingEventTypes tlet = m_eventData[eventId].TraceLoggingEventTypes;
1281 tlet = new TraceLoggingEventTypes(m_eventData[eventId].Name,
1283 m_eventData[eventId].Parameters);
1284 Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, tlet, null);
1287 long origKwd = unchecked((long)((ulong)m_eventData[eventId].Descriptor.Keywords & ~(SessionMask.All.ToEventKeywords())));
1288 // TODO: activity ID support
1289 EventSourceOptions opt = new EventSourceOptions
1291 Keywords = (EventKeywords)unchecked((long)etwSessions.ToEventKeywords() | origKwd),
1292 Level = (EventLevel)m_eventData[eventId].Descriptor.Level,
1293 Opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode
1296 WriteMultiMerge(m_eventData[eventId].Name, ref opt, tlet, pActivityId, relatedActivityId, data);
1300 if (!SelfDescribingEvents)
1302 if (!m_provider.WriteEvent(ref m_eventData[eventId].Descriptor, pActivityId, relatedActivityId, eventDataCount, (IntPtr)data))
1303 ThrowEventSourceException();
1307 TraceLoggingEventTypes tlet = m_eventData[eventId].TraceLoggingEventTypes;
1310 tlet = new TraceLoggingEventTypes(m_eventData[eventId].Name,
1311 m_eventData[eventId].Tags,
1312 m_eventData[eventId].Parameters);
1313 Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, tlet, null);
1316 EventSourceOptions opt = new EventSourceOptions
1318 Keywords = (EventKeywords)m_eventData[eventId].Descriptor.Keywords,
1319 Level = (EventLevel)m_eventData[eventId].Descriptor.Level,
1320 Opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode
1323 WriteMultiMerge(m_eventData[eventId].Name, ref opt, tlet, pActivityId, relatedActivityId, data);
1325 #endif // FEATURE_ACTIVITYSAMPLING
1327 #endif // FEATURE_MANAGED_ETW
1329 if (m_Dispatchers != null && m_eventData[eventId].EnabledForAnyListener)
1330 WriteToAllListeners(eventId, relatedActivityId, eventDataCount, data);
1332 catch (Exception ex)
1334 if (ex is EventSourceException)
1337 ThrowEventSourceException(ex);
1342 // fallback varags helpers.
1344 /// This is the varargs helper for writing an event. It does create an array and box all the arguments so it is
1345 /// relatively inefficient and should only be used for relatively rare events (e.g. less than 100 / sec). If your
1346 /// rates are faster than that you should use <see cref="WriteEventCore"/> to create fast helpers for your particular
1347 /// method signature. Even if you use this for rare events, this call should be guarded by an <see cref="IsEnabled()"/>
1348 /// check so that the varargs call is not made when the EventSource is not active.
1350 [SecuritySafeCritical]
1351 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
1352 protected unsafe void WriteEvent(int eventId, params object[] args)
1354 WriteEventVarargs(eventId, null, args);
1358 /// This is the varargs helper for writing an event which also specifies a related activity. It is completely analogous
1359 /// to corresponding WriteEvent (they share implementation). It does create an array and box all the arguments so it is
1360 /// relatively inefficient and should only be used for relatively rare events (e.g. less than 100 / sec). If your
1361 /// rates are faster than that you should use <see cref="WriteEventWithRelatedActivityIdCore"/> to create fast helpers for your
1362 /// particular method signature. Even if you use this for rare events, this call should be guarded by an <see cref="IsEnabled()"/>
1363 /// check so that the varargs call is not made when the EventSource is not active.
1365 [SecuritySafeCritical]
1366 protected unsafe void WriteEventWithRelatedActivityId(int eventId, Guid relatedActivityId, params object[] args)
1368 WriteEventVarargs(eventId, &relatedActivityId, args);
1373 #region IDisposable Members
1375 /// Disposes of an EventSource.
1377 public void Dispose()
1380 GC.SuppressFinalize(this);
1383 /// Disposes of an EventSource.
1386 /// Called from Dispose() with disposing=true, and from the finalizer (~EventSource) with disposing=false.
1388 /// 1. We may be called more than once: do nothing after the first call.
1389 /// 2. Avoid throwing exceptions if disposing is false, i.e. if we're being finalized.
1391 /// <param name="disposing">True if called from Dispose(), false if called from the finalizer.</param>
1392 protected virtual void Dispose(bool disposing)
1396 #if FEATURE_MANAGED_ETW
1397 // Send the manifest one more time to ensure circular buffers have a chance to get to this information
1398 // even in scenarios with a high volume of ETW events.
1399 if (m_eventSourceEnabled)
1403 SendManifest(m_rawManifest);
1406 { } // If it fails, simply give up.
1407 m_eventSourceEnabled = false;
1409 if (m_provider != null)
1411 m_provider.Dispose();
1416 m_eventSourceEnabled = false;
1419 /// Finalizer for EventSource
1423 this.Dispose(false);
1428 #if FEATURE_ACTIVITYSAMPLING
1429 internal void WriteStringToListener(EventListener listener, string msg, SessionMask m)
1431 Contract.Assert(listener == null || (uint)m == (uint)SessionMask.FromId(0));
1433 if (m_eventSourceEnabled)
1435 if (listener == null)
1437 WriteEventString(0, unchecked((long)m.ToEventKeywords()), msg);
1441 List<object> arg = new List<object>();
1443 EventWrittenEventArgs eventCallbackArgs = new EventWrittenEventArgs(this);
1444 eventCallbackArgs.EventId = 0;
1445 eventCallbackArgs.Payload = new ReadOnlyCollection<object>(arg);
1446 listener.OnEventWritten(eventCallbackArgs);
1452 private unsafe void WriteEventRaw(
1453 ref EventDescriptor eventDescriptor,
1455 Guid* relatedActivityID,
1459 #if FEATURE_MANAGED_ETW
1460 if (m_provider == null)
1462 ThrowEventSourceException();
1466 if (!m_provider.WriteEventRaw(ref eventDescriptor, activityID, relatedActivityID, dataCount, data))
1467 ThrowEventSourceException();
1469 #endif // FEATURE_MANAGED_ETW
1472 // FrameworkEventSource is on the startup path for the framework, so we have this internal overload that it can use
1473 // to prevent the working set hit from looking at the custom attributes on the type to get the Guid.
1474 internal EventSource(Guid eventSourceGuid, string eventSourceName)
1475 : this(eventSourceGuid, eventSourceName, EventSourceSettings.EtwManifestEventFormat)
1478 // Used by the internal FrameworkEventSource constructor and the TraceLogging-style event source constructor
1479 internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSettings settings, string[] traits = null)
1481 m_config = ValidateSettings(settings);
1482 Initialize(eventSourceGuid, eventSourceName, traits);
1486 /// This method is responsible for the common initialization path from our constructors. It must
1487 /// not leak any exceptions (otherwise, since most EventSource classes define a static member,
1488 /// "Log", such an exception would become a cached exception for the initialization of the static
1489 /// member, and any future access to the "Log" would throw the cached exception).
1491 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "guid")]
1492 [SecuritySafeCritical]
1493 private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[] traits)
1498 if (m_traits != null && m_traits.Length % 2 != 0)
1499 throw new ArgumentException(Environment.GetResourceString("TraitEven"), "traits");
1501 if (eventSourceGuid == Guid.Empty)
1502 throw new ArgumentException(Environment.GetResourceString("EventSource_NeedGuid"));
1504 if (eventSourceName == null)
1505 throw new ArgumentException(Environment.GetResourceString("EventSource_NeedName"));
1507 m_name = eventSourceName;
1508 m_guid = eventSourceGuid;
1509 #if FEATURE_ACTIVITYSAMPLING
1510 m_curLiveSessions = new SessionMask(0);
1511 m_etwSessionIdMap = new EtwSession[SessionMask.MAX];
1512 #endif // FEATURE_ACTIVITYSAMPLING
1514 //Enable Implicit Activity tracker
1515 m_activityTracker = ActivityTracker.Instance;
1517 #if FEATURE_MANAGED_ETW
1518 // Create and register our provider traits. We do this early because it is needed to log errors
1519 // In the self-describing event case.
1520 this.InitializeProviderMetadata();
1522 // Register the provider with ETW
1523 var provider = new OverideEventProvider(this);
1524 provider.Register(eventSourceGuid);
1526 // Add the eventSource to the global (weak) list.
1527 // This also sets m_id, which is the index in the list.
1528 EventListener.AddEventSource(this);
1530 #if FEATURE_MANAGED_ETW
1531 // OK if we get this far without an exception, then we can at least write out error messages.
1532 // Set m_provider, which allows this.
1533 m_provider = provider;
1535 #if !ES_BUILD_STANDALONE
1536 // API available on OS >= Win 8 and patched Win 7.
1537 // Disable only for FrameworkEventSource to avoid recursion inside exception handling.
1538 var osVer = Environment.OSVersion.Version.Major * 10 + Environment.OSVersion.Version.Minor;
1539 if (this.Name != "System.Diagnostics.Eventing.FrameworkEventSource" || osVer >= 62)
1542 int setInformationResult;
1543 fixed (void* providerMetadata = this.providerMetadata)
1545 setInformationResult = m_provider.SetInformation(
1546 UnsafeNativeMethods.ManifestEtw.EVENT_INFO_CLASS.SetTraits,
1548 this.providerMetadata.Length);
1551 #endif // FEATURE_MANAGED_ETW
1553 Contract.Assert(!m_eventSourceEnabled); // We can't be enabled until we are completely initted.
1554 // We are logically completely initialized at this point.
1555 m_completelyInited = true;
1559 if (m_constructionException == null)
1560 m_constructionException = e;
1561 ReportOutOfBandMessage("ERROR: Exception during construction of EventSource " + Name + ": " + e.Message, true);
1564 // Once m_completelyInited is set, you can have concurrency, so all work is under the lock.
1565 lock (EventListener.EventListenersLock)
1567 // If there are any deferred commands, we can do them now.
1568 // This is the most likely place for exceptions to happen.
1569 // Note that we are NOT resetting m_deferredCommands to NULL here,
1570 // We are giving for EventHandler<EventCommandEventArgs> that will be attached later
1571 EventCommandEventArgs deferredCommands = m_deferredCommands;
1572 while (deferredCommands != null)
1574 DoCommand(deferredCommands); // This can never throw, it catches them and reports the errors.
1575 deferredCommands = deferredCommands.nextCommand;
1580 private static string GetName(Type eventSourceType, EventManifestOptions flags)
1582 if (eventSourceType == null)
1583 throw new ArgumentNullException("eventSourceType");
1584 Contract.EndContractBlock();
1586 EventSourceAttribute attrib = (EventSourceAttribute)GetCustomAttributeHelper(eventSourceType, typeof(EventSourceAttribute), flags);
1587 if (attrib != null && attrib.Name != null)
1590 return eventSourceType.Name;
1594 /// Implements the SHA1 hashing algorithm. Note that this
1595 /// implementation is for hashing public information. Do not
1596 /// use this code to hash private data, as this implementation does
1597 /// not take any steps to avoid information disclosure.
1599 private struct Sha1ForNonSecretPurposes
1601 private long length; // Total message length in bits
1602 private uint[] w; // Workspace
1603 private int pos; // Length of current chunk in bytes
1606 /// Call Start() to initialize the hash object.
1612 this.w = new uint[85];
1617 this.w[80] = 0x67452301;
1618 this.w[81] = 0xEFCDAB89;
1619 this.w[82] = 0x98BADCFE;
1620 this.w[83] = 0x10325476;
1621 this.w[84] = 0xC3D2E1F0;
1625 /// Adds an input byte to the hash.
1627 /// <param name="input">Data to include in the hash.</param>
1628 public void Append(byte input)
1630 this.w[this.pos / 4] = (this.w[this.pos / 4] << 8) | input;
1631 if (64 == ++this.pos)
1638 /// Adds input bytes to the hash.
1640 /// <param name="input">
1641 /// Data to include in the hash. Must not be null.
1643 public void Append(byte[] input)
1645 foreach (var b in input)
1652 /// Retrieves the hash value.
1653 /// Note that after calling this function, the hash object should
1654 /// be considered uninitialized. Subsequent calls to Append or
1655 /// Finish will produce useless results. Call Start() to
1658 /// <param name="output">
1659 /// Buffer to receive the hash value. Must not be null.
1660 /// Up to 20 bytes of hash will be written to the output buffer.
1661 /// If the buffer is smaller than 20 bytes, the remaining hash
1662 /// bytes will be lost. If the buffer is larger than 20 bytes, the
1663 /// rest of the buffer is left unmodified.
1665 public void Finish(byte[] output)
1667 long l = this.length + 8 * this.pos;
1669 while (this.pos != 56)
1676 this.Append((byte)(l >> 56));
1677 this.Append((byte)(l >> 48));
1678 this.Append((byte)(l >> 40));
1679 this.Append((byte)(l >> 32));
1680 this.Append((byte)(l >> 24));
1681 this.Append((byte)(l >> 16));
1682 this.Append((byte)(l >> 8));
1683 this.Append((byte)l);
1685 int end = output.Length < 20 ? output.Length : 20;
1686 for (int i = 0; i != end; i++)
1688 uint temp = this.w[80 + i / 4];
1689 output[i] = (byte)(temp >> 24);
1690 this.w[80 + i / 4] = temp << 8;
1696 /// Called when this.pos reaches 64.
1698 private void Drain()
1700 for (int i = 16; i != 80; i++)
1702 this.w[i] = Rol1((this.w[i - 3] ^ this.w[i - 8] ^ this.w[i - 14] ^ this.w[i - 16]));
1707 uint a = this.w[80];
1708 uint b = this.w[81];
1709 uint c = this.w[82];
1710 uint d = this.w[83];
1711 uint e = this.w[84];
1713 for (int i = 0; i != 20; i++)
1715 const uint k = 0x5A827999;
1716 uint f = (b & c) | ((~b) & d);
1717 uint temp = Rol5(a) + f + e + k + this.w[i]; e = d; d = c; c = Rol30(b); b = a; a = temp;
1720 for (int i = 20; i != 40; i++)
1723 const uint k = 0x6ED9EBA1;
1724 uint temp = Rol5(a) + f + e + k + this.w[i]; e = d; d = c; c = Rol30(b); b = a; a = temp;
1727 for (int i = 40; i != 60; i++)
1729 uint f = (b & c) | (b & d) | (c & d);
1730 const uint k = 0x8F1BBCDC;
1731 uint temp = Rol5(a) + f + e + k + this.w[i]; e = d; d = c; c = Rol30(b); b = a; a = temp;
1734 for (int i = 60; i != 80; i++)
1737 const uint k = 0xCA62C1D6;
1738 uint temp = Rol5(a) + f + e + k + this.w[i]; e = d; d = c; c = Rol30(b); b = a; a = temp;
1748 this.length += 512; // 64 bytes == 512 bits
1752 private static uint Rol1(uint input)
1754 return (input << 1) | (input >> 31);
1757 private static uint Rol5(uint input)
1759 return (input << 5) | (input >> 27);
1762 private static uint Rol30(uint input)
1764 return (input << 30) | (input >> 2);
1768 private static Guid GenerateGuidFromName(string name)
1770 byte[] bytes = Encoding.BigEndianUnicode.GetBytes(name);
1771 var hash = new Sha1ForNonSecretPurposes();
1773 hash.Append(namespaceBytes);
1775 Array.Resize(ref bytes, 16);
1778 bytes[7] = unchecked((byte)((bytes[7] & 0x0F) | 0x50)); // Set high 4 bits of octet 7 to 5, as per RFC 4122
1779 return new Guid(bytes);
1783 private unsafe object DecodeObject(int eventId, int parameterId, ref EventSource.EventData* data)
1785 // TODO FIX : We use reflection which in turn uses EventSource, right now we carefully avoid
1786 // the recursion, but can we do this in a robust way?
1788 IntPtr dataPointer = data->DataPointer;
1789 // advance to next EventData in array
1792 Type dataType = m_eventData[eventId].Parameters[parameterId].ParameterType;
1795 if (dataType == typeof(IntPtr))
1797 return *((IntPtr*)dataPointer);
1799 else if (dataType == typeof(int))
1801 return *((int*)dataPointer);
1803 else if (dataType == typeof(uint))
1805 return *((uint*)dataPointer);
1807 else if (dataType == typeof(long))
1809 return *((long*)dataPointer);
1811 else if (dataType == typeof(ulong))
1813 return *((ulong*)dataPointer);
1815 else if (dataType == typeof(byte))
1817 return *((byte*)dataPointer);
1819 else if (dataType == typeof(sbyte))
1821 return *((sbyte*)dataPointer);
1823 else if (dataType == typeof(short))
1825 return *((short*)dataPointer);
1827 else if (dataType == typeof(ushort))
1829 return *((ushort*)dataPointer);
1831 else if (dataType == typeof(float))
1833 return *((float*)dataPointer);
1835 else if (dataType == typeof(double))
1837 return *((double*)dataPointer);
1839 else if (dataType == typeof(decimal))
1841 return *((decimal*)dataPointer);
1843 else if (dataType == typeof(bool))
1845 // The manifest defines a bool as a 32bit type (WIN32 BOOL), not 1 bit as CLR Does.
1846 if (*((int*)dataPointer) == 1)
1855 else if (dataType == typeof(Guid))
1857 return *((Guid*)dataPointer);
1859 else if (dataType == typeof(char))
1861 return *((char*)dataPointer);
1863 else if (dataType == typeof(DateTime))
1865 long dateTimeTicks = *((long*)dataPointer);
1866 return DateTime.FromFileTimeUtc(dateTimeTicks);
1868 else if (dataType == typeof(byte[]))
1870 // byte[] are written to EventData* as an int followed by a blob
1871 int cbSize = *((int*)dataPointer);
1872 byte[] blob = new byte[cbSize];
1873 dataPointer = data->DataPointer;
1875 for (int i = 0; i < cbSize; ++i)
1876 blob[i] = *((byte*)dataPointer);
1879 else if (dataType == typeof(byte*))
1881 // TODO: how do we want to handle this? For now we ignore it...
1886 if (dataType.IsEnum())
1888 dataType = Enum.GetUnderlyingType(dataType);
1892 // TODO FIX NOW Assuming that it is a string at this point is really likely to be fragile
1893 // We should do something better.
1895 // Everything else is marshaled as a string.
1896 // ETW strings are NULL-terminated, so marshal everything up to the first
1897 // null in the string.
1898 return System.Runtime.InteropServices.Marshal.PtrToStringUni(dataPointer);
1902 // Finds the Dispatcher (which holds the filtering state), for a given dispatcher for the current
1904 private EventDispatcher GetDispatcher(EventListener listener)
1906 EventDispatcher dispatcher = m_Dispatchers;
1907 while (dispatcher != null)
1909 if (dispatcher.m_Listener == listener)
1911 dispatcher = dispatcher.m_Next;
1917 private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object[] args)
1919 if (m_eventSourceEnabled)
1923 Contract.Assert(m_eventData != null); // You must have initialized this if you enabled the source.
1924 if (childActivityID != null)
1926 ValidateEventOpcodeForTransfer(ref m_eventData[eventId]);
1928 // If you use WriteEventWithRelatedActivityID you MUST declare the first argument to be a GUID
1929 // with the name 'relatedActivityID, and NOT pass this argument to the WriteEvent method.
1930 // During manifest creation we modify the ParameterInfo[] that we store to strip out any
1931 // first parameter that is of type Guid and named "relatedActivityId." Thus, if you call
1932 // WriteEventWithRelatedActivityID from a method that doesn't name its first parameter correctly
1933 // we can end up in a state where the ParameterInfo[] doesn't have its first parameter stripped,
1934 // and this leads to a mismatch between the number of arguments and the number of ParameterInfos,
1935 // which would cause a cryptic IndexOutOfRangeException later if we don't catch it here.
1936 if (!m_eventData[eventId].HasRelatedActivityID)
1938 throw new ArgumentException(Environment.GetResourceString("EventSource_NoRelatedActivityId"));
1942 LogEventArgsMismatches(m_eventData[eventId].Parameters, args);
1943 #if FEATURE_MANAGED_ETW
1944 if (m_eventData[eventId].EnabledForETW)
1946 Guid* pActivityId = null;
1947 Guid activityId = Guid.Empty;
1948 Guid relatedActivityId = Guid.Empty;
1949 EventOpcode opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode;
1950 EventActivityOptions activityOptions = m_eventData[eventId].ActivityOptions;
1952 if (childActivityID == null &&
1953 ((activityOptions & EventActivityOptions.Disable) == 0))
1955 if (opcode == EventOpcode.Start)
1957 m_activityTracker.OnStart(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId, ref relatedActivityId, m_eventData[eventId].ActivityOptions);
1959 else if (opcode == EventOpcode.Stop)
1961 m_activityTracker.OnStop(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId);
1964 if (activityId != Guid.Empty)
1965 pActivityId = &activityId;
1966 if (relatedActivityId != Guid.Empty)
1967 childActivityID = &relatedActivityId;
1970 #if FEATURE_ACTIVITYSAMPLING
1971 // this code should be kept in sync with WriteEventWithRelatedActivityIdCore().
1972 SessionMask etwSessions = SessionMask.All;
1973 // only compute etwSessions if there are *any* ETW filters enabled...
1974 if ((ulong)m_curLiveSessions != 0)
1975 etwSessions = GetEtwSessionMask(eventId, childActivityID);
1977 if ((ulong)etwSessions != 0 || m_legacySessions != null && m_legacySessions.Count > 0)
1979 if (!SelfDescribingEvents)
1981 if (etwSessions.IsEqualOrSupersetOf(m_curLiveSessions))
1983 // by default the Descriptor.Keyword will have the perEventSourceSessionId bit
1984 // mask set to 0x0f so, when all ETW sessions want the event we don't need to
1985 // synthesize a new one
1986 if (!m_provider.WriteEvent(ref m_eventData[eventId].Descriptor, pActivityId, childActivityID, args))
1987 ThrowEventSourceException();
1991 long origKwd = unchecked((long)((ulong)m_eventData[eventId].Descriptor.Keywords & ~(SessionMask.All.ToEventKeywords())));
1992 // only some of the ETW sessions will receive this event. Synthesize a new
1993 // Descriptor whose Keywords field will have the appropriate bits set.
1994 var desc = new EventDescriptor(
1995 m_eventData[eventId].Descriptor.EventId,
1996 m_eventData[eventId].Descriptor.Version,
1997 m_eventData[eventId].Descriptor.Channel,
1998 m_eventData[eventId].Descriptor.Level,
1999 m_eventData[eventId].Descriptor.Opcode,
2000 m_eventData[eventId].Descriptor.Task,
2001 unchecked((long)(ulong)etwSessions | origKwd));
2003 if (!m_provider.WriteEvent(ref desc, pActivityId, childActivityID, args))
2004 ThrowEventSourceException();
2009 TraceLoggingEventTypes tlet = m_eventData[eventId].TraceLoggingEventTypes;
2012 tlet = new TraceLoggingEventTypes(m_eventData[eventId].Name,
2014 m_eventData[eventId].Parameters);
2015 Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, tlet, null);
2018 long origKwd = unchecked((long)((ulong)m_eventData[eventId].Descriptor.Keywords & ~(SessionMask.All.ToEventKeywords())));
2019 // TODO: activity ID support
2020 EventSourceOptions opt = new EventSourceOptions
2022 Keywords = (EventKeywords)unchecked((long)(ulong)etwSessions | origKwd),
2023 Level = (EventLevel)m_eventData[eventId].Descriptor.Level,
2024 Opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode
2027 WriteMultiMerge(m_eventData[eventId].Name, ref opt, tlet, pActivityId, childActivityID, args);
2031 if (!SelfDescribingEvents)
2033 if (!m_provider.WriteEvent(ref m_eventData[eventId].Descriptor, pActivityId, childActivityID, args))
2034 ThrowEventSourceException();
2038 TraceLoggingEventTypes tlet = m_eventData[eventId].TraceLoggingEventTypes;
2041 tlet = new TraceLoggingEventTypes(m_eventData[eventId].Name,
2043 m_eventData[eventId].Parameters);
2044 Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, tlet, null);
2047 // TODO: activity ID support
2048 EventSourceOptions opt = new EventSourceOptions
2050 Keywords = (EventKeywords)m_eventData[eventId].Descriptor.Keywords,
2051 Level = (EventLevel)m_eventData[eventId].Descriptor.Level,
2052 Opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode
2055 WriteMultiMerge(m_eventData[eventId].Name, ref opt, tlet, pActivityId, childActivityID, args);
2057 #endif // FEATURE_ACTIVITYSAMPLING
2059 #endif // FEATURE_MANAGED_ETW
2060 if (m_Dispatchers != null && m_eventData[eventId].EnabledForAnyListener)
2062 #if !ES_BUILD_STANDALONE
2063 // Maintain old behavior - object identity is preserved
2064 if (AppContextSwitches.PreserveEventListnerObjectIdentity)
2066 WriteToAllListeners(eventId, childActivityID, args);
2069 #endif // !ES_BUILD_STANDALONE
2071 object[] serializedArgs = SerializeEventArgs(eventId, args);
2072 WriteToAllListeners(eventId, childActivityID, serializedArgs);
2076 catch (Exception ex)
2078 if (ex is EventSourceException)
2081 ThrowEventSourceException(ex);
2087 unsafe private object[] SerializeEventArgs(int eventId, object[] args)
2089 TraceLoggingEventTypes eventTypes = m_eventData[eventId].TraceLoggingEventTypes;
2090 if (eventTypes == null)
2092 eventTypes = new TraceLoggingEventTypes(m_eventData[eventId].Name,
2094 m_eventData[eventId].Parameters);
2095 Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, eventTypes, null);
2097 var eventData = new object[eventTypes.typeInfos.Length];
2098 for (int i = 0; i < eventTypes.typeInfos.Length; i++)
2100 eventData[i] = eventTypes.typeInfos[i].GetData(args[i]);
2106 /// We expect that the arguments to the Event method and the arguments to WriteEvent match. This function
2107 /// checks that they in fact match and logs a warning to the debugger if they don't.
2109 /// <param name="infos"></param>
2110 /// <param name="args"></param>
2111 private void LogEventArgsMismatches(ParameterInfo[] infos, object[] args)
2114 // It would be nice to have this on PCL builds, but it would be pointless since there isn't support for
2115 // writing to the debugger log on PCL.
2116 bool typesMatch = args.Length == infos.Length;
2119 while (typesMatch && i < args.Length)
2121 Type pType = infos[i].ParameterType;
2123 // Checking to see if the Parameter types (from the Event method) match the supplied argument types.
2124 // Fail if one of two things hold : either the argument type is not equal to the parameter type, or the
2125 // argument is null and the parameter type is non-nullable.
2126 if ((args[i] != null && (args[i].GetType() != pType))
2127 || (args[i] == null && (!(pType.IsGenericType && pType.GetGenericTypeDefinition() == typeof(Nullable<>))))
2139 System.Diagnostics.Debugger.Log(0, null, Environment.GetResourceString("EventSource_VarArgsParameterMismatch") + "\r\n");
2141 #endif //!ES_BUILD_PCL
2145 unsafe private void WriteToAllListeners(int eventId, Guid* childActivityID, int eventDataCount, EventSource.EventData* data)
2147 int paramCount = m_eventData[eventId].Parameters.Length;
2148 if (eventDataCount != paramCount)
2150 ReportOutOfBandMessage(Environment.GetResourceString("EventSource_EventParametersMismatch", eventId, eventDataCount, paramCount), true);
2151 paramCount = Math.Min(paramCount, eventDataCount);
2154 object[] args = new object[paramCount];
2156 EventSource.EventData* dataPtr = data;
2157 for (int i = 0; i < paramCount; i++)
2158 args[i] = DecodeObject(eventId, i, ref dataPtr);
2159 WriteToAllListeners(eventId, childActivityID, args);
2162 // helper for writing to all EventListeners attached the current eventSource.
2164 unsafe private void WriteToAllListeners(int eventId, Guid* childActivityID, params object[] args)
2166 EventWrittenEventArgs eventCallbackArgs = new EventWrittenEventArgs(this);
2167 eventCallbackArgs.EventId = eventId;
2168 if (childActivityID != null)
2169 eventCallbackArgs.RelatedActivityId = *childActivityID;
2170 eventCallbackArgs.EventName = m_eventData[eventId].Name;
2171 eventCallbackArgs.Message = m_eventData[eventId].Message;
2172 eventCallbackArgs.Payload = new ReadOnlyCollection<object>(args);
2174 DispatchToAllListeners(eventId, childActivityID, eventCallbackArgs);
2178 private unsafe void DispatchToAllListeners(int eventId, Guid* childActivityID, EventWrittenEventArgs eventCallbackArgs)
2180 Exception lastThrownException = null;
2181 for (EventDispatcher dispatcher = m_Dispatchers; dispatcher != null; dispatcher = dispatcher.m_Next)
2183 Contract.Assert(dispatcher.m_EventEnabled != null);
2184 if (eventId == -1 || dispatcher.m_EventEnabled[eventId])
2186 #if FEATURE_ACTIVITYSAMPLING
2187 var activityFilter = dispatcher.m_Listener.m_activityFilter;
2188 // order below is important as PassesActivityFilter will "flow" active activities
2189 // even when the current EventSource doesn't have filtering enabled. This allows
2190 // interesting activities to be updated so that sources that do sample can get
2192 if (activityFilter == null ||
2193 ActivityFilter.PassesActivityFilter(activityFilter, childActivityID,
2194 m_eventData[eventId].TriggersActivityTracking > 0,
2196 !dispatcher.m_activityFilteringEnabled)
2197 #endif // FEATURE_ACTIVITYSAMPLING
2201 dispatcher.m_Listener.OnEventWritten(eventCallbackArgs);
2205 ReportOutOfBandMessage("ERROR: Exception during EventSource.OnEventWritten: "
2206 + e.Message, false);
2207 lastThrownException = e;
2213 if (lastThrownException != null)
2215 throw new EventSourceException(lastThrownException);
2219 [SecuritySafeCritical]
2220 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
2221 private unsafe void WriteEventString(EventLevel level, long keywords, string msgString)
2223 #if FEATURE_MANAGED_ETW
2224 if (m_provider != null)
2226 string eventName = "EventSourceMessage";
2227 if (SelfDescribingEvents)
2229 EventSourceOptions opt = new EventSourceOptions
2231 Keywords = (EventKeywords)unchecked(keywords),
2234 var msg = new { message = msgString };
2235 var tlet = new TraceLoggingEventTypes(eventName, EventTags.None, new Type[] { msg.GetType() });
2236 WriteMultiMergeInner(eventName, ref opt, tlet, null, null, msg);
2240 // We want the name of the provider to show up so if we don't have a manifest we create
2241 // on that at least has the provider name (I don't define any events).
2242 if (m_rawManifest == null && m_outOfBandMessageCount == 1)
2244 ManifestBuilder manifestBuilder = new ManifestBuilder(Name, Guid, Name, null, EventManifestOptions.None);
2245 manifestBuilder.StartEvent(eventName, new EventAttribute(0) { Level = EventLevel.LogAlways, Task = (EventTask)0xFFFE });
2246 manifestBuilder.AddEventParameter(typeof(string), "message");
2247 manifestBuilder.EndEvent();
2248 SendManifest(manifestBuilder.CreateManifest());
2251 // We use this low level routine to to bypass the enabled checking, since the eventSource itself is only partially inited.
2252 fixed (char* msgStringPtr = msgString)
2254 EventDescriptor descr = new EventDescriptor(0, 0, 0, (byte)level, 0, 0, keywords);
2255 EventProvider.EventData data = new EventProvider.EventData();
2256 data.Ptr = (ulong)msgStringPtr;
2257 data.Size = (uint)(2 * (msgString.Length + 1));
2259 m_provider.WriteEvent(ref descr, null, null, 1, (IntPtr)((void*)&data));
2263 #endif // FEATURE_MANAGED_ETW
2267 /// Since this is a means of reporting errors (see ReportoutOfBandMessage) any failure encountered
2268 /// while writing the message to any one of the listeners will be silently ignored.
2270 private void WriteStringToAllListeners(string eventName, string msg)
2272 EventWrittenEventArgs eventCallbackArgs = new EventWrittenEventArgs(this);
2273 eventCallbackArgs.EventId = 0;
2274 eventCallbackArgs.Message = msg;
2275 eventCallbackArgs.Payload = new ReadOnlyCollection<object>(new List<object>() { msg });
2276 eventCallbackArgs.PayloadNames = new ReadOnlyCollection<string>(new List<string> { "message" });
2277 eventCallbackArgs.EventName = eventName;
2279 for (EventDispatcher dispatcher = m_Dispatchers; dispatcher != null; dispatcher = dispatcher.m_Next)
2281 bool dispatcherEnabled = false;
2282 if (dispatcher.m_EventEnabled == null)
2284 // if the listeners that weren't correctly initialized, we will send to it
2285 // since this is an error message and we want to see it go out.
2286 dispatcherEnabled = true;
2290 // if there's *any* enabled event on the dispatcher we'll write out the string
2291 // otherwise we'll treat the listener as disabled and skip it
2292 for (int evtId = 0; evtId < dispatcher.m_EventEnabled.Length; ++evtId)
2294 if (dispatcher.m_EventEnabled[evtId])
2296 dispatcherEnabled = true;
2303 if (dispatcherEnabled)
2304 dispatcher.m_Listener.OnEventWritten(eventCallbackArgs);
2308 // ignore any exceptions thrown by listeners' OnEventWritten
2313 #if FEATURE_ACTIVITYSAMPLING
2315 unsafe private SessionMask GetEtwSessionMask(int eventId, Guid* childActivityID)
2317 SessionMask etwSessions = new SessionMask();
2319 for (int i = 0; i < SessionMask.MAX; ++i)
2321 EtwSession etwSession = m_etwSessionIdMap[i];
2322 if (etwSession != null)
2324 ActivityFilter activityFilter = etwSession.m_activityFilter;
2325 // PassesActivityFilter() will flow "interesting" activities, so make sure
2326 // to perform this test first, before ORing with ~m_activityFilteringForETWEnabled
2327 // (note: the first test for !m_activityFilteringForETWEnabled[i] ensures we
2328 // do not fire events indiscriminately, when no filters are specified, but only
2329 // if, in addition, the session did not also enable ActivitySampling)
2330 if (activityFilter == null && !m_activityFilteringForETWEnabled[i] ||
2331 activityFilter != null &&
2332 ActivityFilter.PassesActivityFilter(activityFilter, childActivityID,
2333 m_eventData[eventId].TriggersActivityTracking > 0, this, eventId) ||
2334 !m_activityFilteringForETWEnabled[i])
2336 etwSessions[i] = true;
2340 // flow "interesting" activities for all legacy sessions in which there's some
2341 // level of activity tracing enabled (even other EventSources)
2342 if (m_legacySessions != null && m_legacySessions.Count > 0 &&
2343 (EventOpcode)m_eventData[eventId].Descriptor.Opcode == EventOpcode.Send)
2345 // only calculate InternalCurrentThreadActivityId once
2346 Guid* pCurrentActivityId = null;
2347 Guid currentActivityId;
2348 foreach (var legacyEtwSession in m_legacySessions)
2350 if (legacyEtwSession == null)
2353 ActivityFilter activityFilter = legacyEtwSession.m_activityFilter;
2354 if (activityFilter != null)
2356 if (pCurrentActivityId == null)
2358 currentActivityId = InternalCurrentThreadActivityId;
2359 pCurrentActivityId = ¤tActivityId;
2361 ActivityFilter.FlowActivityIfNeeded(activityFilter, pCurrentActivityId, childActivityID);
2368 #endif // FEATURE_ACTIVITYSAMPLING
2371 /// Returns true if 'eventNum' is enabled if you only consider the level and matchAnyKeyword filters.
2372 /// It is possible that eventSources turn off the event based on additional filtering criteria.
2374 private bool IsEnabledByDefault(int eventNum, bool enable, EventLevel currentLevel, EventKeywords currentMatchAnyKeyword)
2379 EventLevel eventLevel = (EventLevel)m_eventData[eventNum].Descriptor.Level;
2380 EventKeywords eventKeywords = unchecked((EventKeywords)((ulong)m_eventData[eventNum].Descriptor.Keywords & (~(SessionMask.All.ToEventKeywords()))));
2382 #if FEATURE_MANAGED_ETW_CHANNELS
2383 EventChannel channel = unchecked((EventChannel)m_eventData[eventNum].Descriptor.Channel);
2385 EventChannel channel = EventChannel.None;
2388 return IsEnabledCommon(enable, currentLevel, currentMatchAnyKeyword, eventLevel, eventKeywords, channel);
2391 private bool IsEnabledCommon(bool enabled, EventLevel currentLevel, EventKeywords currentMatchAnyKeyword,
2392 EventLevel eventLevel, EventKeywords eventKeywords, EventChannel eventChannel)
2397 // does is pass the level test?
2398 if ((currentLevel != 0) && (currentLevel < eventLevel))
2401 // if yes, does it pass the keywords test?
2402 if (currentMatchAnyKeyword != 0 && eventKeywords != 0)
2404 #if FEATURE_MANAGED_ETW_CHANNELS
2405 // is there a channel with keywords that match currentMatchAnyKeyword?
2406 if (eventChannel != EventChannel.None && this.m_channelData != null && this.m_channelData.Length > (int)eventChannel)
2408 EventKeywords channel_keywords = unchecked((EventKeywords)(m_channelData[(int)eventChannel] | (ulong)eventKeywords));
2409 if (channel_keywords != 0 && (channel_keywords & currentMatchAnyKeyword) == 0)
2415 if ((unchecked((ulong)eventKeywords & (ulong)currentMatchAnyKeyword)) == 0)
2422 [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
2423 private void ThrowEventSourceException(Exception innerEx = null)
2425 // If we fail during out of band logging we may end up trying
2426 // to throw another EventSourceException, thus hitting a StackOverflowException.
2427 // Avoid StackOverflow by making sure we do not recursively call this method.
2428 if (m_EventSourceExceptionRecurenceCount > 0)
2432 m_EventSourceExceptionRecurenceCount++;
2434 // TODO Create variations of EventSourceException that indicate more information using the error code.
2435 switch (EventProvider.GetLastWriteEventError())
2437 case EventProvider.WriteEventErrorCode.EventTooBig:
2438 ReportOutOfBandMessage("EventSourceException: " + Environment.GetResourceString("EventSource_EventTooBig"), true);
2439 if (ThrowOnEventWriteErrors) throw new EventSourceException(Environment.GetResourceString("EventSource_EventTooBig"), innerEx);
2441 case EventProvider.WriteEventErrorCode.NoFreeBuffers:
2442 ReportOutOfBandMessage("EventSourceException: " + Environment.GetResourceString("EventSource_NoFreeBuffers"), true);
2443 if (ThrowOnEventWriteErrors) throw new EventSourceException(Environment.GetResourceString("EventSource_NoFreeBuffers"), innerEx);
2445 case EventProvider.WriteEventErrorCode.NullInput:
2446 ReportOutOfBandMessage("EventSourceException: " + Environment.GetResourceString("EventSource_NullInput"), true);
2447 if (ThrowOnEventWriteErrors) throw new EventSourceException(Environment.GetResourceString("EventSource_NullInput"), innerEx);
2449 case EventProvider.WriteEventErrorCode.TooManyArgs:
2450 ReportOutOfBandMessage("EventSourceException: " + Environment.GetResourceString("EventSource_TooManyArgs"), true);
2451 if (ThrowOnEventWriteErrors) throw new EventSourceException(Environment.GetResourceString("EventSource_TooManyArgs"), innerEx);
2454 if (innerEx != null)
2455 ReportOutOfBandMessage("EventSourceException: " + innerEx.GetType() + ":" + innerEx.Message, true);
2457 ReportOutOfBandMessage("EventSourceException", true);
2458 if (ThrowOnEventWriteErrors) throw new EventSourceException(innerEx);
2464 m_EventSourceExceptionRecurenceCount--;
2468 private void ValidateEventOpcodeForTransfer(ref EventMetadata eventData)
2470 if ((EventOpcode)eventData.Descriptor.Opcode != EventOpcode.Send &&
2471 (EventOpcode)eventData.Descriptor.Opcode != EventOpcode.Receive &&
2472 (EventOpcode)eventData.Descriptor.Opcode != EventOpcode.Start)
2474 ThrowEventSourceException();
2478 internal static EventOpcode GetOpcodeWithDefault(EventOpcode opcode, string eventName)
2480 if (opcode == EventOpcode.Info && eventName != null)
2482 if (eventName.EndsWith(s_ActivityStartSuffix))
2484 return EventOpcode.Start;
2486 else if (eventName.EndsWith(s_ActivityStopSuffix))
2488 return EventOpcode.Stop;
2495 #if FEATURE_MANAGED_ETW
2497 /// This class lets us hook the 'OnEventCommand' from the eventSource.
2499 private class OverideEventProvider : EventProvider
2501 public OverideEventProvider(EventSource eventSource)
2503 this.m_eventSource = eventSource;
2505 protected override void OnControllerCommand(ControllerCommand command, IDictionary<string, string> arguments,
2506 int perEventSourceSessionId, int etwSessionId)
2508 // We use null to represent the ETW EventListener.
2509 EventListener listener = null;
2510 m_eventSource.SendCommand(listener, perEventSourceSessionId, etwSessionId,
2511 (EventCommand)command, IsEnabled(), Level, MatchAnyKeyword, arguments);
2513 private EventSource m_eventSource;
2518 /// Used to hold all the static information about an event. This includes everything in the event
2519 /// descriptor as well as some stuff we added specifically for EventSource. see the
2520 /// code:m_eventData for where we use this.
2522 internal struct EventMetadata
2524 public EventDescriptor Descriptor;
2525 public EventTags Tags;
2526 public bool EnabledForAnyListener; // true if any dispatcher has this event turned on
2527 public bool EnabledForETW; // is this event on for the OS ETW data dispatcher?
2529 public bool HasRelatedActivityID; // Set if the event method's first parameter is a Guid named 'relatedActivityId'
2530 #if !FEATURE_ACTIVITYSAMPLING
2531 #pragma warning disable 0649
2533 public byte TriggersActivityTracking; // count of listeners that marked this event as trigger for start of activity logging.
2534 #if !FEATURE_ACTIVITYSAMPLING
2535 #pragma warning restore 0649
2537 public string Name; // the name of the event
2538 public string Message; // If the event has a message associated with it, this is it.
2539 public ParameterInfo[] Parameters; // TODO can we remove?
2541 public TraceLoggingEventTypes TraceLoggingEventTypes;
2542 public EventActivityOptions ActivityOptions;
2545 // This is the internal entry point that code:EventListeners call when wanting to send a command to a
2546 // eventSource. The logic is as follows
2548 // * if Command == Update
2549 // * perEventSourceSessionId specifies the per-provider ETW session ID that the command applies
2550 // to (if listener != null)
2551 // perEventSourceSessionId = 0 - reserved for EventListeners
2552 // perEventSourceSessionId = 1..SessionMask.MAX - reserved for activity tracing aware ETW sessions
2553 // perEventSourceSessionId-1 represents the bit in the reserved field (bits 44..47) in
2554 // Keywords that identifies the session
2555 // perEventSourceSessionId = SessionMask.MAX+1 - reserved for legacy ETW sessions; these are
2556 // discriminated by etwSessionId
2557 // * etwSessionId specifies a machine-wide ETW session ID; this allows correlation of
2558 // activity tracing across different providers (which might have different sessionIds
2559 // for the same ETW session)
2560 // * enable, level, matchAnyKeywords are used to set a default for all events for the
2561 // eventSource. In particular, if 'enabled' is false, 'level' and
2562 // 'matchAnyKeywords' are not used.
2563 // * OnEventCommand is invoked, which may cause calls to
2564 // code:EventSource.EnableEventForDispatcher which may cause changes in the filtering
2565 // depending on the logic in that routine.
2566 // * else (command != Update)
2567 // * Simply call OnEventCommand. The expectation is that filtering is NOT changed.
2568 // * The 'enabled' 'level', matchAnyKeyword' arguments are ignored (must be true, 0, 0).
2570 // dispatcher == null has special meaning. It is the 'ETW' dispatcher.
2571 internal void SendCommand(EventListener listener, int perEventSourceSessionId, int etwSessionId,
2572 EventCommand command, bool enable,
2573 EventLevel level, EventKeywords matchAnyKeyword,
2574 IDictionary<string, string> commandArguments)
2576 var commandArgs = new EventCommandEventArgs(command, commandArguments, this, listener, perEventSourceSessionId, etwSessionId, enable, level, matchAnyKeyword);
2577 lock (EventListener.EventListenersLock)
2579 if (m_completelyInited)
2581 // After the first command arrive after construction, we are ready to get rid of the deferred commands
2582 this.m_deferredCommands = null;
2583 // We are fully initialized, do the command
2584 DoCommand(commandArgs);
2588 // We can't do the command, simply remember it and we do it when we are fully constructed.
2589 commandArgs.nextCommand = m_deferredCommands;
2590 m_deferredCommands = commandArgs;
2596 /// We want the eventSource to be fully initialized when we do commands because that way we can send
2597 /// error messages and other logging directly to the event stream. Unfortunately we can get callbacks
2598 /// when we are not fully initialized. In that case we store them in 'commandArgs' and do them later.
2599 /// This helper actually does all actual command logic.
2601 internal void DoCommand(EventCommandEventArgs commandArgs)
2603 // PRECONDITION: We should be holding the EventListener.EventListenersLock
2604 // We defer commands until we are completely inited. This allows error messages to be sent.
2605 Contract.Assert(m_completelyInited);
2607 #if FEATURE_MANAGED_ETW
2608 if (m_provider == null) // If we failed to construct
2610 #endif // FEATURE_MANAGED_ETW
2612 m_outOfBandMessageCount = 0;
2613 bool shouldReport = (commandArgs.perEventSourceSessionId > 0) && (commandArgs.perEventSourceSessionId <= SessionMask.MAX);
2616 EnsureDescriptorsInitialized();
2617 Contract.Assert(m_eventData != null);
2619 // Find the per-EventSource dispatcher corresponding to registered dispatcher
2620 commandArgs.dispatcher = GetDispatcher(commandArgs.listener);
2621 if (commandArgs.dispatcher == null && commandArgs.listener != null) // dispatcher == null means ETW dispatcher
2622 throw new ArgumentException(Environment.GetResourceString("EventSource_ListenerNotFound"));
2624 if (commandArgs.Arguments == null)
2625 commandArgs.Arguments = new Dictionary<string, string>();
2627 if (commandArgs.Command == EventCommand.Update)
2629 // Set it up using the 'standard' filtering bitfields (use the "global" enable, not session specific one)
2630 for (int i = 0; i < m_eventData.Length; i++)
2631 EnableEventForDispatcher(commandArgs.dispatcher, i, IsEnabledByDefault(i, commandArgs.enable, commandArgs.level, commandArgs.matchAnyKeyword));
2633 if (commandArgs.enable)
2635 if (!m_eventSourceEnabled)
2637 // EventSource turned on for the first time, simply copy the bits.
2638 m_level = commandArgs.level;
2639 m_matchAnyKeyword = commandArgs.matchAnyKeyword;
2643 // Already enabled, make it the most verbose of the existing and new filter
2644 if (commandArgs.level > m_level)
2645 m_level = commandArgs.level;
2646 if (commandArgs.matchAnyKeyword == 0)
2647 m_matchAnyKeyword = 0;
2648 else if (m_matchAnyKeyword != 0)
2649 m_matchAnyKeyword = unchecked(m_matchAnyKeyword | commandArgs.matchAnyKeyword);
2653 // interpret perEventSourceSessionId's sign, and adjust perEventSourceSessionId to
2654 // represent 0-based positive values
2655 bool bSessionEnable = (commandArgs.perEventSourceSessionId >= 0);
2656 if (commandArgs.perEventSourceSessionId == 0 && commandArgs.enable == false)
2657 bSessionEnable = false;
2659 if (commandArgs.listener == null)
2661 if (!bSessionEnable)
2662 commandArgs.perEventSourceSessionId = -commandArgs.perEventSourceSessionId;
2663 // for "global" enable/disable (passed in with listener == null and
2664 // perEventSourceSessionId == 0) perEventSourceSessionId becomes -1
2665 --commandArgs.perEventSourceSessionId;
2668 commandArgs.Command = bSessionEnable ? EventCommand.Enable : EventCommand.Disable;
2670 // perEventSourceSessionId = -1 when ETW sent a notification, but the set of active sessions
2672 // sesisonId = SessionMask.MAX when one of the legacy ETW sessions changed
2673 // 0 <= perEventSourceSessionId < SessionMask.MAX for activity-tracing aware sessions
2674 Contract.Assert(commandArgs.perEventSourceSessionId >= -1 && commandArgs.perEventSourceSessionId <= SessionMask.MAX);
2676 // Send the manifest if we are enabling an ETW session
2677 if (bSessionEnable && commandArgs.dispatcher == null)
2679 // eventSourceDispatcher == null means this is the ETW manifest
2681 // Note that we unconditionally send the manifest whenever we are enabled, even if
2682 // we were already enabled. This is because there may be multiple sessions active
2683 // and we can't know that all the sessions have seen the manifest.
2684 if (!SelfDescribingEvents)
2685 SendManifest(m_rawManifest);
2688 #if FEATURE_ACTIVITYSAMPLING
2689 if (bSessionEnable && commandArgs.perEventSourceSessionId != -1)
2691 bool participateInSampling = false;
2692 string activityFilters;
2695 ParseCommandArgs(commandArgs.Arguments, out participateInSampling,
2696 out activityFilters, out sessionIdBit);
2698 if (commandArgs.listener == null && commandArgs.Arguments.Count > 0 && commandArgs.perEventSourceSessionId != sessionIdBit)
2700 throw new ArgumentException(Environment.GetResourceString("EventSource_SessionIdError",
2701 commandArgs.perEventSourceSessionId + SessionMask.SHIFT_SESSION_TO_KEYWORD,
2702 sessionIdBit + SessionMask.SHIFT_SESSION_TO_KEYWORD));
2705 if (commandArgs.listener == null)
2707 UpdateEtwSession(commandArgs.perEventSourceSessionId, commandArgs.etwSessionId, true, activityFilters, participateInSampling);
2711 ActivityFilter.UpdateFilter(ref commandArgs.listener.m_activityFilter, this, 0, activityFilters);
2712 commandArgs.dispatcher.m_activityFilteringEnabled = participateInSampling;
2715 else if (!bSessionEnable && commandArgs.listener == null)
2717 // if we disable an ETW session, indicate that in a synthesized command argument
2718 if (commandArgs.perEventSourceSessionId >= 0 && commandArgs.perEventSourceSessionId < SessionMask.MAX)
2720 commandArgs.Arguments["EtwSessionKeyword"] = (commandArgs.perEventSourceSessionId + SessionMask.SHIFT_SESSION_TO_KEYWORD).ToString(CultureInfo.InvariantCulture);
2723 #endif // FEATURE_ACTIVITYSAMPLING
2725 // Turn on the enable bit before making the OnEventCommand callback This allows you to do useful
2726 // things like log messages, or test if keywords are enabled in the callback.
2727 if (commandArgs.enable)
2729 Contract.Assert(m_eventData != null);
2730 m_eventSourceEnabled = true;
2733 this.OnEventCommand(commandArgs);
2734 var eventCommandCallback = this.m_eventCommandExecuted;
2735 if (eventCommandCallback != null)
2736 eventCommandCallback(this, commandArgs);
2738 #if FEATURE_ACTIVITYSAMPLING
2739 if (commandArgs.listener == null && !bSessionEnable && commandArgs.perEventSourceSessionId != -1)
2741 // if we disable an ETW session, complete disabling it
2742 UpdateEtwSession(commandArgs.perEventSourceSessionId, commandArgs.etwSessionId, false, null, false);
2744 #endif // FEATURE_ACTIVITYSAMPLING
2746 if (!commandArgs.enable)
2748 // If we are disabling, maybe we can turn on 'quick checks' to filter
2749 // quickly. These are all just optimizations (since later checks will still filter)
2751 #if FEATURE_ACTIVITYSAMPLING
2752 // Turn off (and forget) any information about Activity Tracing.
2753 if (commandArgs.listener == null)
2755 // reset all filtering information for activity-tracing-aware sessions
2756 for (int i = 0; i < SessionMask.MAX; ++i)
2758 EtwSession etwSession = m_etwSessionIdMap[i];
2759 if (etwSession != null)
2760 ActivityFilter.DisableFilter(ref etwSession.m_activityFilter, this);
2762 m_activityFilteringForETWEnabled = new SessionMask(0);
2763 m_curLiveSessions = new SessionMask(0);
2764 // reset activity-tracing-aware sessions
2765 if (m_etwSessionIdMap != null)
2766 for (int i = 0; i < SessionMask.MAX; ++i)
2767 m_etwSessionIdMap[i] = null;
2768 // reset legacy sessions
2769 if (m_legacySessions != null)
2770 m_legacySessions.Clear();
2774 ActivityFilter.DisableFilter(ref commandArgs.listener.m_activityFilter, this);
2775 commandArgs.dispatcher.m_activityFilteringEnabled = false;
2777 #endif // FEATURE_ACTIVITYSAMPLING
2779 // There is a good chance EnabledForAnyListener are not as accurate as
2780 // they could be, go ahead and get a better estimate.
2781 for (int i = 0; i < m_eventData.Length; i++)
2783 bool isEnabledForAnyListener = false;
2784 for (EventDispatcher dispatcher = m_Dispatchers; dispatcher != null; dispatcher = dispatcher.m_Next)
2786 if (dispatcher.m_EventEnabled[i])
2788 isEnabledForAnyListener = true;
2792 m_eventData[i].EnabledForAnyListener = isEnabledForAnyListener;
2795 // If no events are enabled, disable the global enabled bit.
2796 if (!AnyEventEnabled())
2799 m_matchAnyKeyword = 0;
2800 m_eventSourceEnabled = false;
2803 #if FEATURE_ACTIVITYSAMPLING
2804 UpdateKwdTriggers(commandArgs.enable);
2805 #endif // FEATURE_ACTIVITYSAMPLING
2809 if (commandArgs.Command == EventCommand.SendManifest)
2811 // TODO: should we generate the manifest here if we hadn't already?
2812 if (m_rawManifest != null)
2813 SendManifest(m_rawManifest);
2816 // These are not used for non-update commands and thus should always be 'default' values
2817 // Contract.Assert(enable == true);
2818 // Contract.Assert(level == EventLevel.LogAlways);
2819 // Contract.Assert(matchAnyKeyword == EventKeywords.None);
2821 this.OnEventCommand(commandArgs);
2822 var eventCommandCallback = m_eventCommandExecuted;
2823 if (eventCommandCallback != null)
2824 eventCommandCallback(this, commandArgs);
2827 #if FEATURE_ACTIVITYSAMPLING
2828 if (m_completelyInited && (commandArgs.listener != null || shouldReport))
2830 SessionMask m = SessionMask.FromId(commandArgs.perEventSourceSessionId);
2831 ReportActivitySamplingInfo(commandArgs.listener, m);
2833 #endif // FEATURE_ACTIVITYSAMPLING
2837 // When the ETW session is created after the EventSource has registered with the ETW system
2838 // we can send any error messages here.
2839 ReportOutOfBandMessage("ERROR: Exception in Command Processing for EventSource " + Name + ": " + e.Message, true);
2840 // We never throw when doing a command.
2844 #if FEATURE_ACTIVITYSAMPLING
2846 internal void UpdateEtwSession(
2850 string activityFilters,
2851 bool participateInSampling)
2853 if (sessionIdBit < SessionMask.MAX)
2855 // activity-tracing-aware etw session
2858 var etwSession = EtwSession.GetEtwSession(etwSessionId, true);
2859 ActivityFilter.UpdateFilter(ref etwSession.m_activityFilter, this, sessionIdBit, activityFilters);
2860 m_etwSessionIdMap[sessionIdBit] = etwSession;
2861 m_activityFilteringForETWEnabled[sessionIdBit] = participateInSampling;
2865 var etwSession = EtwSession.GetEtwSession(etwSessionId);
2866 m_etwSessionIdMap[sessionIdBit] = null;
2867 m_activityFilteringForETWEnabled[sessionIdBit] = false;
2868 if (etwSession != null)
2870 ActivityFilter.DisableFilter(ref etwSession.m_activityFilter, this);
2871 // the ETW session is going away; remove it from the global list
2872 EtwSession.RemoveEtwSession(etwSession);
2875 m_curLiveSessions[sessionIdBit] = bEnable;
2879 // legacy etw session
2882 if (m_legacySessions == null)
2883 m_legacySessions = new List<EtwSession>(8);
2884 var etwSession = EtwSession.GetEtwSession(etwSessionId, true);
2885 if (!m_legacySessions.Contains(etwSession))
2886 m_legacySessions.Add(etwSession);
2890 var etwSession = EtwSession.GetEtwSession(etwSessionId);
2891 if (etwSession != null)
2893 if (m_legacySessions != null)
2894 m_legacySessions.Remove(etwSession);
2895 // the ETW session is going away; remove it from the global list
2896 EtwSession.RemoveEtwSession(etwSession);
2902 internal static bool ParseCommandArgs(
2903 IDictionary<string, string> commandArguments,
2904 out bool participateInSampling,
2905 out string activityFilters,
2906 out int sessionIdBit)
2909 participateInSampling = false;
2910 string activityFilterString;
2911 if (commandArguments.TryGetValue("ActivitySamplingStartEvent", out activityFilters))
2913 // if a start event is specified default the event source to participate in sampling
2914 participateInSampling = true;
2917 if (commandArguments.TryGetValue("ActivitySampling", out activityFilterString))
2919 if (string.Compare(activityFilterString, "false", StringComparison.OrdinalIgnoreCase) == 0 ||
2920 activityFilterString == "0")
2921 participateInSampling = false;
2923 participateInSampling = true;
2927 int sessionKwd = -1;
2928 if (!commandArguments.TryGetValue("EtwSessionKeyword", out sSessionKwd) ||
2929 !int.TryParse(sSessionKwd, out sessionKwd) ||
2930 sessionKwd < SessionMask.SHIFT_SESSION_TO_KEYWORD ||
2931 sessionKwd >= SessionMask.SHIFT_SESSION_TO_KEYWORD + SessionMask.MAX)
2938 sessionIdBit = sessionKwd - SessionMask.SHIFT_SESSION_TO_KEYWORD;
2943 internal void UpdateKwdTriggers(bool enable)
2947 // recompute m_keywordTriggers
2948 ulong gKeywords = unchecked((ulong)m_matchAnyKeyword);
2950 gKeywords = 0xFFFFffffFFFFffff;
2952 m_keywordTriggers = 0;
2953 for (int sessId = 0; sessId < SessionMask.MAX; ++sessId)
2955 EtwSession etwSession = m_etwSessionIdMap[sessId];
2956 if (etwSession == null)
2959 ActivityFilter activityFilter = etwSession.m_activityFilter;
2960 ActivityFilter.UpdateKwdTriggers(activityFilter, m_guid, this, unchecked((EventKeywords)gKeywords));
2965 m_keywordTriggers = 0;
2969 #endif // FEATURE_ACTIVITYSAMPLING
2972 /// If 'value is 'true' then set the eventSource so that 'dispatcher' will receive event with the eventId
2973 /// of 'eventId. If value is 'false' disable the event for that dispatcher. If 'eventId' is out of
2974 /// range return false, otherwise true.
2976 internal bool EnableEventForDispatcher(EventDispatcher dispatcher, int eventId, bool value)
2978 if (dispatcher == null)
2980 if (eventId >= m_eventData.Length)
2982 #if FEATURE_MANAGED_ETW
2983 if (m_provider != null)
2984 m_eventData[eventId].EnabledForETW = value;
2989 if (eventId >= dispatcher.m_EventEnabled.Length)
2991 dispatcher.m_EventEnabled[eventId] = value;
2993 m_eventData[eventId].EnabledForAnyListener = true;
2999 /// Returns true if any event at all is on.
3001 private bool AnyEventEnabled()
3003 for (int i = 0; i < m_eventData.Length; i++)
3004 if (m_eventData[i].EnabledForETW || m_eventData[i].EnabledForAnyListener)
3009 private bool IsDisposed
3011 #if FEATURE_MANAGED_ETW
3012 get { return m_provider == null || m_provider.m_disposed; }
3014 get { return false; } // ETW is not present (true means that the EventSource is "off" / broken)
3015 #endif // FEATURE_MANAGED_ETW
3018 [SecuritySafeCritical]
3019 private void EnsureDescriptorsInitialized()
3021 #if !ES_BUILD_STANDALONE
3022 Contract.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
3024 if (m_eventData == null)
3026 Contract.Assert(m_rawManifest == null);
3027 m_rawManifest = CreateManifestAndDescriptors(this.GetType(), Name, this);
3028 Contract.Assert(m_eventData != null);
3030 // TODO Enforce singleton pattern
3031 foreach (WeakReference eventSourceRef in EventListener.s_EventSources)
3033 EventSource eventSource = eventSourceRef.Target as EventSource;
3034 if (eventSource != null && eventSource.Guid == m_guid && !eventSource.IsDisposed)
3036 if (eventSource != this)
3037 throw new ArgumentException(Environment.GetResourceString("EventSource_EventSourceGuidInUse", m_guid));
3041 // Make certain all dispatchers also have their arrays initialized
3042 EventDispatcher dispatcher = m_Dispatchers;
3043 while (dispatcher != null)
3045 if (dispatcher.m_EventEnabled == null)
3046 dispatcher.m_EventEnabled = new bool[m_eventData.Length];
3047 dispatcher = dispatcher.m_Next;
3050 if (s_currentPid == 0)
3052 #if ES_BUILD_STANDALONE && !ES_BUILD_PCL
3053 // for non-BCL EventSource we must assert SecurityPermission
3054 new SecurityPermission(PermissionState.Unrestricted).Assert();
3056 s_currentPid = Win32Native.GetCurrentProcessId();
3060 // Send out the ETW manifest XML out to ETW
3061 // Today, we only send the manifest to ETW, custom listeners don't get it.
3062 [SecuritySafeCritical]
3063 private unsafe bool SendManifest(byte[] rawManifest)
3065 bool success = true;
3067 if (rawManifest == null)
3070 Contract.Assert(!SelfDescribingEvents);
3072 #if FEATURE_MANAGED_ETW
3073 fixed (byte* dataPtr = rawManifest)
3075 // we don't want the manifest to show up in the event log channels so we specify as keywords
3076 // everything but the first 8 bits (reserved for the 8 channels)
3077 var manifestDescr = new EventDescriptor(0xFFFE, 1, 0, 0, 0xFE, 0xFFFE, 0x00ffFFFFffffFFFF);
3078 ManifestEnvelope envelope = new ManifestEnvelope();
3080 envelope.Format = ManifestEnvelope.ManifestFormats.SimpleXmlFormat;
3081 envelope.MajorVersion = 1;
3082 envelope.MinorVersion = 0;
3083 envelope.Magic = 0x5B; // An unusual number that can be checked for consistency.
3084 int dataLeft = rawManifest.Length;
3085 envelope.ChunkNumber = 0;
3087 EventProvider.EventData* dataDescrs = stackalloc EventProvider.EventData[2];
3088 dataDescrs[0].Ptr = (ulong)&envelope;
3089 dataDescrs[0].Size = (uint)sizeof(ManifestEnvelope);
3090 dataDescrs[0].Reserved = 0;
3092 dataDescrs[1].Ptr = (ulong)dataPtr;
3093 dataDescrs[1].Reserved = 0;
3095 int chunkSize = ManifestEnvelope.MaxChunkSize;
3096 TRY_AGAIN_WITH_SMALLER_CHUNK_SIZE:
3097 envelope.TotalChunks = (ushort)((dataLeft + (chunkSize - 1)) / chunkSize);
3098 while (dataLeft > 0)
3100 dataDescrs[1].Size = (uint)Math.Min(dataLeft, chunkSize);
3101 if (m_provider != null)
3103 if (!m_provider.WriteEvent(ref manifestDescr, null, null, 2, (IntPtr)dataDescrs))
3105 // Turns out that if users set the BufferSize to something less than 64K then WriteEvent
3106 // can fail. If we get this failure on the first chunk try again with something smaller
3107 // The smallest BufferSize is 1K so if we get to 256 (to account for envelope overhead), we can give up making it smaller.
3108 if (EventProvider.GetLastWriteEventError() == EventProvider.WriteEventErrorCode.EventTooBig)
3110 if (envelope.ChunkNumber == 0 && chunkSize > 256)
3112 chunkSize = chunkSize / 2;
3113 goto TRY_AGAIN_WITH_SMALLER_CHUNK_SIZE;
3117 if (ThrowOnEventWriteErrors)
3118 ThrowEventSourceException();
3122 dataLeft -= chunkSize;
3123 dataDescrs[1].Ptr += (uint)chunkSize;
3124 envelope.ChunkNumber++;
3127 #endif // FEATURE_MANAGED_ETW
3132 internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType, EventManifestOptions flags = EventManifestOptions.None)
3134 return GetCustomAttributeHelper(type.GetTypeInfo(), attributeType, flags);
3138 // Helper to deal with the fact that the type we are reflecting over might be loaded in the ReflectionOnly context.
3139 // When that is the case, we have the build the custom assemblies on a member by hand.
3140 internal static Attribute GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags = EventManifestOptions.None)
3142 if (!member.Module.Assembly.ReflectionOnly() && (flags & EventManifestOptions.AllowEventSourceOverride) == 0)
3144 // Let the runtime to the work for us, since we can execute code in this context.
3145 Attribute firstAttribute = null;
3146 foreach (var attribute in member.GetCustomAttributes(attributeType, false))
3148 firstAttribute = (Attribute)attribute;
3151 return firstAttribute;
3155 // In the reflection only context, we have to do things by hand.
3156 string fullTypeNameToFind = attributeType.FullName;
3158 #if EVENT_SOURCE_LEGACY_NAMESPACE_SUPPORT
3159 fullTypeNameToFind = fullTypeNameToFind.Replace("System.Diagnostics.Eventing", "System.Diagnostics.Tracing");
3162 foreach (CustomAttributeData data in CustomAttributeData.GetCustomAttributes(member))
3164 if (AttributeTypeNamesMatch(attributeType, data.Constructor.ReflectedType))
3166 Attribute attr = null;
3168 Contract.Assert(data.ConstructorArguments.Count <= 1);
3170 if (data.ConstructorArguments.Count == 1)
3172 attr = (Attribute)Activator.CreateInstance(attributeType, new object[] { data.ConstructorArguments[0].Value });
3174 else if (data.ConstructorArguments.Count == 0)
3176 attr = (Attribute)Activator.CreateInstance(attributeType);
3181 Type t = attr.GetType();
3183 foreach (CustomAttributeNamedArgument namedArgument in data.NamedArguments)
3185 PropertyInfo p = t.GetProperty(namedArgument.MemberInfo.Name, BindingFlags.Public | BindingFlags.Instance);
3186 object value = namedArgument.TypedValue.Value;
3188 if (p.PropertyType.IsEnum)
3190 value = Enum.Parse(p.PropertyType, value.ToString());
3193 p.SetValue(attr, value, null);
3202 #else // ES_BUILD_PCL
3203 throw new ArgumentException(Environment.GetResourceString("EventSource", "EventSource_PCLPlatformNotSupportedReflection"));
3208 /// Evaluates if two related "EventSource"-domain types should be considered the same
3210 /// <param name="attributeType">The attribute type in the load context - it's associated with the running
3211 /// EventSource type. This type may be different fromt he base type of the user-defined EventSource.</param>
3212 /// <param name="reflectedAttributeType">The attribute type in the reflection context - it's associated with
3213 /// the user-defined EventSource, and is in the same assembly as the eventSourceType passed to
3215 /// <returns>True - if the types should be considered equivalent, False - otherwise</returns>
3216 private static bool AttributeTypeNamesMatch(Type attributeType, Type reflectedAttributeType)
3219 // are these the same type?
3220 attributeType == reflectedAttributeType ||
3221 // are the full typenames equal?
3222 string.Equals(attributeType.FullName, reflectedAttributeType.FullName, StringComparison.Ordinal) ||
3223 // are the typenames equal and the namespaces under "Diagnostics.Tracing" (typically
3224 // either Microsoft.Diagnostics.Tracing or System.Diagnostics.Tracing)?
3225 string.Equals(attributeType.Name, reflectedAttributeType.Name, StringComparison.Ordinal) &&
3226 attributeType.Namespace.EndsWith("Diagnostics.Tracing") &&
3227 (reflectedAttributeType.Namespace.EndsWith("Diagnostics.Tracing")
3228 #if EVENT_SOURCE_LEGACY_NAMESPACE_SUPPORT
3229 || reflectedAttributeType.Namespace.EndsWith("Diagnostics.Eventing")
3234 private static Type GetEventSourceBaseType(Type eventSourceType, bool allowEventSourceOverride, bool reflectionOnly)
3236 // return false for "object" and interfaces
3237 if (eventSourceType.BaseType() == null)
3240 // now go up the inheritance chain until hitting a concrete type ("object" at worse)
3243 eventSourceType = eventSourceType.BaseType();
3245 while (eventSourceType != null && eventSourceType.IsAbstract());
3247 if (eventSourceType != null)
3249 if (!allowEventSourceOverride)
3251 if (reflectionOnly && eventSourceType.FullName != typeof(EventSource).FullName ||
3252 !reflectionOnly && eventSourceType != typeof(EventSource))
3257 if (eventSourceType.Name != "EventSource")
3261 return eventSourceType;
3264 // Use reflection to look at the attributes of a class, and generate a manifest for it (as UTF8) and
3265 // return the UTF8 bytes. It also sets up the code:EventData structures needed to dispatch events
3266 // at run time. 'source' is the event source to place the descriptors. If it is null,
3267 // then the descriptors are not creaed, and just the manifest is generated.
3268 private static byte[] CreateManifestAndDescriptors(Type eventSourceType, string eventSourceDllName, EventSource source,
3269 EventManifestOptions flags = EventManifestOptions.None)
3271 ManifestBuilder manifest = null;
3272 bool bNeedsManifest = source != null ? !source.SelfDescribingEvents : true;
3273 Exception exception = null; // exception that might get raised during validation b/c we couldn't/didn't recover from a previous error
3276 if (eventSourceType.IsAbstract() && (flags & EventManifestOptions.Strict) == 0)
3279 #if DEBUG && ES_BUILD_STANDALONE
3280 TestSupport.TestHooks.MaybeThrow(eventSourceType,
3281 TestSupport.Category.ManifestError,
3282 "EventSource_CreateManifestAndDescriptors",
3283 new ArgumentException("EventSource_CreateManifestAndDescriptors"));
3288 MethodInfo[] methods = eventSourceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
3289 EventAttribute defaultEventAttribute;
3290 int eventId = 1; // The number given to an event that does not have a explicitly given ID.
3291 EventMetadata[] eventData = null;
3292 Dictionary<string, string> eventsByName = null;
3293 if (source != null || (flags & EventManifestOptions.Strict) != 0)
3295 eventData = new EventMetadata[methods.Length + 1];
3296 eventData[0].Name = ""; // Event 0 is the 'write messages string' event, and has an empty name.
3299 // See if we have localization information.
3300 ResourceManager resources = null;
3301 EventSourceAttribute eventSourceAttrib = (EventSourceAttribute)GetCustomAttributeHelper(eventSourceType, typeof(EventSourceAttribute), flags);
3302 if (eventSourceAttrib != null && eventSourceAttrib.LocalizationResources != null)
3303 resources = new ResourceManager(eventSourceAttrib.LocalizationResources, eventSourceType.Assembly());
3305 manifest = new ManifestBuilder(GetName(eventSourceType, flags), GetGuid(eventSourceType), eventSourceDllName,
3308 // Add an entry unconditionally for event ID 0 which will be for a string message.
3309 manifest.StartEvent("EventSourceMessage", new EventAttribute(0) { Level = EventLevel.LogAlways, Task = (EventTask)0xFFFE });
3310 manifest.AddEventParameter(typeof(string), "message");
3311 manifest.EndEvent();
3313 // eventSourceType must be sealed and must derive from this EventSource
3314 if ((flags & EventManifestOptions.Strict) != 0)
3316 bool typeMatch = GetEventSourceBaseType(eventSourceType, (flags & EventManifestOptions.AllowEventSourceOverride) != 0, eventSourceType.Assembly().ReflectionOnly()) != null;
3319 manifest.ManifestError(Environment.GetResourceString("EventSource_TypeMustDeriveFromEventSource"));
3320 if (!eventSourceType.IsAbstract() && !eventSourceType.IsSealed())
3321 manifest.ManifestError(Environment.GetResourceString("EventSource_TypeMustBeSealedOrAbstract"));
3324 // Collect task, opcode, keyword and channel information
3325 #if FEATURE_MANAGED_ETW_CHANNELS && FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
3326 foreach (var providerEnumKind in new string[] { "Keywords", "Tasks", "Opcodes", "Channels" })
3328 foreach (var providerEnumKind in new string[] { "Keywords", "Tasks", "Opcodes" })
3331 Type nestedType = eventSourceType.GetNestedType(providerEnumKind);
3332 if (nestedType != null)
3334 if (eventSourceType.IsAbstract())
3336 manifest.ManifestError(Environment.GetResourceString("EventSource_AbstractMustNotDeclareKTOC", nestedType.Name));
3340 foreach (FieldInfo staticField in nestedType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
3342 AddProviderEnumKind(manifest, staticField, providerEnumKind);
3347 // ensure we have keywords for the session-filtering reserved bits
3349 manifest.AddKeyword("Session3", (long)0x1000 << 32);
3350 manifest.AddKeyword("Session2", (long)0x2000 << 32);
3351 manifest.AddKeyword("Session1", (long)0x4000 << 32);
3352 manifest.AddKeyword("Session0", (long)0x8000 << 32);
3355 if (eventSourceType != typeof(EventSource))
3357 for (int i = 0; i < methods.Length; i++)
3359 MethodInfo method = methods[i];
3360 ParameterInfo[] args = method.GetParameters();
3362 // Get the EventDescriptor (from the Custom attributes)
3363 EventAttribute eventAttribute = (EventAttribute)GetCustomAttributeHelper(method, typeof(EventAttribute), flags);
3365 // Compat: until v4.5.1 we ignored any non-void returning methods as well as virtual methods for
3366 // the only reason of limiting the number of methods considered to be events. This broke a common
3367 // design of having event sources implement specific interfaces. To fix this in a compatible way
3368 // we will now allow both non-void returning and virtual methods to be Event methods, as long
3369 // as they are marked with the [Event] attribute
3370 if (/* method.IsVirtual || */ method.IsStatic)
3375 if (eventSourceType.IsAbstract())
3377 if (eventAttribute != null)
3378 manifest.ManifestError(Environment.GetResourceString("EventSource_AbstractMustNotDeclareEventMethods", method.Name, eventAttribute.EventId));
3381 else if (eventAttribute == null)
3383 // Methods that don't return void can't be events, if they're NOT marked with [Event].
3384 // (see Compat comment above)
3385 if (method.ReturnType != typeof(void))
3390 // Continue to ignore virtual methods if they do NOT have the [Event] attribute
3391 // (see Compat comment above)
3392 if (method.IsVirtual)
3397 // If we explicitly mark the method as not being an event, then honor that.
3398 if (GetCustomAttributeHelper(method, typeof(NonEventAttribute), flags) != null)
3401 defaultEventAttribute = new EventAttribute(eventId);
3402 eventAttribute = defaultEventAttribute;
3404 else if (eventAttribute.EventId <= 0)
3406 manifest.ManifestError(Environment.GetResourceString("EventSource_NeedPositiveId", method.Name), true);
3407 continue; // don't validate anything else for this event
3409 if (method.Name.LastIndexOf('.') >= 0)
3410 manifest.ManifestError(Environment.GetResourceString("EventSource_EventMustNotBeExplicitImplementation", method.Name, eventAttribute.EventId));
3413 string eventName = method.Name;
3415 if (eventAttribute.Opcode == EventOpcode.Info) // We are still using the default opcode.
3417 // By default pick a task ID derived from the EventID, starting with the highest task number and working back
3418 bool noTask = (eventAttribute.Task == EventTask.None);
3420 eventAttribute.Task = (EventTask)(0xFFFE - eventAttribute.EventId);
3422 // Unless we explicitly set the opcode to Info (to override the auto-generate of Start or Stop opcodes,
3423 // pick a default opcode based on the event name (either Info or start or stop if the name ends with that suffix).
3424 if (!eventAttribute.IsOpcodeSet)
3425 eventAttribute.Opcode = GetOpcodeWithDefault(EventOpcode.Info, eventName);
3427 // Make the stop opcode have the same task as the start opcode.
3430 if (eventAttribute.Opcode == EventOpcode.Start)
3432 string taskName = eventName.Substring(0, eventName.Length - s_ActivityStartSuffix.Length); // Remove the Stop suffix to get the task name
3433 if (string.Compare(eventName, 0, taskName, 0, taskName.Length) == 0 &&
3434 string.Compare(eventName, taskName.Length, s_ActivityStartSuffix, 0, Math.Max(eventName.Length - taskName.Length, s_ActivityStartSuffix.Length)) == 0)
3436 // Add a task that is just the task name for the start event. This suppress the auto-task generation
3437 // That would otherwise happen (and create 'TaskName'Start as task name rather than just 'TaskName'
3438 manifest.AddTask(taskName, (int)eventAttribute.Task);
3441 else if (eventAttribute.Opcode == EventOpcode.Stop)
3443 // Find the start associated with this stop event. We require start to be immediately before the stop
3444 int startEventId = eventAttribute.EventId - 1;
3445 if (eventData != null && startEventId < eventData.Length)
3447 Contract.Assert(0 <= startEventId); // Since we reserve id 0, we know that id-1 is <= 0
3448 EventMetadata startEventMetadata = eventData[startEventId];
3450 // If you remove the Stop and add a Start does that name match the Start Event's Name?
3451 // Ideally we would throw an error
3452 string taskName = eventName.Substring(0, eventName.Length - s_ActivityStopSuffix.Length); // Remove the Stop suffix to get the task name
3453 if (startEventMetadata.Descriptor.Opcode == (byte)EventOpcode.Start &&
3454 string.Compare(startEventMetadata.Name, 0, taskName, 0, taskName.Length) == 0 &&
3455 string.Compare(startEventMetadata.Name, taskName.Length, s_ActivityStartSuffix, 0, Math.Max(startEventMetadata.Name.Length - taskName.Length, s_ActivityStartSuffix.Length)) == 0)
3458 // Make the stop event match the start event
3459 eventAttribute.Task = (EventTask)startEventMetadata.Descriptor.Task;
3463 if (noTask && (flags & EventManifestOptions.Strict) != 0) // Throw an error if we can compatibly.
3464 throw new ArgumentException(Environment.GetResourceString("EventSource_StopsFollowStarts"));
3469 bool hasRelatedActivityID = RemoveFirstArgIfRelatedActivityId(ref args);
3470 if (!(source != null && source.SelfDescribingEvents))
3472 manifest.StartEvent(eventName, eventAttribute);
3473 for (int fieldIdx = 0; fieldIdx < args.Length; fieldIdx++)
3475 manifest.AddEventParameter(args[fieldIdx].ParameterType, args[fieldIdx].Name);
3477 manifest.EndEvent();
3480 if (source != null || (flags & EventManifestOptions.Strict) != 0)
3482 // Do checking for user errors (optional, but not a big deal so we do it).
3483 DebugCheckEvent(ref eventsByName, eventData, method, eventAttribute, manifest, flags);
3485 #if FEATURE_MANAGED_ETW_CHANNELS
3486 // add the channel keyword for Event Viewer channel based filters. This is added for creating the EventDescriptors only
3487 // and is not required for the manifest
3488 if (eventAttribute.Channel != EventChannel.None)
3492 eventAttribute.Keywords |= (EventKeywords)manifest.GetChannelKeyword(eventAttribute.Channel);
3496 string eventKey = "event_" + eventName;
3497 string msg = manifest.GetLocalizedMessage(eventKey, CultureInfo.CurrentUICulture, etwFormat: false);
3498 // overwrite inline message with the localized message
3499 if (msg != null) eventAttribute.Message = msg;
3501 AddEventDescriptor(ref eventData, eventName, eventAttribute, args, hasRelatedActivityID);
3506 // Tell the TraceLogging stuff where to start allocating its own IDs.
3507 NameInfo.ReserveEventIDsBelow(eventId);
3511 TrimEventDescriptors(ref eventData);
3512 source.m_eventData = eventData; // officially initialize it. We do this at most once (it is racy otherwise).
3513 #if FEATURE_MANAGED_ETW_CHANNELS
3514 source.m_channelData = manifest.GetChannelData();
3518 // if this is an abstract event source we've already performed all the validation we can
3519 if (!eventSourceType.IsAbstract() && (source == null || !source.SelfDescribingEvents))
3521 bNeedsManifest = (flags & EventManifestOptions.OnlyIfNeededForRegistration) == 0
3522 #if FEATURE_MANAGED_ETW_CHANNELS
3523 || manifest.GetChannelData().Length > 0
3527 // if the manifest is not needed and we're not requested to validate the event source return early
3528 if (!bNeedsManifest && (flags & EventManifestOptions.Strict) == 0)
3531 res = manifest.CreateManifest();
3536 // if this is a runtime manifest generation let the exception propagate
3537 if ((flags & EventManifestOptions.Strict) == 0)
3539 // else store it to include it in the Argument exception we raise below
3543 if ((flags & EventManifestOptions.Strict) != 0 && (manifest.Errors.Count > 0 || exception != null))
3545 string msg = String.Empty;
3546 if (manifest.Errors.Count > 0)
3548 bool firstError = true;
3549 foreach (string error in manifest.Errors)
3552 msg += Environment.NewLine;
3558 msg = "Unexpected error: " + exception.Message;
3560 throw new ArgumentException(msg, exception);
3563 return bNeedsManifest ? res : null;
3566 private static bool RemoveFirstArgIfRelatedActivityId(ref ParameterInfo[] args)
3568 // If the first parameter is (case insensitive) 'relatedActivityId' then skip it.
3569 if (args.Length > 0 && args[0].ParameterType == typeof(Guid) &&
3570 string.Compare(args[0].Name, "relatedActivityId", StringComparison.OrdinalIgnoreCase) == 0)
3572 var newargs = new ParameterInfo[args.Length - 1];
3573 Array.Copy(args, 1, newargs, 0, args.Length - 1);
3582 // adds a enumeration (keyword, opcode, task or channel) represented by 'staticField'
3584 private static void AddProviderEnumKind(ManifestBuilder manifest, FieldInfo staticField, string providerEnumKind)
3586 bool reflectionOnly = staticField.Module.Assembly.ReflectionOnly();
3587 Type staticFieldType = staticField.FieldType;
3588 if (!reflectionOnly && (staticFieldType == typeof(EventOpcode)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventOpcode)))
3590 if (providerEnumKind != "Opcodes") goto Error;
3591 int value = (int)staticField.GetRawConstantValue();
3592 manifest.AddOpcode(staticField.Name, value);
3594 else if (!reflectionOnly && (staticFieldType == typeof(EventTask)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventTask)))
3596 if (providerEnumKind != "Tasks") goto Error;
3597 int value = (int)staticField.GetRawConstantValue();
3598 manifest.AddTask(staticField.Name, value);
3600 else if (!reflectionOnly && (staticFieldType == typeof(EventKeywords)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventKeywords)))
3602 if (providerEnumKind != "Keywords") goto Error;
3603 ulong value = unchecked((ulong)(long)staticField.GetRawConstantValue());
3604 manifest.AddKeyword(staticField.Name, value);
3606 #if FEATURE_MANAGED_ETW_CHANNELS && FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
3607 else if (!reflectionOnly && (staticFieldType == typeof(EventChannel)) || AttributeTypeNamesMatch(staticFieldType, typeof(EventChannel)))
3609 if (providerEnumKind != "Channels") goto Error;
3610 var channelAttribute = (EventChannelAttribute)GetCustomAttributeHelper(staticField, typeof(EventChannelAttribute));
3611 manifest.AddChannel(staticField.Name, (byte)staticField.GetRawConstantValue(), channelAttribute);
3616 manifest.ManifestError(Environment.GetResourceString("EventSource_EnumKindMismatch", staticField.Name, staticField.FieldType.Name, providerEnumKind));
3619 // Helper used by code:CreateManifestAndDescriptors to add a code:EventData descriptor for a method
3620 // with the code:EventAttribute 'eventAttribute'. resourceManger may be null in which case we populate it
3621 // it is populated if we need to look up message resources
3622 private static void AddEventDescriptor(ref EventMetadata[] eventData, string eventName,
3623 EventAttribute eventAttribute, ParameterInfo[] eventParameters,
3624 bool hasRelatedActivityID)
3626 if (eventData == null || eventData.Length <= eventAttribute.EventId)
3628 EventMetadata[] newValues = new EventMetadata[Math.Max(eventData.Length + 16, eventAttribute.EventId + 1)];
3629 Array.Copy(eventData, newValues, eventData.Length);
3630 eventData = newValues;
3633 eventData[eventAttribute.EventId].Descriptor = new EventDescriptor(
3634 eventAttribute.EventId,
3635 eventAttribute.Version,
3636 #if FEATURE_MANAGED_ETW_CHANNELS
3637 (byte)eventAttribute.Channel,
3641 (byte)eventAttribute.Level,
3642 (byte)eventAttribute.Opcode,
3643 (int)eventAttribute.Task,
3644 unchecked((long)((ulong)eventAttribute.Keywords | SessionMask.All.ToEventKeywords())));
3646 eventData[eventAttribute.EventId].Tags = eventAttribute.Tags;
3647 eventData[eventAttribute.EventId].Name = eventName;
3648 eventData[eventAttribute.EventId].Parameters = eventParameters;
3649 eventData[eventAttribute.EventId].Message = eventAttribute.Message;
3650 eventData[eventAttribute.EventId].ActivityOptions = eventAttribute.ActivityOptions;
3651 eventData[eventAttribute.EventId].HasRelatedActivityID = hasRelatedActivityID;
3654 // Helper used by code:CreateManifestAndDescriptors that trims the m_eventData array to the correct
3655 // size after all event descriptors have been added.
3656 private static void TrimEventDescriptors(ref EventMetadata[] eventData)
3658 int idx = eventData.Length;
3662 if (eventData[idx].Descriptor.EventId != 0)
3665 if (eventData.Length - idx > 2) // allow one wasted slot.
3667 EventMetadata[] newValues = new EventMetadata[idx + 1];
3668 Array.Copy(eventData, newValues, newValues.Length);
3669 eventData = newValues;
3673 // Helper used by code:EventListener.AddEventSource and code:EventListener.EventListener
3674 // when a listener gets attached to a eventSource
3675 internal void AddListener(EventListener listener)
3677 lock (EventListener.EventListenersLock)
3679 bool[] enabledArray = null;
3680 if (m_eventData != null)
3681 enabledArray = new bool[m_eventData.Length];
3682 m_Dispatchers = new EventDispatcher(m_Dispatchers, enabledArray, listener);
3683 listener.OnEventSourceCreated(this);
3687 // Helper used by code:CreateManifestAndDescriptors to find user mistakes like reusing an event
3688 // index for two distinct events etc. Throws exceptions when it finds something wrong.
3689 private static void DebugCheckEvent(ref Dictionary<string, string> eventsByName,
3690 EventMetadata[] eventData, MethodInfo method, EventAttribute eventAttribute,
3691 ManifestBuilder manifest, EventManifestOptions options)
3693 int evtId = eventAttribute.EventId;
3694 string evtName = method.Name;
3695 int eventArg = GetHelperCallFirstArg(method);
3696 if (eventArg >= 0 && evtId != eventArg)
3698 manifest.ManifestError(Environment.GetResourceString("EventSource_MismatchIdToWriteEvent", evtName, evtId, eventArg), true);
3701 if (evtId < eventData.Length && eventData[evtId].Descriptor.EventId != 0)
3703 manifest.ManifestError(Environment.GetResourceString("EventSource_EventIdReused", evtName, evtId, eventData[evtId].Name), true);
3706 // We give a task to things if they don't have one.
3707 // TODO this is moderately expensive (N*N). We probably should not even bother....
3708 Contract.Assert(eventAttribute.Task != EventTask.None || eventAttribute.Opcode != EventOpcode.Info);
3709 for (int idx = 0; idx < eventData.Length; ++idx)
3711 // skip unused Event IDs.
3712 if (eventData[idx].Name == null)
3715 if (eventData[idx].Descriptor.Task == (int)eventAttribute.Task && eventData[idx].Descriptor.Opcode == (int)eventAttribute.Opcode)
3717 manifest.ManifestError(Environment.GetResourceString("EventSource_TaskOpcodePairReused",
3718 evtName, evtId, eventData[idx].Name, idx));
3720 // If we are not strict stop on first error. We have had problems with really large providers taking forever. because of many errors.
3721 if ((options & EventManifestOptions.Strict) == 0)
3726 // for non-default event opcodes the user must define a task!
3727 if (eventAttribute.Opcode != EventOpcode.Info)
3729 bool failure = false;
3730 if (eventAttribute.Task == EventTask.None)
3734 // If you have the auto-assigned Task, then you did not explicitly set one.
3735 // This is OK for Start events because we have special logic to assign the task to a prefix derived from the event name
3736 // But all other cases we want to catch the omission.
3737 var autoAssignedTask = (EventTask)(0xFFFE - evtId);
3738 if ((eventAttribute.Opcode != EventOpcode.Start && eventAttribute.Opcode != EventOpcode.Stop) && eventAttribute.Task == autoAssignedTask)
3742 manifest.ManifestError(Environment.GetResourceString("EventSource_EventMustHaveTaskIfNonDefaultOpcode", evtName, evtId));
3745 // If we ever want to enforce the rule: MethodName = TaskName + OpcodeName here's how:
3746 // (the reason we don't is backwards compat and the need for handling this as a non-fatal error
3747 // by eventRegister.exe)
3748 // taskName & opcodeName could be passed in by the caller which has opTab & taskTab handy
3749 // if (!(((int)eventAttribute.Opcode == 0 && evtName == taskName) || (evtName == taskName+opcodeName)))
3751 // throw new WarningException(Environment.GetResourceString("EventSource_EventNameDoesNotEqualTaskPlusOpcode"));
3754 if (eventsByName == null)
3755 eventsByName = new Dictionary<string, string>();
3757 if (eventsByName.ContainsKey(evtName))
3758 manifest.ManifestError(Environment.GetResourceString("EventSource_EventNameReused", evtName), true);
3760 eventsByName[evtName] = evtName;
3764 /// This method looks at the IL and tries to pattern match against the standard
3765 /// 'boilerplate' event body
3767 /// { if (Enabled()) WriteEvent(#, ...) }
3769 /// If the pattern matches, it returns the literal number passed as the first parameter to
3770 /// the WriteEvent. This is used to find common user errors (mismatching this
3771 /// number with the EventAttribute ID). It is only used for validation.
3773 /// <param name="method">The method to probe.</param>
3774 /// <returns>The literal value or -1 if the value could not be determined. </returns>
3775 [SecuritySafeCritical]
3776 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Switch statement is clearer than alternatives")]
3777 static private int GetHelperCallFirstArg(MethodInfo method)
3780 // Currently searches for the following pattern
3782 // ... // CAN ONLY BE THE INSTRUCTIONS BELOW
3785 // ... // CAN ONLY BE THE INSTRUCTIONS BELOW CAN'T BE A BRANCH OR A CALL
3787 // NOP // 0 or more times
3790 // If we find this pattern we return the XXX. Otherwise we return -1.
3791 (new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)).Assert();
3792 byte[] instrs = method.GetMethodBody().GetILAsByteArray();
3794 for (int idx = 0; idx < instrs.Length; )
3796 switch (instrs[idx])
3819 case 21: // LDC_I4_M1
3820 case 22: // LDC_I4_0
3821 case 23: // LDC_I4_1
3822 case 24: // LDC_I4_2
3823 case 25: // LDC_I4_3
3824 case 26: // LDC_I4_4
3825 case 27: // LDC_I4_5
3826 case 28: // LDC_I4_6
3827 case 29: // LDC_I4_7
3828 case 30: // LDC_I4_8
3829 if (idx > 0 && instrs[idx - 1] == 2) // preceeded by LDARG0
3830 retVal = instrs[idx] - 22;
3832 case 31: // LDC_I4_S
3833 if (idx > 0 && instrs[idx - 1] == 2) // preceeded by LDARG0
3834 retVal = instrs[idx + 1];
3847 // Is this call just before return?
3848 for (int search = idx + 1; search < instrs.Length; search++)
3850 if (instrs[search] == 42) // RET
3852 if (instrs[search] != 0) // NOP
3858 case 44: // BRFALSE_S
3859 case 45: // BRTRUE_S
3868 case 103: // CONV_I1
3869 case 104: // CONV_I2
3870 case 105: // CONV_I4
3871 case 106: // CONV_I8
3872 case 109: // CONV_U4
3873 case 110: // CONV_U8
3879 case 162: // STELEM_REF
3883 // Covers the CEQ instructions used in debug code for some reason.
3884 if (idx >= instrs.Length || instrs[idx] >= 6)
3888 /* Contract.Assert(false, "Warning: User validation code sub-optimial: Unsuported opcode " + instrs[idx] +
3889 " at " + idx + " in method " + method.Name); */
3898 #if false // This routine is not needed at all, it was used for unit test debugging.
3899 [Conditional("DEBUG")]
3900 private static void OutputDebugString(string msg)
3903 msg = msg.TrimEnd('\r', '\n') +
3904 string.Format(CultureInfo.InvariantCulture, ", Thrd({0})" + Environment.NewLine, Thread.CurrentThread.ManagedThreadId);
3905 System.Diagnostics.Debugger.Log(0, null, msg);
3911 /// Sends an error message to the debugger (outputDebugString), as well as the EventListeners
3912 /// It will do this even if the EventSource is not enabled.
3913 /// TODO remove flush parameter it is not used.
3915 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "This does not need to be correct when racing with other threads")]
3916 internal void ReportOutOfBandMessage(string msg, bool flush)
3921 // send message to debugger without delay
3922 System.Diagnostics.Debugger.Log(0, null, msg + "\r\n");
3925 // Send it to all listeners.
3926 if (m_outOfBandMessageCount < 16 - 1) // Note this is only if size byte
3927 m_outOfBandMessageCount++;
3930 if (m_outOfBandMessageCount == 16)
3932 m_outOfBandMessageCount = 16; // Mark that we hit the limit. Notify them that this is the case.
3933 msg = "Reached message limit. End of EventSource error messages.";
3936 WriteEventString(EventLevel.LogAlways, -1, msg);
3937 WriteStringToAllListeners("EventSourceMessage", msg);
3939 catch (Exception) { } // If we fail during last chance logging, well, we have to give up....
3942 private EventSourceSettings ValidateSettings(EventSourceSettings settings)
3944 var evtFormatMask = EventSourceSettings.EtwManifestEventFormat |
3945 EventSourceSettings.EtwSelfDescribingEventFormat;
3946 if ((settings & evtFormatMask) == evtFormatMask)
3947 throw new ArgumentException(Environment.GetResourceString("EventSource_InvalidEventFormat"), "settings");
3949 // If you did not explicitly ask for manifest, you get self-describing.
3950 if ((settings & evtFormatMask) == 0)
3951 settings |= EventSourceSettings.EtwSelfDescribingEventFormat;
3955 private bool ThrowOnEventWriteErrors
3957 get { return (m_config & EventSourceSettings.ThrowOnEventWriteErrors) != 0; }
3960 if (value) m_config |= EventSourceSettings.ThrowOnEventWriteErrors;
3961 else m_config &= ~EventSourceSettings.ThrowOnEventWriteErrors;
3965 private bool SelfDescribingEvents
3969 Contract.Assert(((m_config & EventSourceSettings.EtwManifestEventFormat) != 0) !=
3970 ((m_config & EventSourceSettings.EtwSelfDescribingEventFormat) != 0));
3971 return (m_config & EventSourceSettings.EtwSelfDescribingEventFormat) != 0;
3977 m_config |= EventSourceSettings.EtwManifestEventFormat;
3978 m_config &= ~EventSourceSettings.EtwSelfDescribingEventFormat;
3982 m_config |= EventSourceSettings.EtwSelfDescribingEventFormat;
3983 m_config &= ~EventSourceSettings.EtwManifestEventFormat;
3988 #if FEATURE_ACTIVITYSAMPLING
3989 private void ReportActivitySamplingInfo(EventListener listener, SessionMask sessions)
3991 Contract.Assert(listener == null || (uint)sessions == (uint)SessionMask.FromId(0));
3993 for (int perEventSourceSessionId = 0; perEventSourceSessionId < SessionMask.MAX; ++perEventSourceSessionId)
3995 if (!sessions[perEventSourceSessionId])
3999 if (listener == null)
4001 EtwSession etwSession = m_etwSessionIdMap[perEventSourceSessionId];
4002 Contract.Assert(etwSession != null);
4003 af = etwSession.m_activityFilter;
4007 af = listener.m_activityFilter;
4013 SessionMask m = new SessionMask();
4014 m[perEventSourceSessionId] = true;
4016 foreach (var t in af.GetFilterAsTuple(m_guid))
4018 WriteStringToListener(listener, string.Format(CultureInfo.InvariantCulture, "Session {0}: {1} = {2}", perEventSourceSessionId, t.Item1, t.Item2), m);
4021 bool participateInSampling = (listener == null) ?
4022 m_activityFilteringForETWEnabled[perEventSourceSessionId] :
4023 GetDispatcher(listener).m_activityFilteringEnabled;
4024 WriteStringToListener(listener, string.Format(CultureInfo.InvariantCulture, "Session {0}: Activity Sampling support: {1}",
4025 perEventSourceSessionId, participateInSampling ? "enabled" : "disabled"), m);
4028 #endif // FEATURE_ACTIVITYSAMPLING
4030 // private instance state
4031 private string m_name; // My friendly name (privided in ctor)
4032 internal int m_id; // A small integer that is unique to this instance.
4033 private Guid m_guid; // GUID representing the ETW eventSource to the OS.
4034 internal volatile EventMetadata[] m_eventData; // None per-event data
4035 private volatile byte[] m_rawManifest; // Bytes to send out representing the event schema
4037 private EventHandler<EventCommandEventArgs> m_eventCommandExecuted;
4039 private EventSourceSettings m_config; // configuration information
4042 private bool m_eventSourceEnabled; // am I enabled (any of my events are enabled for any dispatcher)
4043 internal EventLevel m_level; // highest level enabled by any output dispatcher
4044 internal EventKeywords m_matchAnyKeyword; // the logical OR of all levels enabled by any output dispatcher (zero is a special case) meaning 'all keywords'
4046 // Dispatching state
4047 internal volatile EventDispatcher m_Dispatchers; // Linked list of code:EventDispatchers we write the data to (we also do ETW specially)
4048 #if FEATURE_MANAGED_ETW
4049 private volatile OverideEventProvider m_provider; // This hooks up ETW commands to our 'OnEventCommand' callback
4051 private bool m_completelyInited; // The EventSource constructor has returned without exception.
4052 private Exception m_constructionException; // If there was an exception construction, this is it
4053 private byte m_outOfBandMessageCount; // The number of out of band messages sent (we throttle them
4054 private EventCommandEventArgs m_deferredCommands;// If we get commands before we are fully we store them here and run the when we are fully inited.
4056 private string[] m_traits; // Used to implement GetTraits
4058 internal static uint s_currentPid; // current process id, used in synthesizing quasi-GUIDs
4060 private static byte m_EventSourceExceptionRecurenceCount = 0; // current recursion count inside ThrowEventSourceException
4062 #if FEATURE_MANAGED_ETW_CHANNELS
4063 internal volatile ulong[] m_channelData;
4066 #if FEATURE_ACTIVITYSAMPLING
4067 private SessionMask m_curLiveSessions; // the activity-tracing aware sessions' bits
4068 private EtwSession[] m_etwSessionIdMap; // the activity-tracing aware sessions
4069 private List<EtwSession> m_legacySessions; // the legacy ETW sessions listening to this source
4070 internal long m_keywordTriggers; // a bit is set if it corresponds to a keyword that's part of an enabled triggering event
4071 internal SessionMask m_activityFilteringForETWEnabled; // does THIS EventSource have activity filtering turned on for each ETW session
4072 static internal Action<Guid> s_activityDying; // Fires when something calls SetCurrentThreadToActivity()
4073 // Also used to mark that activity tracing is on for some case
4074 #endif // FEATURE_ACTIVITYSAMPLING
4076 // We use a single instance of ActivityTracker for all EventSources instances to allow correlation between multiple event providers.
4077 // We have m_activityTracker field simply because instance field is more efficient than static field fetch.
4078 ActivityTracker m_activityTracker;
4079 internal const string s_ActivityStartSuffix = "Start";
4080 internal const string s_ActivityStopSuffix = "Stop";
4082 // used for generating GUID from eventsource name
4083 private static readonly byte[] namespaceBytes = new byte[] {
4084 0x48, 0x2C, 0x2D, 0xB2, 0xC3, 0x90, 0x47, 0xC8,
4085 0x87, 0xF8, 0x1A, 0x15, 0xBF, 0xC1, 0x30, 0xFB,
4092 /// Enables specifying event source configuration options to be used in the EventSource constructor.
4095 public enum EventSourceSettings
4098 /// This specifies none of the special configuration options should be enabled.
4102 /// Normally an EventSource NEVER throws; setting this option will tell it to throw when it encounters errors.
4104 ThrowOnEventWriteErrors = 1,
4106 /// Setting this option is a directive to the ETW listener should use manifest-based format when
4107 /// firing events. This is the default option when defining a type derived from EventSource
4108 /// (using the protected EventSource constructors).
4109 /// Only one of EtwManifestEventFormat or EtwSelfDescribingEventFormat should be specified
4111 EtwManifestEventFormat = 4,
4113 /// Setting this option is a directive to the ETW listener should use self-describing event format
4114 /// when firing events. This is the default option when creating a new instance of the EventSource
4115 /// type (using the public EventSource constructors).
4116 /// Only one of EtwManifestEventFormat or EtwSelfDescribingEventFormat should be specified
4118 EtwSelfDescribingEventFormat = 8,
4122 /// An EventListener represents a target for the events generated by EventSources (that is subclasses
4123 /// of <see cref="EventSource"/>), in the current appdomain. When a new EventListener is created
4124 /// it is logically attached to all eventSources in that appdomain. When the EventListener is Disposed, then
4125 /// it is disconnected from the event eventSources. Note that there is a internal list of STRONG references
4126 /// to EventListeners, which means that relying on the lack of references to EventListeners to clean up
4127 /// EventListeners will NOT work. You must call EventListener.Dispose explicitly when a dispatcher is no
4130 /// Once created, EventListeners can enable or disable on a per-eventSource basis using verbosity levels
4131 /// (<see cref="EventLevel"/>) and bitfields (<see cref="EventKeywords"/>) to further restrict the set of
4132 /// events to be sent to the dispatcher. The dispatcher can also send arbitrary commands to a particular
4133 /// eventSource using the 'SendCommand' method. The meaning of the commands are eventSource specific.
4135 /// The Null Guid (that is (new Guid()) has special meaning as a wildcard for 'all current eventSources in
4136 /// the appdomain'. Thus it is relatively easy to turn on all events in the appdomain if desired.
4138 /// It is possible for there to be many EventListener's defined in a single appdomain. Each dispatcher is
4139 /// logically independent of the other listeners. Thus when one dispatcher enables or disables events, it
4140 /// affects only that dispatcher (other listeners get the events they asked for). It is possible that
4141 /// commands sent with 'SendCommand' would do a semantic operation that would affect the other listeners
4142 /// (like doing a GC, or flushing data ...), but this is the exception rather than the rule.
4144 /// Thus the model is that each EventSource keeps a list of EventListeners that it is sending events
4145 /// to. Associated with each EventSource-dispatcher pair is a set of filtering criteria that determine for
4146 /// that eventSource what events that dispatcher will receive.
4148 /// Listeners receive the events on their 'OnEventWritten' method. Thus subclasses of EventListener must
4149 /// override this method to do something useful with the data.
4151 /// In addition, when new eventSources are created, the 'OnEventSourceCreate' method is called. The
4152 /// invariant associated with this callback is that every eventSource gets exactly one
4153 /// 'OnEventSourceCreate' call for ever eventSource that can potentially send it log messages. In
4154 /// particular when a EventListener is created, typically a series of OnEventSourceCreate' calls are
4155 /// made to notify the new dispatcher of all the eventSources that existed before the EventListener was
4159 public abstract class EventListener : IDisposable
4161 private event EventHandler<EventSourceCreatedEventArgs> _EventSourceCreated;
4164 /// This event is raised whenever a new eventSource is 'attached' to the dispatcher.
4165 /// This can happen for all existing EventSources when the EventListener is created
4166 /// as well as for any EventSources that come into existence after the EventListener
4167 /// has been created.
4169 /// These 'catch up' events are called during the construction of the EventListener.
4170 /// Subclasses need to be prepared for that.
4172 /// In a multi-threaded environment, it is possible that 'EventSourceEventWrittenCallback'
4173 /// events for a particular eventSource to occur BEFORE the EventSourceCreatedCallback is issued.
4175 public event EventHandler<EventSourceCreatedEventArgs> EventSourceCreated
4179 CallBackForExistingEventSources(false, value);
4181 this._EventSourceCreated = (EventHandler<EventSourceCreatedEventArgs>)Delegate.Combine(_EventSourceCreated, value);
4185 this._EventSourceCreated = (EventHandler<EventSourceCreatedEventArgs>)Delegate.Remove(_EventSourceCreated, value);
4190 /// This event is raised whenever an event has been written by a EventSource for which
4191 /// the EventListener has enabled events.
4193 public event EventHandler<EventWrittenEventArgs> EventWritten;
4196 /// Create a new EventListener in which all events start off turned off (use EnableEvents to turn
4199 protected EventListener()
4201 // This will cause the OnEventSourceCreated callback to fire.
4202 CallBackForExistingEventSources(true, (obj, args) => args.EventSource.AddListener(this) );
4206 /// Dispose should be called when the EventListener no longer desires 'OnEvent*' callbacks. Because
4207 /// there is an internal list of strong references to all EventListeners, calling 'Dispose' directly
4208 /// is the only way to actually make the listen die. Thus it is important that users of EventListener
4209 /// call Dispose when they are done with their logging.
4211 #if ES_BUILD_STANDALONE
4212 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
4214 public virtual void Dispose()
4216 lock (EventListenersLock)
4218 Contract.Assert(s_Listeners != null);
4219 if (s_Listeners != null)
4221 if (this == s_Listeners)
4223 EventListener cur = s_Listeners;
4224 s_Listeners = this.m_Next;
4225 RemoveReferencesToListenerInEventSources(cur);
4229 // Find 'this' from the s_Listeners linked list.
4230 EventListener prev = s_Listeners;
4233 EventListener cur = prev.m_Next;
4238 // Found our Listener, remove references to to it in the eventSources
4239 prev.m_Next = cur.m_Next; // Remove entry.
4240 RemoveReferencesToListenerInEventSources(cur);
4250 // We don't expose a Dispose(bool), because the contract is that you don't have any non-syncronous
4251 // 'cleanup' associated with this object
4254 /// Enable all events from the eventSource identified by 'eventSource' to the current
4255 /// dispatcher that have a verbosity level of 'level' or lower.
4257 /// This call can have the effect of REDUCING the number of events sent to the
4258 /// dispatcher if 'level' indicates a less verbose level than was previously enabled.
4260 /// This call never has an effect on other EventListeners.
4263 public void EnableEvents(EventSource eventSource, EventLevel level)
4265 EnableEvents(eventSource, level, EventKeywords.None);
4268 /// Enable all events from the eventSource identified by 'eventSource' to the current
4269 /// dispatcher that have a verbosity level of 'level' or lower and have a event keyword
4270 /// matching any of the bits in 'matchAnyKeyword'.
4272 /// This call can have the effect of REDUCING the number of events sent to the
4273 /// dispatcher if 'level' indicates a less verbose level than was previously enabled or
4274 /// if 'matchAnyKeyword' has fewer keywords set than where previously set.
4276 /// This call never has an effect on other EventListeners.
4278 public void EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword)
4280 EnableEvents(eventSource, level, matchAnyKeyword, null);
4283 /// Enable all events from the eventSource identified by 'eventSource' to the current
4284 /// dispatcher that have a verbosity level of 'level' or lower and have a event keyword
4285 /// matching any of the bits in 'matchAnyKeyword' as well as any (eventSource specific)
4286 /// effect passing additional 'key-value' arguments 'arguments' might have.
4288 /// This call can have the effect of REDUCING the number of events sent to the
4289 /// dispatcher if 'level' indicates a less verbose level than was previously enabled or
4290 /// if 'matchAnyKeyword' has fewer keywords set than where previously set.
4292 /// This call never has an effect on other EventListeners.
4294 public void EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword, IDictionary<string, string> arguments)
4296 if (eventSource == null)
4298 throw new ArgumentNullException("eventSource");
4300 Contract.EndContractBlock();
4302 eventSource.SendCommand(this, 0, 0, EventCommand.Update, true, level, matchAnyKeyword, arguments);
4305 /// Disables all events coming from eventSource identified by 'eventSource'.
4307 /// This call never has an effect on other EventListeners.
4309 public void DisableEvents(EventSource eventSource)
4311 if (eventSource == null)
4313 throw new ArgumentNullException("eventSource");
4315 Contract.EndContractBlock();
4317 eventSource.SendCommand(this, 0, 0, EventCommand.Update, false, EventLevel.LogAlways, EventKeywords.None, null);
4321 /// EventSourceIndex is small non-negative integer (suitable for indexing in an array)
4322 /// identifying EventSource. It is unique per-appdomain. Some EventListeners might find
4323 /// it useful to store additional information about each eventSource connected to it,
4324 /// and EventSourceIndex allows this extra information to be efficiently stored in a
4325 /// (growable) array (eg List(T)).
4327 public static int EventSourceIndex(EventSource eventSource) { return eventSource.m_id; }
4330 /// This method is called whenever a new eventSource is 'attached' to the dispatcher.
4331 /// This can happen for all existing EventSources when the EventListener is created
4332 /// as well as for any EventSources that come into existence after the EventListener
4333 /// has been created.
4335 /// These 'catch up' events are called during the construction of the EventListener.
4336 /// Subclasses need to be prepared for that.
4338 /// In a multi-threaded environment, it is possible that 'OnEventWritten' callbacks
4339 /// for a particular eventSource to occur BEFORE the OnEventSourceCreated is issued.
4341 /// <param name="eventSource"></param>
4342 internal protected virtual void OnEventSourceCreated(EventSource eventSource)
4344 EventHandler<EventSourceCreatedEventArgs> callBack = this._EventSourceCreated;
4345 if(callBack != null)
4347 EventSourceCreatedEventArgs args = new EventSourceCreatedEventArgs();
4348 args.EventSource = eventSource;
4349 callBack(this, args);
4354 /// This method is called whenever an event has been written by a EventSource for which
4355 /// the EventListener has enabled events.
4357 /// <param name="eventData"></param>
4358 internal protected virtual void OnEventWritten(EventWrittenEventArgs eventData)
4360 EventHandler<EventWrittenEventArgs> callBack = this.EventWritten;
4361 if (callBack != null)
4363 callBack(this, eventData);
4370 /// This routine adds newEventSource to the global list of eventSources, it also assigns the
4371 /// ID to the eventSource (which is simply the ordinal in the global list).
4373 /// EventSources currently do not pro-actively remove themselves from this list. Instead
4374 /// when eventSources's are GCed, the weak handle in this list naturally gets nulled, and
4375 /// we will reuse the slot. Today this list never shrinks (but we do reuse entries
4376 /// that are in the list). This seems OK since the expectation is that EventSources
4377 /// tend to live for the lifetime of the appdomain anyway (they tend to be used in
4378 /// global variables).
4380 /// <param name="newEventSource"></param>
4381 internal static void AddEventSource(EventSource newEventSource)
4383 lock (EventListenersLock)
4385 if (s_EventSources == null)
4386 s_EventSources = new List<WeakReference>(2);
4388 if (!s_EventSourceShutdownRegistered)
4390 s_EventSourceShutdownRegistered = true;
4391 #if !ES_BUILD_PCL && !FEATURE_CORECLR
4392 AppDomain.CurrentDomain.ProcessExit += DisposeOnShutdown;
4393 AppDomain.CurrentDomain.DomainUnload += DisposeOnShutdown;
4398 // Periodically search the list for existing entries to reuse, this avoids
4399 // unbounded memory use if we keep recycling eventSources (an unlikely thing).
4401 if (s_EventSources.Count % 64 == 63) // on every block of 64, fill up the block before continuing
4403 int i = s_EventSources.Count; // Work from the top down.
4407 WeakReference weakRef = s_EventSources[i];
4408 if (!weakRef.IsAlive)
4411 weakRef.Target = newEventSource;
4418 newIndex = s_EventSources.Count;
4419 s_EventSources.Add(new WeakReference(newEventSource));
4421 newEventSource.m_id = newIndex;
4423 // Add every existing dispatcher to the new EventSource
4424 for (EventListener listener = s_Listeners; listener != null; listener = listener.m_Next)
4425 newEventSource.AddListener(listener);
4431 // Whenver we have async callbacks from native code, there is an ugly issue where
4432 // during .NET shutdown native code could be calling the callback, but the CLR
4433 // has already prohibited callbacks to managed code in the appdomain, causing the CLR
4434 // to throw a COMPLUS_BOOT_EXCEPTION. The guideline we give is that you must unregister
4435 // such callbacks on process shutdown or appdomain so that unmanaged code will never
4436 // do this. This is what this callback is for.
4437 // See bug 724140 for more
4438 private static void DisposeOnShutdown(object sender, EventArgs e)
4440 lock(EventListenersLock)
4442 foreach (var esRef in s_EventSources)
4444 EventSource es = esRef.Target as EventSource;
4452 /// Helper used in code:Dispose that removes any references to 'listenerToRemove' in any of the
4453 /// eventSources in the appdomain.
4455 /// The EventListenersLock must be held before calling this routine.
4457 private static void RemoveReferencesToListenerInEventSources(EventListener listenerToRemove)
4459 // Foreach existing EventSource in the appdomain
4460 foreach (WeakReference eventSourceRef in s_EventSources)
4462 EventSource eventSource = eventSourceRef.Target as EventSource;
4463 if (eventSource != null)
4465 // Is the first output dispatcher the dispatcher we are removing?
4466 if (eventSource.m_Dispatchers.m_Listener == listenerToRemove)
4467 eventSource.m_Dispatchers = eventSource.m_Dispatchers.m_Next;
4470 // Remove 'listenerToRemove' from the eventSource.m_Dispatchers linked list.
4471 EventDispatcher prev = eventSource.m_Dispatchers;
4474 EventDispatcher cur = prev.m_Next;
4477 Contract.Assert(false, "EventSource did not have a registered EventListener!");
4480 if (cur.m_Listener == listenerToRemove)
4482 prev.m_Next = cur.m_Next; // Remove entry.
4493 /// Checks internal consistency of EventSources/Listeners.
4495 [Conditional("DEBUG")]
4496 internal static void Validate()
4498 lock (EventListenersLock)
4500 // Get all listeners
4501 Dictionary<EventListener, bool> allListeners = new Dictionary<EventListener, bool>();
4502 EventListener cur = s_Listeners;
4505 allListeners.Add(cur, true);
4509 // For all eventSources
4511 foreach (WeakReference eventSourceRef in s_EventSources)
4514 EventSource eventSource = eventSourceRef.Target as EventSource;
4515 if (eventSource == null)
4517 Contract.Assert(eventSource.m_id == id, "Unexpected event source ID.");
4519 // None listeners on eventSources exist in the dispatcher list.
4520 EventDispatcher dispatcher = eventSource.m_Dispatchers;
4521 while (dispatcher != null)
4523 Contract.Assert(allListeners.ContainsKey(dispatcher.m_Listener), "EventSource has a listener not on the global list.");
4524 dispatcher = dispatcher.m_Next;
4527 // Every dispatcher is on Dispatcher List of every eventSource.
4528 foreach (EventListener listener in allListeners.Keys)
4530 dispatcher = eventSource.m_Dispatchers;
4533 Contract.Assert(dispatcher != null, "Listener is not on all eventSources.");
4534 if (dispatcher.m_Listener == listener)
4536 dispatcher = dispatcher.m_Next;
4544 /// Gets a global lock that is intended to protect the code:s_Listeners linked list and the
4545 /// code:s_EventSources WeakReference list. (We happen to use the s_EventSources list as
4546 /// the lock object)
4548 internal static object EventListenersLock
4552 if (s_EventSources == null)
4553 Interlocked.CompareExchange(ref s_EventSources, new List<WeakReference>(2), null);
4554 return s_EventSources;
4558 private void CallBackForExistingEventSources(bool addToListenersList, EventHandler<EventSourceCreatedEventArgs> callback)
4560 lock (EventListenersLock)
4562 // Disallow creating EventListener reentrancy.
4563 if (s_CreatingListener)
4564 throw new InvalidOperationException(Environment.GetResourceString("EventSource_ListenerCreatedInsideCallback"));
4568 s_CreatingListener = true;
4570 if (addToListenersList)
4572 // Add to list of listeners in the system, do this BEFORE firing the 'OnEventSourceCreated' so that
4573 // Those added sources see this listener.
4574 this.m_Next = s_Listeners;
4578 // Find all existing eventSources call OnEventSourceCreated to 'catchup'
4579 // Note that we DO have reentrancy here because 'AddListener' calls out to user code (via OnEventSourceCreated callback)
4580 // We tolerate this by iterating over a copy of the list here. New event sources will take care of adding listeners themselves
4581 // EventSources are not guaranteed to be added at the end of the s_EventSource list -- We re-use slots when a new source
4583 WeakReference[] eventSourcesSnapshot = s_EventSources.ToArray();
4585 for (int i = 0; i < eventSourcesSnapshot.Length; i++)
4587 WeakReference eventSourceRef = eventSourcesSnapshot[i];
4588 EventSource eventSource = eventSourceRef.Target as EventSource;
4589 if (eventSource != null)
4591 EventSourceCreatedEventArgs args = new EventSourceCreatedEventArgs();
4592 args.EventSource = eventSource;
4593 callback(this, args);
4601 s_CreatingListener = false;
4608 internal volatile EventListener m_Next; // These form a linked list in s_Listeners
4609 #if FEATURE_ACTIVITYSAMPLING
4610 internal ActivityFilter m_activityFilter; // If we are filtering by activity on this Listener, this keeps track of it.
4611 #endif // FEATURE_ACTIVITYSAMPLING
4616 /// The list of all listeners in the appdomain. Listeners must be explicitly disposed to remove themselves
4617 /// from this list. Note that EventSources point to their listener but NOT the reverse.
4619 internal static EventListener s_Listeners;
4621 /// The list of all active eventSources in the appdomain. Note that eventSources do NOT
4622 /// remove themselves from this list this is a weak list and the GC that removes them may
4623 /// not have happened yet. Thus it can contain event sources that are dead (thus you have
4624 /// to filter those out.
4626 internal static List<WeakReference> s_EventSources;
4629 /// Used to disallow reentrancy.
4631 private static bool s_CreatingListener = false;
4634 /// Used to register AD/Process shutdown callbacks.
4636 private static bool s_EventSourceShutdownRegistered = false;
4641 /// Passed to the code:EventSource.OnEventCommand callback
4643 public class EventCommandEventArgs : EventArgs
4646 /// Gets the command for the callback.
4648 public EventCommand Command { get; internal set; }
4651 /// Gets the arguments for the callback.
4653 public IDictionary<String, String> Arguments { get; internal set; }
4656 /// Enables the event that has the specified identifier.
4658 /// <param name="eventId">Event ID of event to be enabled</param>
4659 /// <returns>true if eventId is in range</returns>
4660 public bool EnableEvent(int eventId)
4662 if (Command != EventCommand.Enable && Command != EventCommand.Disable)
4663 throw new InvalidOperationException();
4664 return eventSource.EnableEventForDispatcher(dispatcher, eventId, true);
4668 /// Disables the event that have the specified identifier.
4670 /// <param name="eventId">Event ID of event to be disabled</param>
4671 /// <returns>true if eventId is in range</returns>
4672 public bool DisableEvent(int eventId)
4674 if (Command != EventCommand.Enable && Command != EventCommand.Disable)
4675 throw new InvalidOperationException();
4676 return eventSource.EnableEventForDispatcher(dispatcher, eventId, false);
4681 internal EventCommandEventArgs(EventCommand command, IDictionary<string, string> arguments, EventSource eventSource,
4682 EventListener listener, int perEventSourceSessionId, int etwSessionId, bool enable, EventLevel level, EventKeywords matchAnyKeyword)
4684 this.Command = command;
4685 this.Arguments = arguments;
4686 this.eventSource = eventSource;
4687 this.listener = listener;
4688 this.perEventSourceSessionId = perEventSourceSessionId;
4689 this.etwSessionId = etwSessionId;
4690 this.enable = enable;
4692 this.matchAnyKeyword = matchAnyKeyword;
4695 internal EventSource eventSource;
4696 internal EventDispatcher dispatcher;
4698 // These are the arguments of sendCommand and are only used for deferring commands until after we are fully initialized.
4699 internal EventListener listener;
4700 internal int perEventSourceSessionId;
4701 internal int etwSessionId;
4702 internal bool enable;
4703 internal EventLevel level;
4704 internal EventKeywords matchAnyKeyword;
4705 internal EventCommandEventArgs nextCommand; // We form a linked list of these deferred commands.
4711 /// EventSourceCreatedEventArgs is passed to <see cref="EventListener.EventSourceCreated"/>
4713 public class EventSourceCreatedEventArgs : EventArgs
4716 /// The EventSource that is attaching to the listener.
4718 public EventSource EventSource
4726 /// EventWrittenEventArgs is passed to the user-provided override for
4727 /// <see cref="EventListener.OnEventWritten"/> when an event is fired.
4729 public class EventWrittenEventArgs : EventArgs
4732 /// The name of the event.
4734 public string EventName
4738 if (m_eventName != null || EventId < 0) // TraceLogging convention EventID == -1
4743 return m_eventSource.m_eventData[EventId].Name;
4747 m_eventName = value;
4752 /// Gets the event ID for the event that was written.
4754 public int EventId { get; internal set; }
4757 /// Gets the activity ID for the thread on which the event was written.
4759 public Guid ActivityId
4761 [System.Security.SecurityCritical]
4762 get { return EventSource.CurrentThreadActivityId; }
4766 /// Gets the related activity ID if one was specified when the event was written.
4768 public Guid RelatedActivityId
4770 [System.Security.SecurityCritical]
4776 /// Gets the payload for the event.
4778 public ReadOnlyCollection<Object> Payload { get; internal set; }
4781 /// Gets the payload argument names.
4783 public ReadOnlyCollection<string> PayloadNames
4787 // For contract based events we create the list lazily.
4788 if (m_payloadNames == null)
4790 // Self described events are identified by id -1.
4791 Contract.Assert(EventId != -1);
4793 var names = new List<string>();
4794 foreach (var parameter in m_eventSource.m_eventData[EventId].Parameters)
4796 names.Add(parameter.Name);
4798 m_payloadNames = new ReadOnlyCollection<string>(names);
4801 return m_payloadNames;
4806 m_payloadNames = value;
4811 /// Gets the event source object.
4813 public EventSource EventSource { get { return m_eventSource; } }
4816 /// Gets the keywords for the event.
4818 public EventKeywords Keywords
4822 if (EventId < 0) // TraceLogging convention EventID == -1
4825 return (EventKeywords)m_eventSource.m_eventData[EventId].Descriptor.Keywords;
4830 /// Gets the operation code for the event.
4832 public EventOpcode Opcode
4836 if (EventId < 0) // TraceLogging convention EventID == -1
4838 return (EventOpcode)m_eventSource.m_eventData[EventId].Descriptor.Opcode;
4843 /// Gets the task for the event.
4845 public EventTask Task
4849 if (EventId < 0) // TraceLogging convention EventID == -1
4850 return EventTask.None;
4852 return (EventTask)m_eventSource.m_eventData[EventId].Descriptor.Task;
4857 /// Any provider/user defined options associated with the event.
4859 public EventTags Tags
4863 if (EventId < 0) // TraceLogging convention EventID == -1
4865 return m_eventSource.m_eventData[EventId].Tags;
4870 /// Gets the message for the event.
4872 public string Message
4876 if (EventId < 0) // TraceLogging convention EventID == -1
4879 return m_eventSource.m_eventData[EventId].Message;
4888 #if FEATURE_MANAGED_ETW_CHANNELS
4890 /// Gets the channel for the event.
4892 public EventChannel Channel
4896 if (EventId < 0) // TraceLogging convention EventID == -1
4897 return EventChannel.None;
4898 return (EventChannel)m_eventSource.m_eventData[EventId].Descriptor.Channel;
4904 /// Gets the version of the event.
4910 if (EventId < 0) // TraceLogging convention EventID == -1
4912 return m_eventSource.m_eventData[EventId].Descriptor.Version;
4917 /// Gets the level for the event.
4919 public EventLevel Level
4923 if (EventId < 0) // TraceLogging convention EventID == -1
4925 return (EventLevel)m_eventSource.m_eventData[EventId].Descriptor.Level;
4930 internal EventWrittenEventArgs(EventSource eventSource)
4932 m_eventSource = eventSource;
4934 private string m_message;
4935 private string m_eventName;
4936 private EventSource m_eventSource;
4937 private ReadOnlyCollection<string> m_payloadNames;
4938 internal EventTags m_tags;
4939 internal EventOpcode m_opcode;
4940 internal EventLevel m_level;
4941 internal EventKeywords m_keywords;
4946 /// Allows customizing defaults and specifying localization support for the event source class to which it is applied.
4948 [AttributeUsage(AttributeTargets.Class)]
4949 public sealed class EventSourceAttribute : Attribute
4952 /// Overrides the ETW name of the event source (which defaults to the class name)
4954 public string Name { get; set; }
4957 /// Overrides the default (calculated) Guid of an EventSource type. Explicitly defining a GUID is discouraged,
4958 /// except when upgrading existing ETW providers to using event sources.
4960 public string Guid { get; set; }
4964 /// EventSources support localization of events. The names used for events, opcodes, tasks, keywords and maps
4965 /// can be localized to several languages if desired. This works by creating a ResX style string table
4966 /// (by simply adding a 'Resource File' to your project). This resource file is given a name e.g.
4967 /// 'DefaultNameSpace.ResourceFileName' which can be passed to the ResourceManager constructor to read the
4968 /// resources. This name is the value of the LocalizationResources property.
4970 /// If LocalizationResources property is non-null, then EventSource will look up the localized strings for events by
4971 /// using the following resource naming scheme
4973 /// <para>* event_EVENTNAME</para>
4974 /// <para>* task_TASKNAME</para>
4975 /// <para>* keyword_KEYWORDNAME</para>
4976 /// <para>* map_MAPNAME</para>
4978 /// where the capitalized name is the name of the event, task, keyword, or map value that should be localized.
4979 /// Note that the localized string for an event corresponds to the Message string, and can have {0} values
4980 /// which represent the payload values.
4983 public string LocalizationResources { get; set; }
4987 /// Any instance methods in a class that subclasses <see cref="EventSource"/> and that return void are
4988 /// assumed by default to be methods that generate an ETW event. Enough information can be deduced from the
4989 /// name of the method and its signature to generate basic schema information for the event. The
4990 /// <see cref="EventAttribute"/> class allows you to specify additional event schema information for an event if
4993 [AttributeUsage(AttributeTargets.Method)]
4994 public sealed class EventAttribute : Attribute
4996 /// <summary>Construct an EventAttribute with specified eventId</summary>
4997 /// <param name="eventId">ID of the ETW event (an integer between 1 and 65535)</param>
4998 public EventAttribute(int eventId) { this.EventId = eventId; Level = EventLevel.Informational; this.m_opcodeSet = false; }
4999 /// <summary>Event's ID</summary>
5000 public int EventId { get; private set; }
5001 /// <summary>Event's severity level: indicates the severity or verbosity of the event</summary>
5002 public EventLevel Level { get; set; }
5003 /// <summary>Event's keywords: allows classification of events by "categories"</summary>
5004 public EventKeywords Keywords { get; set; }
5005 /// <summary>Event's operation code: allows defining operations, generally used with Tasks</summary>
5006 public EventOpcode Opcode
5014 this.m_opcode = value;
5015 this.m_opcodeSet = true;
5019 internal bool IsOpcodeSet
5027 /// <summary>Event's task: allows logical grouping of events</summary>
5028 public EventTask Task { get; set; }
5029 #if FEATURE_MANAGED_ETW_CHANNELS
5030 /// <summary>Event's channel: defines an event log as an additional destination for the event</summary>
5031 public EventChannel Channel { get; set; }
5033 /// <summary>Event's version</summary>
5034 public byte Version { get; set; }
5037 /// This can be specified to enable formatting and localization of the event's payload. You can
5038 /// use standard .NET substitution operators (eg {1}) in the string and they will be replaced
5039 /// with the 'ToString()' of the corresponding part of the event payload.
5041 public string Message { get; set; }
5044 /// User defined options associated with the event. These do not have meaning to the EventSource but
5045 /// are passed through to listeners which given them semantics.
5047 public EventTags Tags { get; set; }
5050 /// Allows fine control over the Activity IDs generated by start and stop events
5052 public EventActivityOptions ActivityOptions { get; set; }
5055 EventOpcode m_opcode;
5056 private bool m_opcodeSet;
5061 /// By default all instance methods in a class that subclasses code:EventSource that and return
5062 /// void are assumed to be methods that generate an event. This default can be overridden by specifying
5063 /// the code:NonEventAttribute
5065 [AttributeUsage(AttributeTargets.Method)]
5066 public sealed class NonEventAttribute : Attribute
5069 /// Constructs a default NonEventAttribute
5071 public NonEventAttribute() { }
5074 // FUTURE we may want to expose this at some point once we have a partner that can help us validate the design.
5075 #if FEATURE_MANAGED_ETW_CHANNELS
5077 /// EventChannelAttribute allows customizing channels supported by an EventSource. This attribute must be
5078 /// applied to an member of type EventChannel defined in a Channels class nested in the EventSource class:
5080 /// public static class Channels
5082 /// [Channel(Enabled = true, EventChannelType = EventChannelType.Admin)]
5083 /// public const EventChannel Admin = (EventChannel)16;
5085 /// [Channel(Enabled = false, EventChannelType = EventChannelType.Operational)]
5086 /// public const EventChannel Operational = (EventChannel)17;
5090 [AttributeUsage(AttributeTargets.Field)]
5091 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
5094 class EventChannelAttribute : Attribute
5097 /// Specified whether the channel is enabled by default
5099 public bool Enabled { get; set; }
5102 /// Legal values are in EventChannelType
5104 public EventChannelType EventChannelType { get; set; }
5106 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
5108 /// Specifies the isolation for the channel
5110 public EventChannelIsolation Isolation { get; set; }
5113 /// Specifies an SDDL access descriptor that controls access to the log file that backs the channel.
5114 /// See MSDN ((http://msdn.microsoft.com/en-us/library/windows/desktop/aa382741.aspx) for details.
5116 public string Access { get; set; }
5119 /// Allows importing channels defined in external manifests
5121 public string ImportChannel { get; set; }
5124 // TODO: there is a convention that the name is the Provider/Type Should we provide an override?
5125 // public string Name { get; set; }
5129 /// Allowed channel types
5131 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
5134 enum EventChannelType
5136 /// <summary>The admin channel</summary>
5138 /// <summary>The operational channel</summary>
5140 /// <summary>The Analytic channel</summary>
5142 /// <summary>The debug channel</summary>
5146 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
5148 /// Allowed isolation levels. See MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/aa382741.aspx)
5149 /// for the default permissions associated with each level. EventChannelIsolation and Access allows control over the
5150 /// access permissions for the channel and backing file.
5153 enum EventChannelIsolation
5156 /// This is the default isolation level. All channels that specify Application isolation use the same ETW session
5160 /// All channels that specify System isolation use the same ETW session
5164 /// Use sparingly! When specifying Custom isolation, a separate ETW session is created for the channel.
5165 /// Using Custom isolation lets you control the access permissions for the channel and backing file.
5166 /// Because there are only 64 ETW sessions available, you should limit your use of Custom isolation.
5174 /// Describes the pre-defined command (EventCommandEventArgs.Command property) that is passed to the OnEventCommand callback.
5176 public enum EventCommand
5179 /// Update EventSource state
5183 /// Request EventSource to generate and send its manifest
5197 #region private classes
5199 #if FEATURE_ACTIVITYSAMPLING
5202 /// ActivityFilter is a helper structure that is used to keep track of run-time state
5203 /// associated with activity filtering. It is 1-1 with EventListeners (logically
5204 /// every listener has one of these, however we actually allocate them lazily), as well
5205 /// as 1-to-1 with tracing-aware EtwSessions.
5207 /// This structure also keeps track of the sampling counts associated with 'trigger'
5208 /// events. Because these trigger events are rare, and you typically only have one of
5209 /// them, we store them here as a linked list.
5211 internal sealed class ActivityFilter : IDisposable
5214 /// Disable all activity filtering for the listener associated with 'filterList',
5215 /// (in the session associated with it) that is triggered by any event in 'source'.
5217 public static void DisableFilter(ref ActivityFilter filterList, EventSource source)
5219 #if !ES_BUILD_STANDALONE
5220 Contract.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
5223 if (filterList == null)
5227 // Remove it from anywhere in the list (except the first element, which has to
5228 // be treated specially)
5229 ActivityFilter prev = filterList;
5233 if (cur.m_providerGuid == source.Guid)
5235 // update TriggersActivityTracking bit
5236 if (cur.m_eventId >= 0 && cur.m_eventId < source.m_eventData.Length)
5237 --source.m_eventData[cur.m_eventId].TriggersActivityTracking;
5239 // Remove it from the linked list.
5240 prev.m_next = cur.m_next;
5241 // dispose of the removed node
5254 // Sadly we have to treat the first element specially in linked list removal in C#
5255 if (filterList.m_providerGuid == source.Guid)
5257 // update TriggersActivityTracking bit
5258 if (filterList.m_eventId >= 0 && filterList.m_eventId < source.m_eventData.Length)
5259 --source.m_eventData[filterList.m_eventId].TriggersActivityTracking;
5261 // We are the first element in the list.
5262 var first = filterList;
5263 filterList = first.m_next;
5264 // dispose of the removed node
5267 // the above might have removed the one ActivityFilter in the session that contains the
5268 // cleanup delegate; re-create the delegate if needed
5269 if (filterList != null)
5271 EnsureActivityCleanupDelegate(filterList);
5276 /// Currently this has "override" semantics. We first disable all filters
5277 /// associated with 'source', and next we add new filters for each entry in the
5278 /// string 'startEvents'. participateInSampling specifies whether non-startEvents
5279 /// always trigger or only trigger when current activity is 'active'.
5281 public static void UpdateFilter(
5282 ref ActivityFilter filterList,
5284 int perEventSourceSessionId,
5287 #if !ES_BUILD_STANDALONE
5288 Contract.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
5291 // first remove all filters associated with 'source'
5292 DisableFilter(ref filterList, source);
5294 if (!string.IsNullOrEmpty(startEvents))
5296 // ActivitySamplingStartEvents is a space-separated list of Event:Frequency pairs.
5297 // The Event may be specified by name or by ID. Errors in parsing such a pair
5298 // result in the error being reported to the listeners, and the pair being ignored.
5299 // E.g. "CustomActivityStart:1000 12:10" specifies that for event CustomActivityStart
5300 // we should initiate activity tracing once every 1000 events, *and* for event ID 12
5301 // we should initiate activity tracing once every 10 events.
5302 string[] activityFilterStrings = startEvents.Split(' ');
5304 for (int i = 0; i < activityFilterStrings.Length; ++i)
5306 string activityFilterString = activityFilterStrings[i];
5309 int colonIdx = activityFilterString.IndexOf(':');
5312 source.ReportOutOfBandMessage("ERROR: Invalid ActivitySamplingStartEvent specification: " +
5313 activityFilterString, false);
5314 // ignore failure...
5317 string sFreq = activityFilterString.Substring(colonIdx + 1);
5318 if (!int.TryParse(sFreq, out sampleFreq))
5320 source.ReportOutOfBandMessage("ERROR: Invalid sampling frequency specification: " + sFreq, false);
5323 activityFilterString = activityFilterString.Substring(0, colonIdx);
5324 if (!int.TryParse(activityFilterString, out eventId))
5328 // see if it's an event name
5329 for (int j = 0; j < source.m_eventData.Length; j++)
5331 EventSource.EventMetadata[] ed = source.m_eventData;
5332 if (ed[j].Name != null && ed[j].Name.Length == activityFilterString.Length &&
5333 string.Compare(ed[j].Name, activityFilterString, StringComparison.OrdinalIgnoreCase) == 0)
5335 eventId = ed[j].Descriptor.EventId;
5340 if (eventId < 0 || eventId >= source.m_eventData.Length)
5342 source.ReportOutOfBandMessage("ERROR: Invalid eventId specification: " + activityFilterString, false);
5345 EnableFilter(ref filterList, source, perEventSourceSessionId, eventId, sampleFreq);
5351 /// Returns the first ActivityFilter from 'filterList' corresponding to 'source'.
5353 public static ActivityFilter GetFilter(ActivityFilter filterList, EventSource source)
5355 for (var af = filterList; af != null; af = af.m_next)
5357 if (af.m_providerGuid == source.Guid && af.m_samplingFreq != -1)
5364 /// Returns a session mask representing all sessions in which the activity
5365 /// associated with the current thread is allowed through the activity filter.
5366 /// If 'triggeringEvent' is true the event MAY be a triggering event. Ideally
5367 /// most of the time this is false as you can guarentee this event is NOT a
5368 /// triggering event. If 'triggeringEvent' is true, then it checks the
5369 /// 'EventSource' and 'eventID' of the event being logged to see if it is actually
5370 /// a trigger. If so it activates the current activity.
5372 /// If 'childActivityID' is present, it will be added to the active set if the
5373 /// current activity is active.
5376 unsafe public static bool PassesActivityFilter(
5377 ActivityFilter filterList,
5378 Guid* childActivityID,
5379 bool triggeringEvent,
5383 Contract.Assert(filterList != null && filterList.m_activeActivities != null);
5384 bool shouldBeLogged = false;
5385 if (triggeringEvent)
5387 for (ActivityFilter af = filterList; af != null; af = af.m_next)
5389 if (eventId == af.m_eventId && source.Guid == af.m_providerGuid)
5391 // Update the sampling count with wrap-around
5392 int curSampleCount, newSampleCount;
5395 curSampleCount = af.m_curSampleCount;
5396 if (curSampleCount <= 1)
5397 newSampleCount = af.m_samplingFreq; // Wrap around, counting down to 1
5399 newSampleCount = curSampleCount - 1;
5401 while (Interlocked.CompareExchange(ref af.m_curSampleCount, newSampleCount, curSampleCount) != curSampleCount);
5402 // If we hit zero, then start tracking the activity.
5403 if (curSampleCount <= 1)
5405 Guid currentActivityId = EventSource.InternalCurrentThreadActivityId;
5406 Tuple<Guid, int> startId;
5407 // only add current activity if it's not already a root activity
5408 if (!af.m_rootActiveActivities.TryGetValue(currentActivityId, out startId))
5410 // EventSource.OutputDebugString(string.Format(" PassesAF - Triggering(session {0}, evt {1})", af.m_perEventSourceSessionId, eventId));
5411 shouldBeLogged = true;
5412 af.m_activeActivities[currentActivityId] = Environment.TickCount;
5413 af.m_rootActiveActivities[currentActivityId] = Tuple.Create(source.Guid, eventId);
5418 // a start event following a triggering start event
5419 Guid currentActivityId = EventSource.InternalCurrentThreadActivityId;
5420 Tuple<Guid, int> startId;
5421 // only remove current activity if we added it
5422 if (af.m_rootActiveActivities.TryGetValue(currentActivityId, out startId) &&
5423 startId.Item1 == source.Guid && startId.Item2 == eventId)
5425 // EventSource.OutputDebugString(string.Format("Activity dying: {0} -> StartEvent({1})", currentActivityId, eventId));
5426 // remove activity only from current logging scope (af)
5428 af.m_activeActivities.TryRemove(currentActivityId, out dummy);
5436 var activeActivities = GetActiveActivities(filterList);
5437 if (activeActivities != null)
5439 // if we hadn't already determined this should be logged, test further
5440 if (!shouldBeLogged)
5442 shouldBeLogged = !activeActivities.IsEmpty &&
5443 activeActivities.ContainsKey(EventSource.InternalCurrentThreadActivityId);
5445 if (shouldBeLogged && childActivityID != null &&
5446 ((EventOpcode)source.m_eventData[eventId].Descriptor.Opcode == EventOpcode.Send))
5448 FlowActivityIfNeeded(filterList, null, childActivityID);
5449 // EventSource.OutputDebugString(string.Format(" PassesAF - activity {0}", *childActivityID));
5452 // EventSource.OutputDebugString(string.Format(" PassesAF - shouldBeLogged(evt {0}) = {1:x}", eventId, shouldBeLogged));
5453 return shouldBeLogged;
5456 [System.Security.SecuritySafeCritical]
5457 public static bool IsCurrentActivityActive(ActivityFilter filterList)
5459 var activeActivities = GetActiveActivities(filterList);
5460 if (activeActivities != null &&
5461 activeActivities.ContainsKey(EventSource.InternalCurrentThreadActivityId))
5468 /// For the EventListener/EtwSession associated with 'filterList', add 'childActivityid'
5469 /// to list of active activities IF 'currentActivityId' is also active. Passing in a null
5470 /// value for 'currentActivityid' is an indication tha caller has already verified
5471 /// that the current activity is active.
5474 unsafe public static void FlowActivityIfNeeded(ActivityFilter filterList, Guid* currentActivityId, Guid* childActivityID)
5476 Contract.Assert(childActivityID != null);
5478 var activeActivities = GetActiveActivities(filterList);
5479 Contract.Assert(activeActivities != null);
5481 // take currentActivityId == null to mean we *know* the current activity is "active"
5482 if (currentActivityId != null && !activeActivities.ContainsKey(*currentActivityId))
5485 if (activeActivities.Count > MaxActivityTrackCount)
5487 TrimActiveActivityStore(activeActivities);
5488 // make sure current activity is still in the set:
5489 activeActivities[EventSource.InternalCurrentThreadActivityId] = Environment.TickCount;
5491 // add child activity to list of actives
5492 activeActivities[*childActivityID] = Environment.TickCount;
5498 public static void UpdateKwdTriggers(ActivityFilter activityFilter, Guid sourceGuid, EventSource source, EventKeywords sessKeywords)
5500 for (var af = activityFilter; af != null; af = af.m_next)
5502 if ((sourceGuid == af.m_providerGuid) &&
5503 (source.m_eventData[af.m_eventId].TriggersActivityTracking > 0 ||
5504 ((EventOpcode)source.m_eventData[af.m_eventId].Descriptor.Opcode == EventOpcode.Send)))
5506 // we could be more precise here, if we tracked 'anykeywords' per session
5509 source.m_keywordTriggers |= (source.m_eventData[af.m_eventId].Descriptor.Keywords & (long)sessKeywords);
5516 /// For the EventSource specified by 'sourceGuid' and the EventListener/EtwSession
5517 /// associated with 'this' ActivityFilter list, return configured sequence of
5518 /// [eventId, sampleFreq] pairs that defines the sampling policy.
5520 public IEnumerable<Tuple<int, int>> GetFilterAsTuple(Guid sourceGuid)
5522 for (ActivityFilter af = this; af != null; af = af.m_next)
5524 if (af.m_providerGuid == sourceGuid)
5525 yield return Tuple.Create(af.m_eventId, af.m_samplingFreq);
5530 /// The cleanup being performed consists of removing the m_myActivityDelegate from
5531 /// the static s_activityDying, therefore allowing the ActivityFilter to be reclaimed.
5533 public void Dispose()
5535 #if !ES_BUILD_STANDALONE
5536 Contract.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
5538 // m_myActivityDelegate is still alive (held by the static EventSource.s_activityDying).
5539 // Therefore we are ok to take a dependency on m_myActivityDelegate being valid even
5540 // during the finalization of the ActivityFilter
5541 if (m_myActivityDelegate != null)
5543 EventSource.s_activityDying = (Action<Guid>)Delegate.Remove(EventSource.s_activityDying, m_myActivityDelegate);
5544 m_myActivityDelegate = null;
5551 /// Creates a new ActivityFilter that is triggered by 'eventId' from 'source' ever
5552 /// 'samplingFreq' times the event fires. You can have several of these forming a
5555 private ActivityFilter(EventSource source, int perEventSourceSessionId, int eventId, int samplingFreq, ActivityFilter existingFilter = null)
5557 m_providerGuid = source.Guid;
5558 m_perEventSourceSessionId = perEventSourceSessionId;
5559 m_eventId = eventId;
5560 m_samplingFreq = samplingFreq;
5561 m_next = existingFilter;
5563 Contract.Assert(existingFilter == null ||
5564 (existingFilter.m_activeActivities == null) == (existingFilter.m_rootActiveActivities == null));
5566 // if this is the first filter we add for this session, we need to create a new
5567 // table of activities. m_activeActivities is common across EventSources in the same
5569 ConcurrentDictionary<Guid, int> activeActivities = null;
5570 if (existingFilter == null ||
5571 (activeActivities = GetActiveActivities(existingFilter)) == null)
5573 m_activeActivities = new ConcurrentDictionary<Guid, int>();
5574 m_rootActiveActivities = new ConcurrentDictionary<Guid, Tuple<Guid, int>>();
5576 // Add a delegate to the 'SetCurrentThreadToActivity callback so that I remove 'dead' activities
5577 m_myActivityDelegate = GetActivityDyingDelegate(this);
5578 EventSource.s_activityDying = (Action<Guid>)Delegate.Combine(EventSource.s_activityDying, m_myActivityDelegate);
5582 m_activeActivities = activeActivities;
5583 m_rootActiveActivities = existingFilter.m_rootActiveActivities;
5589 /// Ensure there's at least one ActivityFilter in the 'filterList' that contains an
5590 /// activity-removing delegate for the listener/session associated with 'filterList'.
5592 private static void EnsureActivityCleanupDelegate(ActivityFilter filterList)
5594 if (filterList == null)
5597 for (ActivityFilter af = filterList; af != null; af = af.m_next)
5599 if (af.m_myActivityDelegate != null)
5603 // we didn't find a delegate
5604 filterList.m_myActivityDelegate = GetActivityDyingDelegate(filterList);
5605 EventSource.s_activityDying = (Action<Guid>)Delegate.Combine(EventSource.s_activityDying, filterList.m_myActivityDelegate);
5609 /// Builds the delegate to be called when an activity is dying. This is responsible
5610 /// for performing whatever cleanup is needed for the ActivityFilter list passed in.
5611 /// This gets "added" to EventSource.s_activityDying and ends up being called from
5612 /// EventSource.SetCurrentThreadActivityId and ActivityFilter.PassesActivityFilter.
5614 /// <returns>The delegate to be called when an activity is dying</returns>
5615 private static Action<Guid> GetActivityDyingDelegate(ActivityFilter filterList)
5617 return (Guid oldActivity) =>
5620 filterList.m_activeActivities.TryRemove(oldActivity, out dummy);
5621 Tuple<Guid, int> dummyTuple;
5622 filterList.m_rootActiveActivities.TryRemove(oldActivity, out dummyTuple);
5627 /// Enables activity filtering for the listener associated with 'filterList', triggering on
5628 /// the event 'eventID' from 'source' with a sampling frequency of 'samplingFreq'
5630 /// if 'eventID' is out of range (e.g. negative), it means we are not triggering (but we are
5631 /// activitySampling if something else triggered).
5633 /// <returns>true if activity sampling is enabled the samplingFreq is non-zero </returns>
5634 private static bool EnableFilter(ref ActivityFilter filterList, EventSource source, int perEventSourceSessionId, int eventId, int samplingFreq)
5636 #if !ES_BUILD_STANDALONE
5637 Contract.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
5639 Contract.Assert(samplingFreq > 0);
5640 Contract.Assert(eventId >= 0);
5642 filterList = new ActivityFilter(source, perEventSourceSessionId, eventId, samplingFreq, filterList);
5644 // Mark the 'quick Check' that indicates this is a trigger event.
5645 // If eventId is out of range then this mark is not done which has the effect of ignoring
5647 if (0 <= eventId && eventId < source.m_eventData.Length)
5648 ++source.m_eventData[eventId].TriggersActivityTracking;
5654 /// Normally this code never runs, it is here just to prevent run-away resource usage.
5656 private static void TrimActiveActivityStore(ConcurrentDictionary<Guid, int> activities)
5658 if (activities.Count > MaxActivityTrackCount)
5660 // Remove half of the oldest activity ids.
5661 var keyValues = activities.ToArray();
5662 var tickNow = Environment.TickCount;
5664 // Sort by age, taking into account wrap-around. As long as x and y are within
5665 // 23 days of now then (0x7FFFFFFF & (tickNow - x.Value)) is the delta (even if
5666 // TickCount wraps). I then sort by DESCENDING age. (that is oldest value first)
5667 Array.Sort(keyValues, (x, y) => (0x7FFFFFFF & (tickNow - y.Value)) - (0x7FFFFFFF & (tickNow - x.Value)));
5668 for (int i = 0; i < keyValues.Length / 2; i++)
5671 activities.TryRemove(keyValues[i].Key, out dummy);
5676 private static ConcurrentDictionary<Guid, int> GetActiveActivities(
5677 ActivityFilter filterList)
5679 for (ActivityFilter af = filterList; af != null; af = af.m_next)
5681 if (af.m_activeActivities != null)
5682 return af.m_activeActivities;
5687 // m_activeActivities always points to the sample dictionary for EVERY ActivityFilter
5688 // in the m_next list. The 'int' value in the m_activities set is a timestamp
5689 // (Environment.TickCount) of when the entry was put in the system and is used to
5690 // remove 'old' entries that if the set gets too big.
5691 ConcurrentDictionary<Guid, int> m_activeActivities;
5693 // m_rootActiveActivities holds the "root" active activities, i.e. the activities
5694 // that were marked as active because a Start event fired on them. We need to keep
5695 // track of these to enable sampling in the scenario of an app's main thread that
5696 // never explicitly sets distinct activity IDs as it executes. To handle these
5697 // situations we manufacture a Guid from the thread's ID, and:
5698 // (a) we consider the firing of a start event when the sampling counter reaches
5699 // zero to mark the beginning of an interesting activity, and
5700 // (b) we consider the very next firing of the same start event to mark the
5701 // ending of that activity.
5702 // We use a ConcurrentDictionary to avoid taking explicit locks.
5703 // The key (a guid) represents the activity ID of the root active activity
5704 // The value is made up of the Guid of the event provider and the eventId of
5706 ConcurrentDictionary<Guid, Tuple<Guid, int>> m_rootActiveActivities;
5707 Guid m_providerGuid; // We use the GUID rather than object identity because we don't want to keep the eventSource alive
5708 int m_eventId; // triggering event
5709 int m_samplingFreq; // Counter reset to this when it hits 0
5710 int m_curSampleCount; // We count down to 0 and then activate the activity.
5711 int m_perEventSourceSessionId; // session ID bit for ETW, 0 for EventListeners
5713 const int MaxActivityTrackCount = 100000; // maximum number of tracked activities
5715 ActivityFilter m_next; // We create a linked list of these
5716 Action<Guid> m_myActivityDelegate;
5722 /// An EtwSession instance represents an activity-tracing-aware ETW session. Since these
5723 /// are limited to 8 concurrent sessions per machine (currently) we're going to store
5724 /// the active ones in a singly linked list.
5726 internal class EtwSession
5728 public static EtwSession GetEtwSession(int etwSessionId, bool bCreateIfNeeded = false)
5730 if (etwSessionId < 0)
5733 EtwSession etwSession;
5734 foreach (var wrEtwSession in s_etwSessions)
5736 #if ES_BUILD_STANDALONE
5737 if ((etwSession = (EtwSession) wrEtwSession.Target) != null && etwSession.m_etwSessionId == etwSessionId)
5740 if (wrEtwSession.TryGetTarget(out etwSession) && etwSession.m_etwSessionId == etwSessionId)
5745 if (!bCreateIfNeeded)
5748 #if ES_BUILD_STANDALONE
5749 if (s_etwSessions == null)
5750 s_etwSessions = new List<WeakReference>();
5752 etwSession = new EtwSession(etwSessionId);
5753 s_etwSessions.Add(new WeakReference(etwSession));
5755 if (s_etwSessions == null)
5756 s_etwSessions = new List<WeakReference<EtwSession>>();
5758 etwSession = new EtwSession(etwSessionId);
5759 s_etwSessions.Add(new WeakReference<EtwSession>(etwSession));
5762 if (s_etwSessions.Count > s_thrSessionCount)
5769 public static void RemoveEtwSession(EtwSession etwSession)
5771 Contract.Assert(etwSession != null);
5772 if (s_etwSessions == null || etwSession == null)
5775 s_etwSessions.RemoveAll((wrEtwSession) =>
5778 #if ES_BUILD_STANDALONE
5779 return (session = (EtwSession) wrEtwSession.Target) != null &&
5780 (session.m_etwSessionId == etwSession.m_etwSessionId);
5782 return wrEtwSession.TryGetTarget(out session) &&
5783 (session.m_etwSessionId == etwSession.m_etwSessionId);
5787 if (s_etwSessions.Count > s_thrSessionCount)
5791 private static void TrimGlobalList()
5793 if (s_etwSessions == null)
5796 s_etwSessions.RemoveAll((wrEtwSession) =>
5798 #if ES_BUILD_STANDALONE
5799 return wrEtwSession.Target == null;
5802 return !wrEtwSession.TryGetTarget(out session);
5807 private EtwSession(int etwSessionId)
5809 m_etwSessionId = etwSessionId;
5812 public readonly int m_etwSessionId; // ETW session ID (as retrieved by EventProvider)
5813 public ActivityFilter m_activityFilter; // all filters enabled for this session
5815 #if ES_BUILD_STANDALONE
5816 private static List<WeakReference> s_etwSessions = new List<WeakReference>();
5818 private static List<WeakReference<EtwSession>> s_etwSessions = new List<WeakReference<EtwSession>>();
5820 private const int s_thrSessionCount = 16;
5823 #endif // FEATURE_ACTIVITYSAMPLING
5825 // holds a bitfield representing a session mask
5827 /// A SessionMask represents a set of (at most MAX) sessions as a bit mask. The perEventSourceSessionId
5828 /// is the index in the SessionMask of the bit that will be set. These can translate to
5829 /// EventSource's reserved keywords bits using the provided ToEventKeywords() and
5830 /// FromEventKeywords() methods.
5832 internal struct SessionMask
5834 public SessionMask(SessionMask m)
5835 { m_mask = m.m_mask; }
5837 public SessionMask(uint mask = 0)
5838 { m_mask = mask & MASK; }
5840 public bool IsEqualOrSupersetOf(SessionMask m)
5842 return (this.m_mask | m.m_mask) == this.m_mask;
5845 public static SessionMask All
5847 get { return new SessionMask(MASK); }
5850 public static SessionMask FromId(int perEventSourceSessionId)
5852 Contract.Assert(perEventSourceSessionId < MAX);
5853 return new SessionMask((uint)1 << perEventSourceSessionId);
5856 public ulong ToEventKeywords()
5858 return (ulong)m_mask << SHIFT_SESSION_TO_KEYWORD;
5861 public static SessionMask FromEventKeywords(ulong m)
5863 return new SessionMask((uint)(m >> SHIFT_SESSION_TO_KEYWORD));
5866 public bool this[int perEventSourceSessionId]
5870 Contract.Assert(perEventSourceSessionId < MAX);
5871 return (m_mask & (1 << perEventSourceSessionId)) != 0;
5875 Contract.Assert(perEventSourceSessionId < MAX);
5876 if (value) m_mask |= ((uint)1 << perEventSourceSessionId);
5877 else m_mask &= ~((uint)1 << perEventSourceSessionId);
5881 public static SessionMask operator |(SessionMask m1, SessionMask m2)
5883 return new SessionMask(m1.m_mask | m2.m_mask);
5886 public static SessionMask operator &(SessionMask m1, SessionMask m2)
5888 return new SessionMask(m1.m_mask & m2.m_mask);
5891 public static SessionMask operator ^(SessionMask m1, SessionMask m2)
5893 return new SessionMask(m1.m_mask ^ m2.m_mask);
5896 public static SessionMask operator ~(SessionMask m)
5898 return new SessionMask(MASK & ~(m.m_mask));
5901 public static explicit operator ulong(SessionMask m)
5902 { return m.m_mask; }
5904 public static explicit operator uint(SessionMask m)
5905 { return m.m_mask; }
5907 private uint m_mask;
5909 internal const int SHIFT_SESSION_TO_KEYWORD = 44; // bits 44-47 inclusive are reserved
5910 internal const uint MASK = 0x0fU; // the mask of 4 reserved bits
5911 internal const uint MAX = 4; // maximum number of simultaneous ETW sessions supported
5915 /// code:EventDispatchers are a simple 'helper' structure that holds the filtering state
5916 /// (m_EventEnabled) for a particular EventSource X EventListener tuple
5918 /// Thus a single EventListener may have many EventDispatchers (one for every EventSource
5919 /// that that EventListener has activate) and a Single EventSource may also have many
5920 /// event Dispatchers (one for every EventListener that has activated it).
5922 /// Logically a particular EventDispatcher belongs to exactly one EventSource and exactly
5923 /// one EventListener (alhtough EventDispatcher does not 'remember' the EventSource it is
5924 /// associated with.
5926 internal class EventDispatcher
5928 internal EventDispatcher(EventDispatcher next, bool[] eventEnabled, EventListener listener)
5931 m_EventEnabled = eventEnabled;
5932 m_Listener = listener;
5936 readonly internal EventListener m_Listener; // The dispatcher this entry is for
5937 internal bool[] m_EventEnabled; // For every event in a the eventSource, is it enabled?
5938 #if FEATURE_ACTIVITYSAMPLING
5939 internal bool m_activityFilteringEnabled; // does THIS EventSource have activity filtering turned on for this listener?
5940 #endif // FEATURE_ACTIVITYSAMPLING
5942 // Only guarenteed to exist after a InsureInit()
5943 internal EventDispatcher m_Next; // These form a linked list in code:EventSource.m_Dispatchers
5944 // Of all listeners for that eventSource.
5948 /// Flags that can be used with EventSource.GenerateManifest to control how the ETW manifest for the EventSource is
5952 public enum EventManifestOptions
5955 /// Only the resources associated with current UI culture are included in the manifest
5959 /// Throw exceptions for any inconsistency encountered
5963 /// Generate a "resources" node under "localization" for every satellite assembly provided
5967 /// Generate the manifest only if the event source needs to be registered on the machine,
5968 /// otherwise return null (but still perform validation if Strict is specified)
5970 OnlyIfNeededForRegistration = 0x4,
5972 /// When generating the manifest do *not* enforce the rule that the current EventSource class
5973 /// must be the base class for the user-defined type passed in. This allows validation of .net
5974 /// event sources using the new validation code
5976 AllowEventSourceOverride = 0x8,
5980 /// ManifestBuilder is designed to isolate the details of the message of the event from the
5981 /// rest of EventSource. This one happens to create XML.
5983 internal class ManifestBuilder
5986 /// Build a manifest for 'providerName' with the given GUID, which will be packaged into 'dllName'.
5987 /// 'resources, is a resource manager. If specified all messages are localized using that manager.
5989 public ManifestBuilder(string providerName, Guid providerGuid, string dllName, ResourceManager resources,
5990 EventManifestOptions flags)
5992 #if FEATURE_MANAGED_ETW_CHANNELS
5993 this.providerName = providerName;
5997 this.resources = resources;
5998 sb = new StringBuilder();
5999 events = new StringBuilder();
6000 templates = new StringBuilder();
6001 opcodeTab = new Dictionary<int, string>();
6002 stringTab = new Dictionary<string, string>();
6003 errors = new List<string>();
6004 perEventByteArrayArgIndices = new Dictionary<string, List<int>>();
6006 sb.AppendLine("<instrumentationManifest xmlns=\"http://schemas.microsoft.com/win/2004/08/events\">");
6007 sb.AppendLine(" <instrumentation xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:win=\"http://manifests.microsoft.com/win/2004/08/windows/events\">");
6008 sb.AppendLine(" <events xmlns=\"http://schemas.microsoft.com/win/2004/08/events\">");
6009 sb.Append("<provider name=\"").Append(providerName).
6010 Append("\" guid=\"{").Append(providerGuid.ToString()).Append("}");
6011 if (dllName != null)
6012 sb.Append("\" resourceFileName=\"").Append(dllName).Append("\" messageFileName=\"").Append(dllName);
6014 var symbolsName = providerName.Replace("-", "").Replace(".", "_"); // Period and - are illegal replace them.
6015 sb.Append("\" symbol=\"").Append(symbolsName);
6016 sb.Append("\">").AppendLine();
6019 public void AddOpcode(string name, int value)
6021 if ((flags & EventManifestOptions.Strict) != 0)
6023 if (value <= 10 || value >= 239)
6024 ManifestError(Environment.GetResourceString("EventSource_IllegalOpcodeValue", name, value));
6026 if (opcodeTab.TryGetValue(value, out prevName) && !name.Equals(prevName, StringComparison.Ordinal))
6027 ManifestError(Environment.GetResourceString("EventSource_OpcodeCollision", name, prevName, value));
6029 opcodeTab[value] = name;
6031 public void AddTask(string name, int value)
6033 if ((flags & EventManifestOptions.Strict) != 0)
6035 if (value <= 0 || value >= 65535)
6036 ManifestError(Environment.GetResourceString("EventSource_IllegalTaskValue", name, value));
6038 if (taskTab != null && taskTab.TryGetValue(value, out prevName) && !name.Equals(prevName, StringComparison.Ordinal))
6039 ManifestError(Environment.GetResourceString("EventSource_TaskCollision", name, prevName, value));
6041 if (taskTab == null)
6042 taskTab = new Dictionary<int, string>();
6043 taskTab[value] = name;
6045 public void AddKeyword(string name, ulong value)
6047 if ((value & (value - 1)) != 0) // Is it a power of 2?
6048 ManifestError(Environment.GetResourceString("EventSource_KeywordNeedPowerOfTwo", "0x" + value.ToString("x", CultureInfo.CurrentCulture), name), true);
6049 if ((flags & EventManifestOptions.Strict) != 0)
6051 if (value >= 0x0000100000000000UL && !name.StartsWith("Session", StringComparison.Ordinal))
6052 ManifestError(Environment.GetResourceString("EventSource_IllegalKeywordsValue", name, "0x" + value.ToString("x", CultureInfo.CurrentCulture)));
6054 if (keywordTab != null && keywordTab.TryGetValue(value, out prevName) && !name.Equals(prevName, StringComparison.Ordinal))
6055 ManifestError(Environment.GetResourceString("EventSource_KeywordCollision", name, prevName, "0x" + value.ToString("x", CultureInfo.CurrentCulture)));
6057 if (keywordTab == null)
6058 keywordTab = new Dictionary<ulong, string>();
6059 keywordTab[value] = name;
6062 #if FEATURE_MANAGED_ETW_CHANNELS
6064 /// Add a channel. channelAttribute can be null
6066 public void AddChannel(string name, int value, EventChannelAttribute channelAttribute)
6068 EventChannel chValue = (EventChannel)value;
6069 if (value < (int)EventChannel.Admin || value > 255)
6070 ManifestError(Environment.GetResourceString("EventSource_EventChannelOutOfRange", name, value));
6071 else if (chValue >= EventChannel.Admin && chValue <= EventChannel.Debug &&
6072 channelAttribute != null && EventChannelToChannelType(chValue) != channelAttribute.EventChannelType)
6074 // we want to ensure developers do not define EventChannels that conflict with the builtin ones,
6075 // but we want to allow them to override the default ones...
6076 ManifestError(Environment.GetResourceString("EventSource_ChannelTypeDoesNotMatchEventChannelValue",
6077 name, ((EventChannel)value).ToString()));
6080 // TODO: validate there are no conflicting manifest exposed names (generally following the format "provider/type")
6082 ulong kwd = GetChannelKeyword(chValue);
6084 if (channelTab == null)
6085 channelTab = new Dictionary<int, ChannelInfo>(4);
6086 channelTab[value] = new ChannelInfo { Name = name, Keywords = kwd, Attribs = channelAttribute };
6089 private EventChannelType EventChannelToChannelType(EventChannel channel)
6091 #if !ES_BUILD_STANDALONE
6092 Contract.Assert(channel >= EventChannel.Admin && channel <= EventChannel.Debug);
6094 return (EventChannelType)((int)channel - (int)EventChannel.Admin + (int)EventChannelType.Admin);
6096 private EventChannelAttribute GetDefaultChannelAttribute(EventChannel channel)
6098 EventChannelAttribute attrib = new EventChannelAttribute();
6099 attrib.EventChannelType = EventChannelToChannelType(channel);
6100 if (attrib.EventChannelType <= EventChannelType.Operational)
6101 attrib.Enabled = true;
6105 public ulong[] GetChannelData()
6107 if (this.channelTab == null)
6109 return new ulong[0];
6112 // We create an array indexed by the channel id for fast look up.
6113 // E.g. channelMask[Admin] will give you the bit mask for Admin channel.
6115 foreach (var item in this.channelTab.Keys)
6123 ulong[] channelMask = new ulong[maxkey + 1];
6124 foreach (var item in this.channelTab)
6126 channelMask[item.Key] = item.Value.Keywords;
6133 public void StartEvent(string eventName, EventAttribute eventAttribute)
6135 Contract.Assert(numParams == 0);
6136 Contract.Assert(this.eventName == null);
6137 this.eventName = eventName;
6139 byteArrArgIndices = null;
6141 events.Append(" <event").
6142 Append(" value=\"").Append(eventAttribute.EventId).Append("\"").
6143 Append(" version=\"").Append(eventAttribute.Version).Append("\"").
6144 Append(" level=\"").Append(GetLevelName(eventAttribute.Level)).Append("\"").
6145 Append(" symbol=\"").Append(eventName).Append("\"");
6147 // at this point we add to the manifest's stringTab a message that is as-of-yet
6148 // "untranslated to manifest convention", b/c we don't have the number or position
6149 // of any byte[] args (which require string format index updates)
6150 WriteMessageAttrib(events, "event", eventName, eventAttribute.Message);
6152 if (eventAttribute.Keywords != 0)
6153 events.Append(" keywords=\"").Append(GetKeywords((ulong)eventAttribute.Keywords, eventName)).Append("\"");
6154 if (eventAttribute.Opcode != 0)
6155 events.Append(" opcode=\"").Append(GetOpcodeName(eventAttribute.Opcode, eventName)).Append("\"");
6156 if (eventAttribute.Task != 0)
6157 events.Append(" task=\"").Append(GetTaskName(eventAttribute.Task, eventName)).Append("\"");
6158 #if FEATURE_MANAGED_ETW_CHANNELS
6159 if (eventAttribute.Channel != 0)
6161 events.Append(" channel=\"").Append(GetChannelName(eventAttribute.Channel, eventName, eventAttribute.Message)).Append("\"");
6166 public void AddEventParameter(Type type, string name)
6169 templates.Append(" <template tid=\"").Append(eventName).Append("Args\">").AppendLine();
6170 if (type == typeof(byte[]))
6172 // mark this index as "extraneous" (it has no parallel in the managed signature)
6173 // we use these values in TranslateToManifestConvention()
6174 if (byteArrArgIndices == null)
6175 byteArrArgIndices = new List<int>(4);
6176 byteArrArgIndices.Add(numParams);
6178 // add an extra field to the template representing the length of the binary blob
6180 templates.Append(" <data name=\"").Append(name).Append("Size\" inType=\"win:UInt32\"/>").AppendLine();
6183 templates.Append(" <data name=\"").Append(name).Append("\" inType=\"").Append(GetTypeName(type)).Append("\"");
6184 // TODO: for 'byte*' types it assumes the user provided length is named using the same naming convention
6185 // as for 'byte[]' args (blob_arg_name + "Size")
6186 if ((type.IsArray || type.IsPointer) && type.GetElementType() == typeof(byte))
6188 // add "length" attribute to the "blob" field in the template (referencing the field added above)
6189 templates.Append(" length=\"").Append(name).Append("Size\"");
6191 // ETW does not support 64-bit value maps, so we don't specify these as ETW maps
6192 if (type.IsEnum() && Enum.GetUnderlyingType(type) != typeof(UInt64) && Enum.GetUnderlyingType(type) != typeof(Int64))
6194 templates.Append(" map=\"").Append(type.Name).Append("\"");
6195 if (mapsTab == null)
6196 mapsTab = new Dictionary<string, Type>();
6197 if (!mapsTab.ContainsKey(type.Name))
6198 mapsTab.Add(type.Name, type); // Remember that we need to dump the type enumeration
6201 templates.Append("/>").AppendLine();
6203 public void EndEvent()
6207 templates.Append(" </template>").AppendLine();
6208 events.Append(" template=\"").Append(eventName).Append("Args\"");
6210 events.Append("/>").AppendLine();
6212 if (byteArrArgIndices != null)
6213 perEventByteArrayArgIndices[eventName] = byteArrArgIndices;
6215 // at this point we have all the information we need to translate the C# Message
6216 // to the manifest string we'll put in the stringTab
6218 if (stringTab.TryGetValue("event_" + eventName, out msg))
6220 msg = TranslateToManifestConvention(msg, eventName);
6221 stringTab["event_" + eventName] = msg;
6226 byteArrArgIndices = null;
6229 #if FEATURE_MANAGED_ETW_CHANNELS
6230 // Channel keywords are generated one per channel to allow channel based filtering in event viewer. These keywords are autogenerated
6231 // by mc.exe for compiling a manifest and are based on the order of the channels (fields) in the Channels inner class (when advanced
6232 // channel support is enabled), or based on the order the predefined channels appear in the EventAttribute properties (for simple
6233 // support). The manifest generated *MUST* have the channels specified in the same order (that's how our computed keywords are mapped
6234 // to channels by the OS infrastructure).
6235 public ulong GetChannelKeyword(EventChannel channel)
6237 if (channelTab == null)
6239 channelTab = new Dictionary<int, ChannelInfo>(4);
6242 if (channelTab.Count == MaxCountChannels)
6243 ManifestError(Environment.GetResourceString("EventSource_MaxChannelExceeded"));
6245 ulong channelKeyword;
6247 if (!channelTab.TryGetValue((int)channel, out info))
6249 channelKeyword = nextChannelKeywordBit;
6250 nextChannelKeywordBit >>= 1;
6254 channelKeyword = info.Keywords;
6257 return channelKeyword;
6261 public byte[] CreateManifest()
6263 string str = CreateManifestString();
6264 return Encoding.UTF8.GetBytes(str);
6267 public IList<string> Errors { get { return errors; } }
6270 /// When validating an event source it adds the error to the error collection.
6271 /// When not validating it throws an exception if runtimeCritical is "true".
6272 /// Otherwise the error is ignored.
6274 /// <param name="msg"></param>
6275 /// <param name="runtimeCritical"></param>
6276 public void ManifestError(string msg, bool runtimeCritical = false)
6278 if ((flags & EventManifestOptions.Strict) != 0)
6280 else if (runtimeCritical)
6281 throw new ArgumentException(msg);
6284 private string CreateManifestString()
6287 #if FEATURE_MANAGED_ETW_CHANNELS
6288 // Write out the channels
6289 if (channelTab != null)
6291 sb.Append(" <channels>").AppendLine();
6292 var sortedChannels = new List<KeyValuePair<int, ChannelInfo>>();
6293 foreach (KeyValuePair<int, ChannelInfo> p in channelTab) { sortedChannels.Add(p); }
6294 sortedChannels.Sort((p1, p2) => -Comparer<ulong>.Default.Compare(p1.Value.Keywords, p2.Value.Keywords));
6295 foreach (var kvpair in sortedChannels)
6297 int channel = kvpair.Key;
6298 ChannelInfo channelInfo = kvpair.Value;
6300 string channelType = null;
6301 string elementName = "channel";
6302 bool enabled = false;
6303 string fullName = null;
6304 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
6305 string isolation = null;
6306 string access = null;
6308 if (channelInfo.Attribs != null)
6310 var attribs = channelInfo.Attribs;
6311 if (Enum.IsDefined(typeof(EventChannelType), attribs.EventChannelType))
6312 channelType = attribs.EventChannelType.ToString();
6313 enabled = attribs.Enabled;
6314 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
6315 if (attribs.ImportChannel != null)
6317 fullName = attribs.ImportChannel;
6318 elementName = "importChannel";
6320 if (Enum.IsDefined(typeof(EventChannelIsolation), attribs.Isolation))
6321 isolation = attribs.Isolation.ToString();
6322 access = attribs.Access;
6325 if (fullName == null)
6326 fullName = providerName + "/" + channelInfo.Name;
6328 sb.Append(" <").Append(elementName);
6329 sb.Append(" chid=\"").Append(channelInfo.Name).Append("\"");
6330 sb.Append(" name=\"").Append(fullName).Append("\"");
6331 if (elementName == "channel") // not applicable to importChannels.
6333 WriteMessageAttrib(sb, "channel", channelInfo.Name, null);
6334 sb.Append(" value=\"").Append(channel).Append("\"");
6335 if (channelType != null)
6336 sb.Append(" type=\"").Append(channelType).Append("\"");
6337 sb.Append(" enabled=\"").Append(enabled.ToString().ToLower()).Append("\"");
6338 #if FEATURE_ADVANCED_MANAGED_ETW_CHANNELS
6340 sb.Append(" access=\"").Append(access).Append("\"");
6341 if (isolation != null)
6342 sb.Append(" isolation=\"").Append(isolation).Append("\"");
6345 sb.Append("/>").AppendLine();
6347 sb.Append(" </channels>").AppendLine();
6351 // Write out the tasks
6352 if (taskTab != null)
6355 sb.Append(" <tasks>").AppendLine();
6356 var sortedTasks = new List<int>(taskTab.Keys);
6358 foreach (int task in sortedTasks)
6360 sb.Append(" <task");
6361 WriteNameAndMessageAttribs(sb, "task", taskTab[task]);
6362 sb.Append(" value=\"").Append(task).Append("\"/>").AppendLine();
6364 sb.Append(" </tasks>").AppendLine();
6367 // Write out the maps
6368 if (mapsTab != null)
6370 sb.Append(" <maps>").AppendLine();
6371 foreach (Type enumType in mapsTab.Values)
6373 bool isbitmap = EventSource.GetCustomAttributeHelper(enumType, typeof(FlagsAttribute), flags) != null;
6374 string mapKind = isbitmap ? "bitMap" : "valueMap";
6375 sb.Append(" <").Append(mapKind).Append(" name=\"").Append(enumType.Name).Append("\">").AppendLine();
6377 // write out each enum value
6378 FieldInfo[] staticFields = enumType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
6379 foreach (FieldInfo staticField in staticFields)
6381 object constantValObj = staticField.GetRawConstantValue();
6382 if (constantValObj != null)
6385 if (constantValObj is int)
6386 hexValue = ((int)constantValObj);
6387 else if (constantValObj is long)
6388 hexValue = ((long)constantValObj);
6392 // ETW requires all bitmap values to be powers of 2. Skip the ones that are not.
6393 // TODO: Warn people about the dropping of values.
6394 if (isbitmap && ((hexValue & (hexValue - 1)) != 0 || hexValue == 0))
6397 sb.Append(" <map value=\"0x").Append(hexValue.ToString("x", CultureInfo.InvariantCulture)).Append("\"");
6398 WriteMessageAttrib(sb, "map", enumType.Name + "." + staticField.Name, staticField.Name);
6399 sb.Append("/>").AppendLine();
6402 sb.Append(" </").Append(mapKind).Append(">").AppendLine();
6404 sb.Append(" </maps>").AppendLine();
6407 // Write out the opcodes
6408 sb.Append(" <opcodes>").AppendLine();
6409 var sortedOpcodes = new List<int>(opcodeTab.Keys);
6410 sortedOpcodes.Sort();
6411 foreach (int opcode in sortedOpcodes)
6413 sb.Append(" <opcode");
6414 WriteNameAndMessageAttribs(sb, "opcode", opcodeTab[opcode]);
6415 sb.Append(" value=\"").Append(opcode).Append("\"/>").AppendLine();
6417 sb.Append(" </opcodes>").AppendLine();
6419 // Write out the keywords
6420 if (keywordTab != null)
6422 sb.Append(" <keywords>").AppendLine();
6423 var sortedKeywords = new List<ulong>(keywordTab.Keys);
6424 sortedKeywords.Sort();
6425 foreach (ulong keyword in sortedKeywords)
6427 sb.Append(" <keyword");
6428 WriteNameAndMessageAttribs(sb, "keyword", keywordTab[keyword]);
6429 sb.Append(" mask=\"0x").Append(keyword.ToString("x", CultureInfo.InvariantCulture)).Append("\"/>").AppendLine();
6431 sb.Append(" </keywords>").AppendLine();
6434 sb.Append(" <events>").AppendLine();
6436 sb.Append(" </events>").AppendLine();
6438 sb.Append(" <templates>").AppendLine();
6439 if (templates.Length > 0)
6441 sb.Append(templates);
6445 // Work around a cornercase ETW issue where a manifest with no templates causes
6446 // ETW events to not get sent to their associated channel.
6447 sb.Append(" <template tid=\"_empty\"></template>").AppendLine();
6449 sb.Append(" </templates>").AppendLine();
6451 sb.Append("</provider>").AppendLine();
6452 sb.Append("</events>").AppendLine();
6453 sb.Append("</instrumentation>").AppendLine();
6455 // Output the localization information.
6456 sb.Append("<localization>").AppendLine();
6458 List<CultureInfo> cultures = null;
6459 if (resources != null && (flags & EventManifestOptions.AllCultures) != 0)
6461 cultures = GetSupportedCultures(resources);
6465 cultures = new List<CultureInfo>();
6466 cultures.Add(CultureInfo.CurrentUICulture);
6468 #if ES_BUILD_STANDALONE
6469 var sortedStrings = new List<string>(stringTab.Keys);
6470 sortedStrings.Sort();
6473 var sortedStrings = new string[stringTab.Keys.Count];
6474 stringTab.Keys.CopyTo(sortedStrings, 0);
6475 // Avoid using public Array.Sort as that attempts to access BinaryCompatibility. Unfortunately FrameworkEventSource gets called
6476 // very early in the app domain creation, when _FusionStore is not set up yet, resulting in a failure to run the static constructory
6477 // for BinaryCompatibility. This failure is then cached and a TypeInitializationException is thrown every time some code attampts to
6478 // access BinaryCompatibility.
6479 ArraySortHelper<string>.IntrospectiveSort(sortedStrings, 0, sortedStrings.Length, Comparer<string>.Default);
6481 foreach (var ci in cultures)
6483 sb.Append(" <resources culture=\"").Append(ci.Name).Append("\">").AppendLine();
6484 sb.Append(" <stringTable>").AppendLine();
6486 foreach (var stringKey in sortedStrings)
6488 string val = GetLocalizedMessage(stringKey, ci, etwFormat: true);
6489 sb.Append(" <string id=\"").Append(stringKey).Append("\" value=\"").Append(val).Append("\"/>").AppendLine();
6491 sb.Append(" </stringTable>").AppendLine();
6492 sb.Append(" </resources>").AppendLine();
6494 sb.Append("</localization>").AppendLine();
6495 sb.AppendLine("</instrumentationManifest>");
6496 return sb.ToString();
6500 private void WriteNameAndMessageAttribs(StringBuilder stringBuilder, string elementName, string name)
6502 stringBuilder.Append(" name=\"").Append(name).Append("\"");
6503 WriteMessageAttrib(sb, elementName, name, name);
6505 private void WriteMessageAttrib(StringBuilder stringBuilder, string elementName, string name, string value)
6507 string key = elementName + "_" + name;
6508 // See if the user wants things localized.
6509 if (resources != null)
6511 // resource fallback: strings in the neutral culture will take precedence over inline strings
6512 string localizedString = resources.GetString(key, CultureInfo.InvariantCulture);
6513 if (localizedString != null)
6514 value = localizedString;
6519 stringBuilder.Append(" message=\"$(string.").Append(key).Append(")\"");
6521 if (stringTab.TryGetValue(key, out prevValue) && !prevValue.Equals(value))
6523 ManifestError(Environment.GetResourceString("EventSource_DuplicateStringKey", key), true);
6527 stringTab[key] = value;
6529 internal string GetLocalizedMessage(string key, CultureInfo ci, bool etwFormat)
6531 string value = null;
6532 if (resources != null)
6534 string localizedString = resources.GetString(key, ci);
6535 if (localizedString != null)
6537 value = localizedString;
6538 if (etwFormat && key.StartsWith("event_"))
6540 var evtName = key.Substring("event_".Length);
6541 value = TranslateToManifestConvention(value, evtName);
6545 if (etwFormat && value == null)
6546 stringTab.TryGetValue(key, out value);
6552 /// There's no API to enumerate all languages an assembly is localized into, so instead
6553 /// we enumerate through all the "known" cultures and attempt to load a corresponding satellite
6556 /// <param name="resources"></param>
6557 /// <returns></returns>
6558 private static List<CultureInfo> GetSupportedCultures(ResourceManager resources)
6560 var cultures = new List<CultureInfo>();
6561 #if !ES_BUILD_PCL && !FEATURE_CORECLR
6562 foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures /*| CultureTypes.NeutralCultures*/))
6564 if (resources.GetResourceSet(ci, true, false) != null)
6567 #endif // !ES_BUILD_PCL && !FEATURE_CORECLR
6568 if (!cultures.Contains(CultureInfo.CurrentUICulture))
6569 cultures.Insert(0, CultureInfo.CurrentUICulture);
6573 private static string GetLevelName(EventLevel level)
6575 return (((int)level >= 16) ? "" : "win:") + level.ToString();
6578 #if FEATURE_MANAGED_ETW_CHANNELS
6579 private string GetChannelName(EventChannel channel, string eventName, string eventMessage)
6581 ChannelInfo info = null;
6582 if (channelTab == null || !channelTab.TryGetValue((int)channel, out info))
6584 if (channel < EventChannel.Admin) // || channel > EventChannel.Debug)
6585 ManifestError(Environment.GetResourceString("EventSource_UndefinedChannel", channel, eventName));
6587 // allow channels to be auto-defined. The well known ones get their well known names, and the
6588 // rest get names Channel<N>. This allows users to modify the Manifest if they want more advanced features.
6589 if (channelTab == null)
6590 channelTab = new Dictionary<int, ChannelInfo>(4);
6592 string channelName = channel.ToString(); // For well know channels this is a nice name, otherwise a number
6593 if (EventChannel.Debug < channel)
6594 channelName = "Channel" + channelName; // Add a 'Channel' prefix for numbers.
6596 AddChannel(channelName, (int)channel, GetDefaultChannelAttribute(channel));
6597 if (!channelTab.TryGetValue((int)channel, out info))
6598 ManifestError(Environment.GetResourceString("EventSource_UndefinedChannel", channel, eventName));
6600 // events that specify admin channels *must* have non-null "Message" attributes
6601 if (resources != null && eventMessage == null)
6602 eventMessage = resources.GetString("event_" + eventName, CultureInfo.InvariantCulture);
6603 if (info.Attribs.EventChannelType == EventChannelType.Admin && eventMessage == null)
6604 ManifestError(Environment.GetResourceString("EventSource_EventWithAdminChannelMustHaveMessage", eventName, info.Name));
6608 private string GetTaskName(EventTask task, string eventName)
6610 if (task == EventTask.None)
6614 if (taskTab == null)
6615 taskTab = new Dictionary<int, string>();
6616 if (!taskTab.TryGetValue((int)task, out ret))
6617 ret = taskTab[(int)task] = eventName;
6620 private string GetOpcodeName(EventOpcode opcode, string eventName)
6624 case EventOpcode.Info:
6626 case EventOpcode.Start:
6628 case EventOpcode.Stop:
6630 case EventOpcode.DataCollectionStart:
6631 return "win:DC_Start";
6632 case EventOpcode.DataCollectionStop:
6633 return "win:DC_Stop";
6634 case EventOpcode.Extension:
6635 return "win:Extension";
6636 case EventOpcode.Reply:
6638 case EventOpcode.Resume:
6639 return "win:Resume";
6640 case EventOpcode.Suspend:
6641 return "win:Suspend";
6642 case EventOpcode.Send:
6644 case EventOpcode.Receive:
6645 return "win:Receive";
6649 if (opcodeTab == null || !opcodeTab.TryGetValue((int)opcode, out ret))
6651 ManifestError(Environment.GetResourceString("EventSource_UndefinedOpcode", opcode, eventName), true);
6656 private string GetKeywords(ulong keywords, string eventName)
6659 for (ulong bit = 1; bit != 0; bit <<= 1)
6661 if ((keywords & bit) != 0)
6663 string keyword = null;
6664 if ((keywordTab == null || !keywordTab.TryGetValue(bit, out keyword)) &&
6665 (bit >= (ulong)0x1000000000000))
6667 // do not report Windows reserved keywords in the manifest (this allows the code
6668 // to be resilient to potential renaming of these keywords)
6669 keyword = string.Empty;
6671 if (keyword == null)
6673 ManifestError(Environment.GetResourceString("EventSource_UndefinedKeyword", "0x" + bit.ToString("x", CultureInfo.CurrentCulture), eventName), true);
6674 keyword = string.Empty;
6676 if (ret.Length != 0 && keyword.Length != 0)
6678 ret = ret + keyword;
6683 private string GetTypeName(Type type)
6687 FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
6688 var typeName = GetTypeName(fields[0].FieldType);
6689 return typeName.Replace("win:Int", "win:UInt"); // ETW requires enums to be unsigned.
6691 switch (type.GetTypeCode())
6693 case TypeCode.Boolean:
6694 return "win:Boolean";
6698 case TypeCode.UInt16:
6699 return "win:UInt16";
6700 case TypeCode.UInt32:
6701 return "win:UInt32";
6702 case TypeCode.UInt64:
6703 return "win:UInt64";
6704 case TypeCode.SByte:
6706 case TypeCode.Int16:
6708 case TypeCode.Int32:
6710 case TypeCode.Int64:
6712 case TypeCode.String:
6713 return "win:UnicodeString";
6714 case TypeCode.Single:
6716 case TypeCode.Double:
6717 return "win:Double";
6718 case TypeCode.DateTime:
6719 return "win:FILETIME";
6721 if (type == typeof(Guid))
6723 else if (type == typeof(IntPtr))
6724 return "win:Pointer";
6725 else if ((type.IsArray || type.IsPointer) && type.GetElementType() == typeof(byte))
6726 return "win:Binary";
6727 ManifestError(Environment.GetResourceString("EventSource_UnsupportedEventTypeInManifest", type.Name), true);
6728 return string.Empty;
6732 private static void UpdateStringBuilder(ref StringBuilder stringBuilder, string eventMessage, int startIndex, int count)
6734 if (stringBuilder == null)
6735 stringBuilder = new StringBuilder();
6736 stringBuilder.Append(eventMessage, startIndex, count);
6739 // Manifest messages use %N conventions for their message substitutions. Translate from
6740 // .NET conventions. We can't use RegEx for this (we are in mscorlib), so we do it 'by hand'
6741 private string TranslateToManifestConvention(string eventMessage, string evtName)
6743 StringBuilder stringBuilder = null; // We lazily create this
6744 int writtenSoFar = 0;
6748 if (i >= eventMessage.Length)
6750 if (stringBuilder == null)
6751 return eventMessage;
6752 UpdateStringBuilder(ref stringBuilder, eventMessage, writtenSoFar, i - writtenSoFar);
6753 return stringBuilder.ToString();
6756 if (eventMessage[i] == '%')
6758 // handle format message escaping character '%' by escaping it
6759 UpdateStringBuilder(ref stringBuilder, eventMessage, writtenSoFar, i - writtenSoFar);
6760 stringBuilder.Append("%%");
6764 else if (i < eventMessage.Length - 1 &&
6765 (eventMessage[i] == '{' && eventMessage[i + 1] == '{' || eventMessage[i] == '}' && eventMessage[i + 1] == '}'))
6767 // handle C# escaped '{" and '}'
6768 UpdateStringBuilder(ref stringBuilder, eventMessage, writtenSoFar, i - writtenSoFar);
6769 stringBuilder.Append(eventMessage[i]);
6773 else if (eventMessage[i] == '{')
6775 int leftBracket = i;
6778 while (i < eventMessage.Length && Char.IsDigit(eventMessage[i]))
6780 argNum = argNum * 10 + eventMessage[i] - '0';
6783 if (i < eventMessage.Length && eventMessage[i] == '}')
6786 UpdateStringBuilder(ref stringBuilder, eventMessage, writtenSoFar, leftBracket - writtenSoFar);
6787 int manIndex = TranslateIndexToManifestConvention(argNum, evtName);
6788 stringBuilder.Append('%').Append(manIndex);
6789 // An '!' after the insert specifier {n} will be interpreted as a literal.
6790 // We'll escape it so that mc.exe does not attempt to consider it the
6791 // beginning of a format string.
6792 if (i < eventMessage.Length && eventMessage[i] == '!')
6795 stringBuilder.Append("%!");
6801 ManifestError(Environment.GetResourceString("EventSource_UnsupportedMessageProperty", evtName, eventMessage));
6804 else if ((chIdx = "&<>'\"\r\n\t".IndexOf(eventMessage[i])) >= 0)
6806 string[] escapes = { "&", "<", ">", "'", """, "%r", "%n", "%t" };
6807 var update = new Action<char, string>(
6810 UpdateStringBuilder(ref stringBuilder, eventMessage, writtenSoFar, i - writtenSoFar);
6812 stringBuilder.Append(escape);
6815 update(eventMessage[i], escapes[chIdx]);
6822 private int TranslateIndexToManifestConvention(int idx, string evtName)
6824 List<int> byteArrArgIndices;
6825 if (perEventByteArrayArgIndices.TryGetValue(evtName, out byteArrArgIndices))
6827 foreach (var byArrIdx in byteArrArgIndices)
6829 if (idx >= byArrIdx)
6838 #if FEATURE_MANAGED_ETW_CHANNELS
6842 public ulong Keywords;
6843 public EventChannelAttribute Attribs;
6847 Dictionary<int, string> opcodeTab;
6848 Dictionary<int, string> taskTab;
6849 #if FEATURE_MANAGED_ETW_CHANNELS
6850 Dictionary<int, ChannelInfo> channelTab;
6852 Dictionary<ulong, string> keywordTab;
6853 Dictionary<string, Type> mapsTab;
6855 Dictionary<string, string> stringTab; // Maps unlocalized strings to localized ones
6857 #if FEATURE_MANAGED_ETW_CHANNELS
6858 ulong nextChannelKeywordBit = 0x8000000000000000; // available Keyword bit to be used for next channel definition
6859 const int MaxCountChannels = 8; // a manifest can defined at most 8 ETW channels
6862 StringBuilder sb; // Holds the provider information.
6863 StringBuilder events; // Holds the events.
6864 StringBuilder templates;
6866 #if FEATURE_MANAGED_ETW_CHANNELS
6867 string providerName;
6869 ResourceManager resources; // Look up localized strings here.
6870 EventManifestOptions flags;
6871 IList<string> errors; // list of currently encountered errors
6872 Dictionary<string, List<int>> perEventByteArrayArgIndices; // "event_name" -> List_of_Indices_of_Byte[]_Arg
6874 // State we track between StartEvent and EndEvent.
6875 string eventName; // Name of the event currently being processed.
6876 int numParams; // keeps track of the number of args the event has.
6877 List<int> byteArrArgIndices; // keeps track of the index of each byte[] argument
6882 /// Used to send the m_rawManifest into the event dispatcher as a series of events.
6884 internal struct ManifestEnvelope
6886 public const int MaxChunkSize = 0xFF00;
6887 public enum ManifestFormats : byte
6889 SimpleXmlFormat = 1, // simply dump the XML manifest as UTF8
6892 public ManifestFormats Format;
6893 public byte MajorVersion;
6894 public byte MinorVersion;
6896 public ushort TotalChunks;
6897 public ushort ChunkNumber;