forked from JustDerb/Tiltify-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiltifyHttpClientHandler.cs
54 lines (48 loc) · 2.85 KB
/
TiltifyHttpClientHandler.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
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Tiltify
{
public class TiltifyHttpClientHandler : DelegatingHandler
{
private readonly ILogger<IHttpCallHandler> _logger;
public TiltifyHttpClientHandler(ILogger<IHttpCallHandler> logger) : base(new HttpClientHandler())
{
_logger = logger;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null)
_logger?.LogInformation("Timestamp: {timestamp} Type: {type} Method: {method} Resource: {url} Content: {content}",
DateTime.Now, "Request", request.Method.ToString(), request.RequestUri.ToString(), await request.Content.ReadAsStringAsync());
else
_logger?.LogInformation("Timestamp: {timestamp} Type: {type} Method: {method} Resource: {url}",
DateTime.Now, "Request", request.Method.ToString(), request.RequestUri.ToString());
var stopwatch = Stopwatch.StartNew();
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
stopwatch.Stop();
if (response.IsSuccessStatusCode)
{
if (response.Content != null)
_logger?.LogInformation("Timestamp: {timestamp} Type: {type} Resource: {url} Statuscode: {statuscode} Elapsed: {elapsed} ms Content: {content}",
DateTime.Now, "Response", response.RequestMessage.RequestUri, (int)response.StatusCode, stopwatch.ElapsedMilliseconds, await response.Content.ReadAsStringAsync());
else
_logger?.LogInformation("Timestamp: {timestamp} Type: {type} Resource: {url} Statuscode: {statuscode} Elapsed: {elapsed} ms",
DateTime.Now, "Response", response.RequestMessage.RequestUri, (int)response.StatusCode, stopwatch.ElapsedMilliseconds);
}
else
{
if (response.Content != null)
_logger?.LogError("Timestamp: {timestamp} Type: {type} Resource: {url} Statuscode: {statuscode} Elapsed: {elapsed} ms Content: {content}",
DateTime.Now, "Response", response.RequestMessage.RequestUri, (int)response.StatusCode, stopwatch.ElapsedMilliseconds, await response.Content.ReadAsStringAsync());
else
_logger?.LogError("Timestamp: {timestamp} Type: {type} Resource: {url} Statuscode: {statuscode} Elapsed: {elapsed} ms",
DateTime.Now, "Response", response.RequestMessage.RequestUri, (int)response.StatusCode, stopwatch.ElapsedMilliseconds);
}
return response;
}
}
}