Skip to content

Commit 7532cc5

Browse files
committed
add missing overrides in LoggingHttpMessageHandler and LoggingScopeHttpMessageHandler (dotnet#85104)
1 parent abaad8f commit 7532cc5

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ async Task<HttpResponseMessage> Core(HttpRequestMessage request, CancellationTok
6969
}
7070
}
7171

72+
/// <inheritdoc />
73+
/// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
74+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
75+
{
76+
ThrowHelper.ThrowIfNull(request);
77+
78+
var shouldRedactHeaderValue = _options?.ShouldRedactHeaderValue ?? _shouldNotRedactHeaderValue;
79+
80+
// Not using a scope here because we always expect this to be at the end of the pipeline, thus there's
81+
// not really anything to surround.
82+
Log.RequestStart(_logger, request, shouldRedactHeaderValue);
83+
var stopwatch = ValueStopwatch.StartNew();
84+
var response = base.Send(request, cancellationToken);
85+
Log.RequestEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
86+
87+
return response;
88+
}
89+
7290
// Used in tests.
7391
internal static class Log
7492
{

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ async Task<HttpResponseMessage> Core(HttpRequestMessage request, CancellationTok
7171
}
7272
}
7373

74+
/// <inheritdoc />
75+
/// <remarks>Loggs the request to and response from the sent <see cref="HttpRequestMessage"/>.</remarks>
76+
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
77+
{
78+
ThrowHelper.ThrowIfNull(request);
79+
80+
var stopwatch = ValueStopwatch.StartNew();
81+
82+
var shouldRedactHeaderValue = _options?.ShouldRedactHeaderValue ?? _shouldNotRedactHeaderValue;
83+
84+
using (Log.BeginRequestPipelineScope(_logger, request))
85+
{
86+
Log.RequestPipelineStart(_logger, request, shouldRedactHeaderValue);
87+
var response = base.Send(request, cancellationToken);
88+
Log.RequestPipelineEnd(_logger, response, stopwatch.GetElapsedTime(), shouldRedactHeaderValue);
89+
90+
return response;
91+
}
92+
}
93+
7494
// Used in tests
7595
internal static class Log
7696
{

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// 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.Linq;
54
using System.Net.Http;
65
using System.Threading;
76
using System.Threading.Tasks;
@@ -90,6 +89,81 @@ public async Task LoggingScopeHttpMessageHandler_LogsAbsoluteUri()
9089
Assert.Equal("HTTP GET http://api.example.com/search?term=Western%20Australia", message.Scope.ToString());
9190
}
9291

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

0 commit comments

Comments
 (0)