-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHttpLoggingHandler.cs
102 lines (80 loc) · 3.73 KB
/
HttpLoggingHandler.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Linq;
namespace SuperSimpleConductor.ConductorClient
{
public class HttpLoggingHandler : DelegatingHandler
{
public HttpLoggingHandler(HttpMessageHandler innerHandler = null)
: base(innerHandler ?? new HttpClientHandler())
{
}
async protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var req = request;
var id = Guid.NewGuid().ToString();
var msg = $"[{id} - Request]";
Debug.WriteLine($"{msg}========Start==========");
Debug.WriteLine($"{msg} {req.Method} {req.RequestUri.PathAndQuery} {req.RequestUri.Scheme}/{req.Version}");
Debug.WriteLine($"{msg} Host: {req.RequestUri.Scheme}://{req.RequestUri.Host}");
foreach (var header in req.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (req.Content != null)
{
foreach (var header in req.Content.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (req.Content is StringContent || IsTextBasedContentType(req.Headers) ||
this.IsTextBasedContentType(req.Content.Headers))
{
var result = await req.Content.ReadAsStringAsync();
Debug.WriteLine($"{msg} Content:");
Debug.WriteLine($"{msg} {result}");
}
}
var start = DateTime.Now;
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
var end = DateTime.Now;
Debug.WriteLine($"{msg} Duration: {end - start}");
Debug.WriteLine($"{msg}==========End==========");
msg = $"[{id} - Response]";
Debug.WriteLine($"{msg}=========Start=========");
var resp = response;
Debug.WriteLine(
$"{msg} {req.RequestUri.Scheme.ToUpper()}/{resp.Version} {(int) resp.StatusCode} {resp.ReasonPhrase}");
foreach (var header in resp.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content != null)
{
foreach (var header in resp.Content.Headers)
Debug.WriteLine($"{msg} {header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content is StringContent || this.IsTextBasedContentType(resp.Headers) ||
this.IsTextBasedContentType(resp.Content.Headers))
{
start = DateTime.Now;
var result = await resp.Content.ReadAsStringAsync();
end = DateTime.Now;
Debug.WriteLine($"{msg} Content:");
Debug.WriteLine($"{msg} {result}");
Debug.WriteLine($"{msg} Duration: {end - start}");
}
}
Debug.WriteLine($"{msg}==========End==========");
return response;
}
readonly string[] types = new[] {"html", "text", "xml", "json", "txt", "x-www-form-urlencoded"};
bool IsTextBasedContentType(HttpHeaders headers)
{
IEnumerable<string> values;
if (!headers.TryGetValues("Content-Type", out values))
return false;
var header = string.Join(" ", values).ToLowerInvariant();
return types.Any(t => header.Contains(t));
}
}
}