From: Layomi Akinrinade Date: Thu, 13 Aug 2020 14:18:49 +0000 (-0700) Subject: Ensure that no exception is thrown when static properties on the Activity type are... X-Git-Tag: submit/tizen/20210909.063632~6025 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6cfc044a32d2c06dbf7075b490200d18ddd7125b;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Ensure that no exception is thrown when static properties on the Activity type are passed to EventListener (#40549) --- diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs index 408cf3c..fdfbd90 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs @@ -948,6 +948,12 @@ namespace System.Diagnostics Logger.Message($"Property {propertyName} not found on {type}"); return new PropertyFetch(type); } + // Delegate creation below is incompatible with static properties. + else if (propertyInfo.GetMethod?.IsStatic == true || propertyInfo.SetMethod?.IsStatic == true) + { + Logger.Message($"Property {propertyName} is static."); + return new PropertyFetch(type); + } Type typedPropertyFetcher = typeInfo.IsValueType ? typeof(ValueTypedFetchProperty<,>) : typeof(RefTypedFetchProperty<,>); Type instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType( diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs index 4f20f3e..83fddd0 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs @@ -859,6 +859,39 @@ namespace System.Diagnostics.Tests } Assert.Equal(string.Join(',', a.Tags), e.Arguments["ActivityTags"]); } + + [Fact] + public void NoExceptionThrownWhenProcessingStaticActivityProperties() + { + // Ensures that no exception is thrown when static properties on the Activity type are passed to EventListener. + + using (var eventListener = new TestDiagnosticSourceEventListener()) + using (var diagnosticListener = new DiagnosticListener("MySource")) + { + string activityProps = + "-DummyProp" + + ";ActivityEvents=*Activity.DefaultIdFormat" + + ";ActivityBaggage=*Activity.Current" + + ";ActivityContext=*Activity.ForceDefaultIdFormat"; + eventListener.Enable( + "MySource/TestActivity1.Start@Activity1Start:" + activityProps + "\r\n" + + "MySource/TestActivity1.Stop@Activity1Stop:" + activityProps + "\r\n" + + "MySource/TestActivity2.Start@Activity2Start:" + activityProps + "\r\n" + + "MySource/TestActivity2.Stop@Activity2Stop:" + activityProps + "\r\n" + ); + + Activity activity1 = new Activity("TestActivity1"); + activity1.SetIdFormat(ActivityIdFormat.W3C); + activity1.TraceStateString = "hi_there"; + activity1.AddTag("one", "1"); + activity1.AddTag("two", "2"); + + var obj = new { DummyProp = "val" }; + + diagnosticListener.StartActivity(activity1, obj); + Assert.Equal(1, eventListener.EventCount); + } + } } /****************************************************************************/