Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Falk <noahfalk@users.noreply.github.com>2021-07-14 13:29:04 +0300
committerGitHub <noreply@github.com>2021-07-14 13:29:04 +0300
commitafaf666eff08435123eb649ac138419f4c9b9344 (patch)
tree091edb1aee8ce5e13cddb6b2539237ae4016d7a1
parent7bd472498e690e9421df86d5a9d728faa939742c (diff)
DiagnosticSourceEventSource supports base class properties (#55613)
Fixes #41300 Previously DiagnosticSourceEventSource would only iterate and recognize properties that were declared on the most derived type. Now it can capture properties that were inherited from a base class too.
-rw-r--r--src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs4
-rw-r--r--src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs40
2 files changed, 42 insertions, 2 deletions
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 72f7572313f..d2ee34fe439 100644
--- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
+++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
@@ -1099,7 +1099,7 @@ namespace System.Diagnostics
{
TransformSpec? newSerializableArgs = null;
TypeInfo curTypeInfo = type.GetTypeInfo();
- foreach (PropertyInfo property in curTypeInfo.DeclaredProperties)
+ foreach (PropertyInfo property in curTypeInfo.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
// prevent TransformSpec from attempting to implicitly transform index properties
if (property.GetMethod == null || property.GetMethod!.GetParameters().Length > 0)
@@ -1319,7 +1319,7 @@ namespace System.Diagnostics
}
else
{
- PropertyInfo? propertyInfo = typeInfo.GetDeclaredProperty(propertyName);
+ PropertyInfo? propertyInfo = typeInfo.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
if (propertyInfo == null)
{
Log.Message($"Property {propertyName} not found on {type}. Ensure the name is spelled correctly. If you published the application with PublishTrimmed=true, ensure the property was not trimmed away.");
diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs
index 2a59c8b48e4..ecf73aa7f06 100644
--- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs
+++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceEventSourceBridgeTests.cs
@@ -491,6 +491,40 @@ namespace System.Diagnostics.Tests
}
/// <summary>
+ /// Tests that DiagnosticSourceEventSource can read property values from base classes
+ /// </summary>
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public void TestBaseClassProperties()
+ {
+ RemoteExecutor.Invoke(() =>
+ {
+ using (var eventSourceListener = new TestDiagnosticSourceEventListener())
+ using (var diagnosticSourceListener = new DiagnosticListener("TestBaseClassProperties"))
+ {
+ Assert.Equal(0, eventSourceListener.EventCount);
+ eventSourceListener.Enable(
+ " TestBaseClassProperties/TestEvent1:Point_X=Point.X;Point_Y=Point.Y;Url_2=Url2\r\n");
+
+ /***************************************************************************************/
+ // Emit an event that matches the first pattern.
+ MyClass val = new MyDerivedClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 }, Url2 = "Second url", AnotherString = "another" };
+ if (diagnosticSourceListener.IsEnabled("TestEvent1"))
+ diagnosticSourceListener.Write("TestEvent1", val);
+
+ Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
+ Assert.Equal("TestBaseClassProperties", eventSourceListener.LastEvent.SourceName);
+ Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
+ Assert.Equal(7, eventSourceListener.LastEvent.Arguments.Count);
+ Assert.Equal("another", eventSourceListener.LastEvent.Arguments["AnotherString"]);
+ Assert.Equal("3", eventSourceListener.LastEvent.Arguments["Point_X"]);
+ Assert.Equal("5", eventSourceListener.LastEvent.Arguments["Point_Y"]);
+ Assert.Equal("Second url", eventSourceListener.LastEvent.Arguments["Url_2"]);
+ eventSourceListener.ResetEventCountAndLastEvent();
+ }
+ }).Dispose();
+ }
+
+ /// <summary>
/// Test that things work properly for Linux newline conventions.
/// </summary>
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
@@ -1314,6 +1348,12 @@ namespace System.Diagnostics.Tests
public MyPoint Point { get; set; }
}
+ internal class MyDerivedClass : MyClass
+ {
+ public string Url2 { get; set; }
+ public string AnotherString { get; set; }
+ }
+
/// <summary>
/// classes for test data.
/// </summary>