Skip to content

Commit 4b104fb

Browse files
authored
Missing overrides in LoggingHttpMessageHandler and LoggingScopeHttpMessageHandler (dotnet#85143)
* add missing overrides in LoggingHttpMessageHandler and LoggingScopeHttpMessageHandler (dotnet#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)
1 parent 2328c12 commit 4b104fb

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

src/libraries/Microsoft.Extensions.Http/src/Logging/LoggingHttpMessageHandler.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public LoggingHttpMessageHandler(ILogger logger, HttpClientFactoryOptions option
4747
_options = options;
4848
}
4949

50-
/// <inheritdoc />
51-
/// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
52-
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
50+
private Task<HttpResponseMessage> SendCoreAsync(HttpRequestMessage request, bool useAsync, CancellationToken cancellationToken)
5351
{
5452
ThrowHelper.ThrowIfNull(request);
5553
return Core(request, cancellationToken);
@@ -62,13 +60,31 @@ async Task<HttpResponseMessage> Core(HttpRequestMessage request, CancellationTok
6260
// not really anything to surround.
6361
Log.RequestStart(_logger, request, shouldRedactHeaderValue);
6462
var stopwatch = ValueStopwatch.StartNew();
65-
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
63+
HttpResponseMessage response = useAsync
64+
? await base.SendAsync(request, cancellationToken).ConfigureAwait(false)
65+
#if NET5_0_OR_GREATER
66+
: base.Send(request, cancellationToken);
67+
#else
68+
: throw new NotImplementedException("Unreachable code");
69+
#endif
6670
Log.RequestEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
6771

6872
return response;
6973
}
7074
}
7175

76+
/// <inheritdoc />
77+
/// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
78+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
79+
=> SendCoreAsync(request, useAsync: true, cancellationToken);
80+
81+
#if NET5_0_OR_GREATER
82+
/// <inheritdoc />
83+
/// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
84+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
85+
=> SendCoreAsync(request, useAsync: false, cancellationToken).GetAwaiter().GetResult();
86+
#endif
87+
7288
// Used in tests.
7389
internal static class Log
7490
{

src/libraries/Microsoft.Extensions.Http/src/Logging/LoggingScopeHttpMessageHandler.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public LoggingScopeHttpMessageHandler(ILogger logger, HttpClientFactoryOptions o
4747
_options = options;
4848
}
4949

50-
/// <inheritdoc />
51-
/// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
52-
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
50+
private Task<HttpResponseMessage> SendCoreAsync(HttpRequestMessage request, bool useAsync, CancellationToken cancellationToken)
5351
{
5452
ThrowHelper.ThrowIfNull(request);
5553
return Core(request, cancellationToken);
@@ -63,14 +61,32 @@ async Task<HttpResponseMessage> Core(HttpRequestMessage request, CancellationTok
6361
using (Log.BeginRequestPipelineScope(_logger, request))
6462
{
6563
Log.RequestPipelineStart(_logger, request, shouldRedactHeaderValue);
66-
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
64+
HttpResponseMessage response = useAsync
65+
? await base.SendAsync(request, cancellationToken).ConfigureAwait(false)
66+
#if NET5_0_OR_GREATER
67+
: base.Send(request, cancellationToken);
68+
#else
69+
: throw new NotImplementedException("Unreachable code");
70+
#endif
6771
Log.RequestPipelineEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
6872

6973
return response;
7074
}
7175
}
7276
}
7377

78+
/// <inheritdoc />
79+
/// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
80+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
81+
=> SendCoreAsync(request, useAsync: true, cancellationToken);
82+
83+
#if NET5_0_OR_GREATER
84+
/// <inheritdoc />
85+
/// <remarks>Logs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
86+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
87+
=> SendCoreAsync(request, useAsync: false, cancellationToken).GetAwaiter().GetResult();
88+
#endif
89+
7490
// Used in tests
7591
internal static class Log
7692
{

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Linq;
56
using System.Net.Http;
67
using System.Threading;
@@ -90,6 +91,83 @@ public async Task LoggingScopeHttpMessageHandler_LogsAbsoluteUri()
9091
Assert.Equal("HTTP GET http://api.example.com/search?term=Western%20Australia", message.Scope.ToString());
9192
}
9293

94+
#if NET5_0_OR_GREATER
95+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))]
96+
public void LoggingHttpMessageHandler_LogsAbsoluteUri_Sync()
97+
{
98+
// Arrange
99+
var sink = new TestSink();
100+
101+
var serviceCollection = new ServiceCollection();
102+
serviceCollection.AddLogging();
103+
serviceCollection.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
104+
105+
serviceCollection
106+
.AddHttpClient("test")
107+
.ConfigurePrimaryHttpMessageHandler(() => new TestMessageHandler());
108+
109+
var services = serviceCollection.BuildServiceProvider();
110+
111+
var client = services.GetRequiredService<IHttpClientFactory>().CreateClient("test");
112+
113+
114+
// Act
115+
var request = new HttpRequestMessage(HttpMethod.Get, "http://api.example.com/search?term=Western%20Australia");
116+
117+
client.Send(request);
118+
119+
// Assert
120+
var messages = sink.Writes.ToArray();
121+
122+
var message = Assert.Single(messages.Where(m =>
123+
{
124+
return
125+
m.EventId == LoggingHttpMessageHandler.Log.EventIds.RequestStart &&
126+
m.LoggerName == "System.Net.Http.HttpClient.test.ClientHandler";
127+
}));
128+
129+
Assert.Equal("Sending HTTP request GET http://api.example.com/search?term=Western%20Australia", message.Message);
130+
}
131+
132+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNetCore))]
133+
public void LoggingScopeHttpMessageHandler_LogsAbsoluteUri_Sync()
134+
{
135+
// Arrange
136+
var sink = new TestSink();
137+
138+
var serviceCollection = new ServiceCollection();
139+
serviceCollection.AddLogging();
140+
serviceCollection.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
141+
142+
serviceCollection
143+
.AddHttpClient("test")
144+
.ConfigurePrimaryHttpMessageHandler(() => new TestMessageHandler());
145+
146+
var services = serviceCollection.BuildServiceProvider();
147+
148+
var client = services.GetRequiredService<IHttpClientFactory>().CreateClient("test");
149+
150+
151+
// Act
152+
var request = new HttpRequestMessage(HttpMethod.Get, "http://api.example.com/search?term=Western%20Australia");
153+
154+
client.Send(request);
155+
156+
// Assert
157+
var messages = sink.Writes.ToArray();
158+
159+
var message = Assert.Single(messages.Where(m =>
160+
{
161+
return
162+
m.EventId == LoggingScopeHttpMessageHandler.Log.EventIds.PipelineStart &&
163+
m.LoggerName == "System.Net.Http.HttpClient.test.LogicalHandler";
164+
}));
165+
166+
Assert.Equal("Start processing HTTP request GET http://api.example.com/search?term=Western%20Australia", message.Message);
167+
Assert.Equal("HTTP GET http://api.example.com/search?term=Western%20Australia", message.Scope.ToString());
168+
}
169+
#endif
170+
93171
private class TestMessageHandler : HttpClientHandler
94172
{
95173
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
@@ -98,6 +176,10 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
98176

99177
return Task.FromResult(response);
100178
}
179+
180+
#if NET5_0_OR_GREATER
181+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) => new();
182+
#endif
101183
}
102184
}
103185
}

0 commit comments

Comments
 (0)