From 5249e7f62cd06767b02e0edce7b9ffb66883d8f9 Mon Sep 17 00:00:00 2001 From: David Mason Date: Tue, 28 Mar 2023 16:35:17 -0700 Subject: [PATCH] Changes --- .../BasicEventSourceTest/TestCallbacks.cs | 90 +++++++++++++++++++ .../Diagnostics/Tracing/CounterGroup.cs | 12 ++- 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestCallbacks.cs diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestCallbacks.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestCallbacks.cs new file mode 100644 index 00000000000000..f037c92bd5c68f --- /dev/null +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestCallbacks.cs @@ -0,0 +1,90 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +#if USE_MDT_EVENTSOURCE +using Microsoft.Diagnostics.Tracing; +#else +using System.Diagnostics.Tracing; +#endif +using System.Reflection; + +namespace BasicEventSourceTests +{ + public class TestsEventSourceCallbacks + { + /// + /// Validates that the EventProvider AppDomain.ProcessExit handler does not keep the EventProvider instance + /// alive. + /// + [Fact] + public void Test_EventSource_Lifetime() + { + using (var source = new CallbacksTestEventSource()) + { + bool isDisabledInDelegate = false; + source.EventCommandExecuted += (sender, args) => + { + if (args.Command == EventCommand.Disable) + { + EventSource eventSource = (EventSource)sender; + isDisabledInDelegate = !eventSource.IsEnabled(); + } + }; + + using (var listener = new CallbacksEventListener()) + { + source.Event(); + } + + if (!source._isDisabledInCallback) + { + Assert.Fail("EventSource was still enabled in OnEventCommand callback"); + } + + if (!isDisabledInDelegate) + { + Assert.Fail("EventSource was still enabled in EventCommandExecuted delegate"); + } + } + } + + private class CallbacksEventListener : EventListener + { + protected override void OnEventSourceCreated(EventSource eventSource) + { + base.OnEventSourceCreated(eventSource); + + if (eventSource.Name.Equals("TestsEventSourceCallbacks.CallbacksTestEventSource")) + { + EnableEvents(eventSource, EventLevel.Verbose); + } + } + } + + [EventSource(Name = "TestsEventSourceCallbacks.CallbacksTestEventSource")] + private class CallbacksTestEventSource : EventSource + { + internal bool _isDisabledInCallback; + + [Event(1)] + public void Event() + { + WriteEvent(1); + } + + [NonEvent] + protected override void OnEventCommand(EventCommandEventArgs command) + { + base.OnEventCommand(command); + + _isDisabledInCallback = !IsEnabled(); + } + } + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs index 4263aff738a4a2..3a0ef6513d5801 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs @@ -56,11 +56,16 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e) { Debug.Assert(e.Arguments != null); - if (e.Arguments.TryGetValue("EventCounterIntervalSec", out string? valueStr) - && float.TryParse(valueStr, out float value)) + string? valueStr = null; + float value = 0.0f; + if (!e.Arguments.TryGetValue("EventCounterIntervalSec", out valueStr) + || !float.TryParse(valueStr, out value)) { - intervalValue = value; + // Command is Enable but no EventCounterIntervalSec arg so ignore + return; } + + intervalValue = value; } if ((e.Command == EventCommand.Disable && !_eventSource.IsEnabled()) || intervalValue <= 0) @@ -74,6 +79,7 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e) Debug.Assert((s_counterGroupEnabledList == null && !_eventSource.IsEnabled()) || (_eventSource.IsEnabled() && s_counterGroupEnabledList!.Contains(this)) + || (intervalValue <= 0 && !s_counterGroupEnabledList!.Contains(this)) || (!_eventSource.IsEnabled() && !s_counterGroupEnabledList!.Contains(this))); } }