Skip to content

Commit da52cac

Browse files
Add Http request and response trace logs 🔊
1 parent 22747d8 commit da52cac

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Notion.Client
7+
{
8+
public class LoggingHandler : DelegatingHandler
9+
{
10+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
11+
{
12+
Log.Trace("Request: {request}", request);
13+
14+
try
15+
{
16+
var response = await base.SendAsync(request, cancellationToken);
17+
18+
Log.Trace("Response: {response}", response);
19+
20+
return response;
21+
}
22+
catch (Exception ex)
23+
{
24+
Log.Error(ex, "Failed to get response: {exception}", ex);
25+
throw;
26+
}
27+
}
28+
}
29+
}

‎Src/Notion.Client/RestClient/RestClient.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ private HttpClient EnsureHttpClient()
153153
{
154154
if (_httpClient == null)
155155
{
156-
_httpClient = new HttpClient();
156+
var pipeline = new LoggingHandler() { InnerHandler = new HttpClientHandler() };
157+
_httpClient = new HttpClient(pipeline);
157158
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
158159
}
159160

0 commit comments

Comments
 (0)