-
Notifications
You must be signed in to change notification settings - Fork 313
/
HttpLogger.cs
46 lines (38 loc) · 1.34 KB
/
HttpLogger.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
// Copyright (c) Microsoft. All rights reserved.
/// <summary>
/// Logging handler you might want to use to
/// see the HTTP traffic sent by SK to LLMs.
/// </summary>
public class HttpLogger : DelegatingHandler
{
public static HttpClient GetHttpClient(bool log = false)
{
return log
? new HttpClient(new HttpLogger(new HttpClientHandler()))
: new HttpClient();
}
public HttpLogger(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
}
Console.WriteLine();
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
}
Console.WriteLine();
return response;
}
}