Missing overrides in LoggingHttpMessageHandler and LoggingScopeHttpMessageHandler...
authormphelt <25581948+mphelt@users.noreply.github.com>
Thu, 4 May 2023 13:49:36 +0000 (15:49 +0200)
committerGitHub <noreply@github.com>
Thu, 4 May 2023 13:49:36 +0000 (15:49 +0200)
* add missing overrides in LoggingHttpMessageHandler and LoggingScopeHttpMessageHandler (https://github.com/dotnet/runtime/issues/85104)

* Update LoggingUriOutputTests.cs

* Update LoggingHttpMessageHandler.cs

* Update LoggingScopeHttpMessageHandler.cs

* Update LoggingScopeHttpMessageHandler.cs

* Update LoggingUriOutputTests.cs

* Update LoggingScopeHttpMessageHandler.cs

* Update LoggingUriOutputTests.cs

* Update LoggingUriOutputTests.cs

* Update LoggingUriOutputTests.cs

* Update LoggingUriOutputTests.cs

* Update LoggingHttpMessageHandler.cs

* Update LoggingScopeHttpMessageHandler.cs

* Update LoggingHttpMessageHandler.cs

* Update LoggingScopeHttpMessageHandler.cs

* Update LoggingHttpMessageHandler.cs

* Update LoggingHttpMessageHandler.cs

* Update LoggingScopeHttpMessageHandler.cs

* Code style update

* back to private methods

* merge with dotnet/runtime (#7)

src/libraries/Microsoft.Extensions.Http/src/Logging/LoggingHttpMessageHandler.cs
src/libraries/Microsoft.Extensions.Http/src/Logging/LoggingScopeHttpMessageHandler.cs
src/libraries/Microsoft.Extensions.Http/tests/Microsoft.Extensions.Http.Tests/Logging/LoggingUriOutputTests.cs

index 62ec57c..c91bc63 100644 (file)
@@ -47,9 +47,7 @@ namespace Microsoft.Extensions.Http.Logging
             _options = options;
         }
 
-        /// <inheritdoc />
-        /// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
-        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+        private Task<HttpResponseMessage> SendCoreAsync(HttpRequestMessage request, bool useAsync, CancellationToken cancellationToken)
         {
             ThrowHelper.ThrowIfNull(request);
             return Core(request, cancellationToken);
@@ -62,13 +60,31 @@ namespace Microsoft.Extensions.Http.Logging
                 // not really anything to surround.
                 Log.RequestStart(_logger, request, shouldRedactHeaderValue);
                 var stopwatch = ValueStopwatch.StartNew();
-                HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
+                HttpResponseMessage response = useAsync
+                    ? await base.SendAsync(request, cancellationToken).ConfigureAwait(false)
+#if NET5_0_OR_GREATER
+                    : base.Send(request, cancellationToken);
+#else
+                    : throw new NotImplementedException("Unreachable code");
+#endif
                 Log.RequestEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
 
                 return response;
             }
         }
 
+        /// <inheritdoc />
+        /// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
+        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+            => SendCoreAsync(request, useAsync: true, cancellationToken);
+
+#if NET5_0_OR_GREATER
+        /// <inheritdoc />
+        /// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
+        protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
+            => SendCoreAsync(request, useAsync: false, cancellationToken).GetAwaiter().GetResult();
+#endif
+
         // Used in tests.
         internal static class Log
         {
index a2cadee..d111c04 100644 (file)
@@ -47,9 +47,7 @@ namespace Microsoft.Extensions.Http.Logging
             _options = options;
         }
 
-        /// <inheritdoc />
-        /// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
-        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+        private Task<HttpResponseMessage> SendCoreAsync(HttpRequestMessage request, bool useAsync, CancellationToken cancellationToken)
         {
             ThrowHelper.ThrowIfNull(request);
             return Core(request, cancellationToken);
@@ -63,7 +61,13 @@ namespace Microsoft.Extensions.Http.Logging
                 using (Log.BeginRequestPipelineScope(_logger, request))
                 {
                     Log.RequestPipelineStart(_logger, request, shouldRedactHeaderValue);
-                    HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
+                    HttpResponseMessage response = useAsync
+                        ? await base.SendAsync(request, cancellationToken).ConfigureAwait(false)
+#if NET5_0_OR_GREATER
+                        : base.Send(request, cancellationToken);
+#else
+                        : throw new NotImplementedException("Unreachable code");
+#endif
                     Log.RequestPipelineEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
 
                     return response;
@@ -71,6 +75,18 @@ namespace Microsoft.Extensions.Http.Logging
             }
         }
 
+        /// <inheritdoc />
+        /// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
+        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+            => SendCoreAsync(request, useAsync: true, cancellationToken);
+
+#if NET5_0_OR_GREATER
+        /// <inheritdoc />
+        /// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
+        protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
+            => SendCoreAsync(request, useAsync: false, cancellationToken).GetAwaiter().GetResult();
+#endif
+
         // Used in tests
         internal static class Log
         {
index 9f5588a..c7a30a5 100644 (file)
@@ -1,6 +1,7 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System;
 using System.Linq;
 using System.Net.Http;
 using System.Threading;
@@ -90,6 +91,83 @@ namespace Microsoft.Extensions.Http.Tests.Logging
             Assert.Equal("HTTP GET http://api.example.com/search?term=Western%20Australia", message.Scope.ToString());
         }
 
+#if NET5_0_OR_GREATER
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))]
+        public void LoggingHttpMessageHandler_LogsAbsoluteUri_Sync()
+        {
+            // Arrange
+            var sink = new TestSink();
+
+            var serviceCollection = new ServiceCollection();
+            serviceCollection.AddLogging();
+            serviceCollection.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
+
+            serviceCollection
+                .AddHttpClient("test")
+                .ConfigurePrimaryHttpMessageHandler(() => new TestMessageHandler());
+
+            var services = serviceCollection.BuildServiceProvider();
+
+            var client = services.GetRequiredService<IHttpClientFactory>().CreateClient("test");
+
+
+            // Act
+            var request = new HttpRequestMessage(HttpMethod.Get, "http://api.example.com/search?term=Western%20Australia");
+
+            client.Send(request);
+
+            // Assert
+            var messages = sink.Writes.ToArray();
+
+            var message = Assert.Single(messages.Where(m =>
+            {
+                return
+                    m.EventId == LoggingHttpMessageHandler.Log.EventIds.RequestStart &&
+                    m.LoggerName == "System.Net.Http.HttpClient.test.ClientHandler";
+            }));
+
+            Assert.Equal("Sending HTTP request GET http://api.example.com/search?term=Western%20Australia", message.Message);
+        }
+
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))]
+        public void LoggingScopeHttpMessageHandler_LogsAbsoluteUri_Sync()
+        {
+            // Arrange
+            var sink = new TestSink();
+
+            var serviceCollection = new ServiceCollection();
+            serviceCollection.AddLogging();
+            serviceCollection.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
+
+            serviceCollection
+                .AddHttpClient("test")
+                .ConfigurePrimaryHttpMessageHandler(() => new TestMessageHandler());
+
+            var services = serviceCollection.BuildServiceProvider();
+
+            var client = services.GetRequiredService<IHttpClientFactory>().CreateClient("test");
+
+
+            // Act
+            var request = new HttpRequestMessage(HttpMethod.Get, "http://api.example.com/search?term=Western%20Australia");
+
+            client.Send(request);
+
+            // Assert
+            var messages = sink.Writes.ToArray();
+
+            var message = Assert.Single(messages.Where(m =>
+            {
+                return
+                    m.EventId == LoggingScopeHttpMessageHandler.Log.EventIds.PipelineStart &&
+                    m.LoggerName == "System.Net.Http.HttpClient.test.LogicalHandler";
+            }));
+
+            Assert.Equal("Start processing HTTP request GET http://api.example.com/search?term=Western%20Australia", message.Message);
+            Assert.Equal("HTTP GET http://api.example.com/search?term=Western%20Australia", message.Scope.ToString());
+        }
+#endif
+
         private class TestMessageHandler : HttpClientHandler
         {
             protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
@@ -98,6 +176,10 @@ namespace Microsoft.Extensions.Http.Tests.Logging
 
                 return Task.FromResult(response);
             }
+
+#if NET5_0_OR_GREATER
+            protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) => new();
+#endif
         }
     }
 }