From 7ebc6006226aa30d79b31c494a5489b6c4be568e Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 26 Oct 2019 17:08:45 -0400 Subject: [PATCH] Remove ILLinkTrim.xml from System.Diagnostics.DiagnosticSource (dotnet/corefx#42124) - Use PreserveDependencyAttribute for ctors used via reflection by FetcherForProperty - Remove reflection use entirely when looking up Activity events Commit migrated from https://github.com/dotnet/corefx/commit/a5d7583c1dc46a0bbf97ba89791079fffd4f1755 --- .../src/ILLinkTrim.xml | 17 -------- .../src/System.Diagnostics.DiagnosticSource.csproj | 3 ++ .../Diagnostics/DiagnosticSourceEventSource.cs | 50 ++++++++++++---------- 3 files changed, 30 insertions(+), 40 deletions(-) delete mode 100644 src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml b/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml deleted file mode 100644 index fd1bb62..0000000 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/ILLinkTrim.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj b/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj index 215c68b..e54238b 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj @@ -19,6 +19,9 @@ + + Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs + 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 6dd477a..de875d6 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs @@ -229,7 +229,7 @@ namespace System.Diagnostics /// Used to mark the beginning of an activity /// [Event(4, Keywords = Keywords.Events)] - private void Activity1Start(string SourceName, string EventName, IEnumerable> Arguments) + private void Activity1Start(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(4, SourceName, EventName, Arguments); } @@ -238,7 +238,7 @@ namespace System.Diagnostics /// Used to mark the end of an activity /// [Event(5, Keywords = Keywords.Events)] - private void Activity1Stop(string SourceName, string EventName, IEnumerable> Arguments) + private void Activity1Stop(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(5, SourceName, EventName, Arguments); } @@ -247,7 +247,7 @@ namespace System.Diagnostics /// Used to mark the beginning of an activity /// [Event(6, Keywords = Keywords.Events)] - private void Activity2Start(string SourceName, string EventName, IEnumerable> Arguments) + private void Activity2Start(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(6, SourceName, EventName, Arguments); } @@ -256,7 +256,7 @@ namespace System.Diagnostics /// Used to mark the end of an activity that can be recursive. /// [Event(7, Keywords = Keywords.Events)] - private void Activity2Stop(string SourceName, string EventName, IEnumerable> Arguments) + private void Activity2Stop(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(7, SourceName, EventName, Arguments); } @@ -265,7 +265,7 @@ namespace System.Diagnostics /// Used to mark the beginning of an activity /// [Event(8, Keywords = Keywords.Events, ActivityOptions = EventActivityOptions.Recursive)] - private void RecursiveActivity1Start(string SourceName, string EventName, IEnumerable> Arguments) + private void RecursiveActivity1Start(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(8, SourceName, EventName, Arguments); } @@ -274,7 +274,7 @@ namespace System.Diagnostics /// Used to mark the end of an activity that can be recursive. /// [Event(9, Keywords = Keywords.Events, ActivityOptions = EventActivityOptions.Recursive)] - private void RecursiveActivity1Stop(string SourceName, string EventName, IEnumerable> Arguments) + private void RecursiveActivity1Stop(string SourceName, string EventName, IEnumerable> Arguments) { WriteEvent(9, SourceName, EventName, Arguments); } @@ -335,13 +335,14 @@ namespace System.Diagnostics } #endif + private DiagnosticSourceEventSource() #if !NO_EVENTSOURCE_COMPLEX_TYPE_SUPPORT - /// - /// This constructor uses EventSourceSettings which is only available on V4.6 and above - /// systems. We use the EventSourceSettings to turn on support for complex types. - /// - private DiagnosticSourceEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat) { } + // This constructor uses EventSourceSettings which is only available on V4.6 and above + // Use the EventSourceSettings to turn on support for complex types, if available (v4.6 and above). + : base(EventSourceSettings.EtwSelfDescribingEventFormat) #endif + { + } /// /// Called when the EventSource gets a command from a EventListener or ETW. @@ -563,19 +564,19 @@ namespace System.Diagnostics Action>>? writeEvent = null; if (activityName != null && activityName.Contains("Activity")) { - MethodInfo? writeEventMethodInfo = typeof(DiagnosticSourceEventSource).GetTypeInfo().GetDeclaredMethod(activityName); - if (writeEventMethodInfo != null) +#if !NO_EVENTSOURCE_COMPLEX_TYPE_SUPPORT + writeEvent = activityName switch { - // This looks up the activityName (which needs to be a name of an event on DiagnosticSourceEventSource - // like Activity1Start and returns that method). This allows us to have a number of them and this code - // just works. - try - { - writeEvent = (Action>>) - writeEventMethodInfo.CreateDelegate(typeof(Action>>), _eventSource); - } - catch (Exception) { } - } + nameof(Activity1Start) => _eventSource.Activity1Start, + nameof(Activity1Stop) => _eventSource.Activity1Stop, + nameof(Activity2Start) => _eventSource.Activity2Start, + nameof(Activity2Stop) => _eventSource.Activity2Stop, + nameof(RecursiveActivity1Start) => _eventSource.RecursiveActivity1Start, + nameof(RecursiveActivity1Stop) => _eventSource.RecursiveActivity1Stop, + _ => null + }; +#endif + if (writeEvent == null) _eventSource.Message("DiagnosticSource: Could not find Event to log Activity " + activityName); } @@ -890,6 +891,9 @@ namespace System.Diagnostics /// /// Create a property fetcher for a propertyName /// + [PreserveDependency(".ctor(System.Type)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/EnumeratePropertyFetch`1")] + [PreserveDependency(".ctor(System.Type, System.Reflection.PropertyInfo)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/RefTypedFetchProperty`2")] + [PreserveDependency(".ctor(System.Type, System.Reflection.PropertyInfo)", "System.Diagnostics.DiagnosticSourceEventSource/TransformSpec/PropertySpec/PropertyFetch/ValueTypedFetchProperty`2")] public static PropertyFetch FetcherForProperty(Type? type, string propertyName) { if (propertyName == null) -- 2.7.4