/
LoggingHandler.cs
134 lines (121 loc) · 4.7 KB
/
LoggingHandler.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace HttpLogging
{
public class LoggingHandler : DelegatingHandler
{
Utf8JsonWriter jsonWriter;
public LoggingHandler(HttpMessageHandler innerHandler, Stream logStream)
: base(innerHandler)
{
this.jsonWriter = new Utf8JsonWriter(logStream, new JsonWriterOptions { Indented = true });
this.jsonWriter.WriteStartObject();
this.jsonWriter.WriteStartObject("log");
this.jsonWriter.WriteString("version", "1.2");
this.jsonWriter.WriteStartObject("creator");
this.jsonWriter.WriteString("name", "LoggingHandler");
this.jsonWriter.WriteString("version", "1.0");
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteStartArray("entries");
}
protected override void Dispose(bool disposing)
{
if (disposing && this.jsonWriter != null)
{
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteEndObject();
this.jsonWriter.Dispose();
this.jsonWriter = null;
}
base.Dispose(disposing);
}
private static void WriteHeaders(Utf8JsonWriter jsonWriter, HttpHeaders httpHeaders)
{
foreach (var header in httpHeaders)
{
foreach (var headerValue in header.Value)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("name", header.Key);
jsonWriter.WriteString("value", headerValue);
jsonWriter.WriteEndObject();
}
}
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.jsonWriter.WriteStartObject();
this.jsonWriter.WriteString("startedDateTime", DateTime.UtcNow);
this.jsonWriter.WriteStartObject("request");
this.jsonWriter.WriteString("method", request.Method.Method);
this.jsonWriter.WriteString("url", request.RequestUri.OriginalString);
this.jsonWriter.WriteString("httpVersion", "HTTP/" + request.Version.ToString(2));
this.jsonWriter.WriteStartArray("cookies");
// FIXME
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteStartArray("headers");
WriteHeaders(this.jsonWriter, request.Headers);
if (request.Content != null)
WriteHeaders(this.jsonWriter, request.Content.Headers);
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteStartArray("queryString");
// FIXME
this.jsonWriter.WriteEndArray();
if (request.Content != null)
{
this.jsonWriter.WriteStartObject("postData");
this.jsonWriter.WriteString("mimeType", request.Content.Headers.ContentType.ToString());
this.jsonWriter.WriteStartArray("params");
// FIXME
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteString("text", await request.Content.ReadAsStringAsync());
this.jsonWriter.WriteEndObject();
}
this.jsonWriter.WriteNumber("headersSize", 0); // FIXME
this.jsonWriter.WriteNumber("bodySize", 0); // FIXME
this.jsonWriter.WriteEndObject();
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
this.jsonWriter.WriteStartObject("response");
this.jsonWriter.WriteNumber("status", (int)response.StatusCode);
this.jsonWriter.WriteString("statusText", response.ReasonPhrase);
this.jsonWriter.WriteString("httpVersion", "HTTP/" + response.Version.ToString(2));
this.jsonWriter.WriteStartArray("cookies");
// TODO
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteStartArray("headers");
WriteHeaders(this.jsonWriter, response.Headers);
if (response.Content != null)
WriteHeaders(this.jsonWriter, response.Content.Headers);
this.jsonWriter.WriteEndArray();
this.jsonWriter.WriteStartObject("content");
if (response.Content != null)
{
this.jsonWriter.WriteNumber("size", 0); // FIXME
this.jsonWriter.WriteNumber("compression", 0); // FIXME
this.jsonWriter.WriteString("mimeType", response.Content.Headers.ContentType.ToString());
this.jsonWriter.WriteString("text", await response.Content.ReadAsStringAsync());
}
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteString("redirectURL", response.Headers.Location?.OriginalString ?? "");
this.jsonWriter.WriteNumber("headersSize", 0); // FIXME
this.jsonWriter.WriteNumber("bodySize", 0); // FIXME
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteStartObject("cache");
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteStartObject("timings");
this.jsonWriter.WriteNumber("send", 0); // FIXME
this.jsonWriter.WriteNumber("wait", 0); // FIXME
this.jsonWriter.WriteNumber("receive", 0); // FIXME
this.jsonWriter.WriteEndObject();
this.jsonWriter.WriteNumber("time", 0);
this.jsonWriter.WriteEndObject();
return response;
}
}
}