/
EnhancedHttpClient.cs
55 lines (44 loc) · 2 KB
/
EnhancedHttpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.DotNet.Interactive.AspNetCore;
internal class EnhancedHttpClient
{
private const string _logKey = "aspnetcore-logs";
public static HttpClient Create(string address, InteractiveLoggerProvider interactiveLoggerProvider) =>
new(new LogCapturingHandler(interactiveLoggerProvider))
{
BaseAddress = new Uri(address)
};
private class LogCapturingHandler : DelegatingHandler
{
private readonly InteractiveLoggerProvider _interactiveLoggerProvider;
public LogCapturingHandler(InteractiveLoggerProvider interactiveLoggerProvider)
: base(new SocketsHttpHandler())
{
_interactiveLoggerProvider = interactiveLoggerProvider;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var logs = new ConcurrentQueue<LogMessage>();
request.Options.Set(new HttpRequestOptionsKey<ConcurrentQueue<LogMessage>>(_logKey), logs);
_interactiveLoggerProvider.Posted += logs.Enqueue;
try
{
var responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
// Wait to download the body so we catch all the logs. If someone wants to access the response before
// downloading the body, they're free to create a their own HttpClient.
await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return responseMessage;
}
finally
{
_interactiveLoggerProvider.Posted -= logs.Enqueue;
}
}
}
}