From: Miha Zupan Date: Tue, 27 Jul 2021 09:28:10 +0000 (-0700) Subject: Honor ThrowOnEventWriteErrors for exceptions thrown by EventListeners (#56232) X-Git-Tag: accepted/tizen/unified/20220110.054933~897 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9d9b465f0be92a0d7584c4b8ad6b873be7f0dbc9;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Honor ThrowOnEventWriteErrors for exceptions thrown by EventListeners (#56232) --- diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.cs index a352374..d7c7046 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.cs @@ -448,5 +448,49 @@ namespace BasicEventSourceTests TestUtilities.CheckNoEventSourcesRunning("Stop"); } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Test_EventListenerThrows_ExceptionIsNotRethrownToCaller(bool setThrowOnEventWriteErrorsFlag) + { + TestUtilities.CheckNoEventSourcesRunning("Start"); + + using (var log = new EventSourceTest(throwOnEventWriteErrors: setThrowOnEventWriteErrorsFlag)) + { + using (var listener = new EventListenerListener()) + { + listener.EventSourceSynchronousEnable(log); + + var thrownException = new Exception("Oops"); + string outOfBandMessage = null; + + listener.EventWritten += (_, e) => + { + if (e.EventId == 0) + { + outOfBandMessage = e.Message; + } + + throw thrownException; + }; + + try + { + log.Event0(); + Assert.False(setThrowOnEventWriteErrorsFlag); + } + catch (EventSourceException ex) + { + Assert.True(setThrowOnEventWriteErrorsFlag); + Assert.Same(thrownException, ex.InnerException); + } + + Assert.Contains(thrownException.Message, outOfBandMessage); + } + } + + TestUtilities.CheckNoEventSourcesRunning("Stop"); + } } } diff --git a/src/libraries/System.Diagnostics.Tracing/tests/CustomEventSources/EventSourceTest.cs b/src/libraries/System.Diagnostics.Tracing/tests/CustomEventSources/EventSourceTest.cs index 5cbd28b..7a6734e 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/CustomEventSources/EventSourceTest.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/CustomEventSources/EventSourceTest.cs @@ -26,8 +26,9 @@ namespace SdtEventSources [EventSource(Guid = "69e2aa3e-083b-5014-cad4-3e511a0b94cf", Name = "EventSourceTest")] public sealed class EventSourceTest : EventSource { - public EventSourceTest(bool useSelfDescribingEvents = false) - : base(true) + public EventSourceTest(bool useSelfDescribingEvents = false, bool throwOnEventWriteErrors = false) + : base((useSelfDescribingEvents ? EventSourceSettings.EtwSelfDescribingEventFormat : EventSourceSettings.EtwManifestEventFormat) + | (throwOnEventWriteErrors ? EventSourceSettings.ThrowOnEventWriteErrors : 0)) { } protected override void OnEventCommand(EventCommandEventArgs command) 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 87d09e1..556e4e8 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 @@ -2127,7 +2127,7 @@ namespace System.Diagnostics.Tracing } } - if (lastThrownException != null) + if (lastThrownException != null && ThrowOnEventWriteErrors) { throw new EventSourceException(lastThrownException); }