From c9fabaefdb4528946110914783abc122341a81cf Mon Sep 17 00:00:00 2001
From: James Newton-King <james@newtonking.com>
Date: Fri, 1 Jul 2022 21:20:04 +0800
Subject: [PATCH] Annotate hosting diagnostic events with DynamicDependency
 (#42512)

---
 .../Internal/HostingApplicationDiagnostics.cs | 20 ++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
index 4c51043c9ee..d2a3e936572 100644
--- a/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
+++ b/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs
@@ -224,7 +224,7 @@ internal sealed class HostingApplicationDiagnostics
 
     [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
         Justification = "The values being passed into Write have the commonly used properties being preserved with DynamicDependency.")]
-    private static void WriteDiagnosticEvent<TValue>(
+    private static void WriteDiagnosticEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(
         DiagnosticSource diagnosticSource, string name, TValue value)
     {
         diagnosticSource.Write(name, value);
@@ -413,6 +413,19 @@ internal sealed class HostingApplicationDiagnostics
     }
 
     // These are versions of DiagnosticSource.Start/StopActivity that don't allocate strings per call (see https://github.com/dotnet/corefx/issues/37055)
+    // DynamicDependency matches the properties selected in:
+    // https://github.com/dotnet/diagnostics/blob/7cc6fbef613cdfe5ff64393120d59d7a15e98bd6/src/Microsoft.Diagnostics.Monitoring.EventPipe/Configuration/HttpRequestSourceConfiguration.cs#L20-L33
+    [DynamicDependency(nameof(HttpContext.Request), typeof(HttpContext))]
+    [DynamicDependency(nameof(HttpRequest.Scheme), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.Host), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.PathBase), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.QueryString), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.Path), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.Method), typeof(HttpRequest))]
+    [DynamicDependency(nameof(HttpRequest.Headers), typeof(HttpRequest))]
+    [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(QueryString))]
+    [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(HostString))]
+    [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(PathString))]
     private Activity StartActivity(Activity activity, HttpContext httpContext)
     {
         activity.Start();
@@ -420,6 +433,11 @@ internal sealed class HostingApplicationDiagnostics
         return activity;
     }
 
+    // DynamicDependency matches the properties selected in:
+    // https://github.com/dotnet/diagnostics/blob/7cc6fbef613cdfe5ff64393120d59d7a15e98bd6/src/Microsoft.Diagnostics.Monitoring.EventPipe/Configuration/HttpRequestSourceConfiguration.cs#L35-L38
+    [DynamicDependency(nameof(HttpContext.Response), typeof(HttpContext))]
+    [DynamicDependency(nameof(HttpResponse.StatusCode), typeof(HttpResponse))]
+    [DynamicDependency(nameof(HttpResponse.Headers), typeof(HttpResponse))]
     private void StopActivity(Activity activity, HttpContext httpContext)
     {
         // Stop sets the end time if it was unset, but we want it set before we issue the write
-- 
GitLab