From c05a7e0d8f6217483d4bc097f0a873fb891c8e92 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Wed, 28 Apr 2021 11:43:47 +0200 Subject: [PATCH 1/5] Respect EventSource::IsSupported setting in more codepaths --- .../System/Diagnostics/Tracing/EventSource.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 552878213779c..da68e14c1b0c7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -494,6 +494,9 @@ public static void SendCommand(EventSource eventSource, EventCommand command, ID /// public override string ToString() { + if (!IsSupported) + return string.Empty; + return SR.Format(SR.EventSource_ToString, Name, Guid); } @@ -667,8 +670,15 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv /// the ETW provider name. /// protected EventSource() - : this(EventSourceSettings.EtwManifestEventFormat) { + if (!IsSupported) + return; + +#if FEATURE_PERFTRACING + m_eventHandleTable = new TraceLoggingEventHandleTable(); +#endif + m_config = EventSourceSettings.EtwManifestEventFormat; + Initialize(null); } /// @@ -702,19 +712,23 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { } /// A collection of key-value strings (must be an even number). protected EventSource(EventSourceSettings settings, params string[]? traits) { - if (IsSupported) - { + if (!IsSupported) + return; + #if FEATURE_PERFTRACING - m_eventHandleTable = new TraceLoggingEventHandleTable(); + m_eventHandleTable = new TraceLoggingEventHandleTable(); #endif - m_config = ValidateSettings(settings); + m_config = ValidateSettings(settings); + Initialize(traits); + } - Type myType = this.GetType(); - Guid eventSourceGuid = GetGuid(myType); - string eventSourceName = GetName(myType); + private void Initialize(string[]? traits) + { + Type myType = this.GetType(); + Guid eventSourceGuid = GetGuid(myType); + string eventSourceName = GetName(myType); - Initialize(eventSourceGuid, eventSourceName, traits); - } + Initialize(eventSourceGuid, eventSourceName, traits); } #if FEATURE_PERFTRACING @@ -1547,6 +1561,8 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSe /// private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[]? traits) { + Debug.Assert(IsSupported); + try { m_traits = traits; From b8faee6caf5ecdad7cc1d9a2de286fbe2432b102 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 25 May 2021 12:20:08 +0200 Subject: [PATCH 2/5] PR feedback --- .../src/System/Diagnostics/Tracing/EventSource.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index da68e14c1b0c7..711610b5dc523 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -670,15 +670,8 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv /// the ETW provider name. /// protected EventSource() + : this(EventSourceSettings.EtwManifestEventFormat, null) { - if (!IsSupported) - return; - -#if FEATURE_PERFTRACING - m_eventHandleTable = new TraceLoggingEventHandleTable(); -#endif - m_config = EventSourceSettings.EtwManifestEventFormat; - Initialize(null); } /// From dc5b75955a67cd25eaf7ad6b8a2fcea17b33d8ae Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Mon, 7 Jun 2021 15:40:00 +0200 Subject: [PATCH 3/5] Undo cosmetic change --- .../System/Diagnostics/Tracing/EventSource.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 711610b5dc523..55a4f80690261 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -705,23 +705,19 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { } /// A collection of key-value strings (must be an even number). protected EventSource(EventSourceSettings settings, params string[]? traits) { - if (!IsSupported) - return; - + if (IsSupported) + { #if FEATURE_PERFTRACING - m_eventHandleTable = new TraceLoggingEventHandleTable(); + m_eventHandleTable = new TraceLoggingEventHandleTable(); #endif - m_config = ValidateSettings(settings); - Initialize(traits); - } + m_config = ValidateSettings(settings); - private void Initialize(string[]? traits) - { - Type myType = this.GetType(); - Guid eventSourceGuid = GetGuid(myType); - string eventSourceName = GetName(myType); + Type myType = this.GetType(); + Guid eventSourceGuid = GetGuid(myType); + string eventSourceName = GetName(myType); - Initialize(eventSourceGuid, eventSourceName, traits); + Initialize(eventSourceGuid, eventSourceName, traits); + } } #if FEATURE_PERFTRACING From d03c649e24aedeed9381f38d333db45fec13c0e3 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Mon, 7 Jun 2021 15:42:11 +0200 Subject: [PATCH 4/5] Undo one more minor perf tweak --- .../src/System/Diagnostics/Tracing/EventSource.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index e65338d7e52b6..250c9560e5bc2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -671,7 +671,7 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv /// the ETW provider name. /// protected EventSource() - : this(EventSourceSettings.EtwManifestEventFormat, null) + : this(EventSourceSettings.EtwManifestEventFormat) { } @@ -1547,8 +1547,6 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSe /// private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[]? traits) { - Debug.Assert(IsSupported); - try { m_traits = traits; From e6da1b7163ffb7ca746abf03e8704f3a7f8015cc Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 22 Jun 2021 12:27:11 +0200 Subject: [PATCH 5/5] Call base instead of returning Empty value --- .../src/System/Diagnostics/Tracing/EventSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 250c9560e5bc2..d6093b4fb66bb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -496,7 +496,7 @@ public static void SendCommand(EventSource eventSource, EventCommand command, ID public override string ToString() { if (!IsSupported) - return string.Empty; + return base.ToString()!; return SR.Format(SR.EventSource_ToString, Name, Guid); }