Ensure that no exception is thrown when static properties on the Activity type are...
authorLayomi Akinrinade <laakinri@microsoft.com>
Thu, 13 Aug 2020 14:18:49 +0000 (07:18 -0700)
committerGitHub <noreply@github.com>
Thu, 13 Aug 2020 14:18:49 +0000 (07:18 -0700)
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs

index 408cf3c..fdfbd90 100644 (file)
@@ -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(
index 4f20f3e..83fddd0 100644 (file)
@@ -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);
+            }
+        }
     }
 
     /****************************************************************************/