-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingHandler.cs
50 lines (45 loc) · 1.67 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace IFX.CostManagement
{
// LoggingHandler derived from http://stackoverflow.com/questions/12300458/web-api-audit-logging
public class LoggingHandler : DelegatingHandler
{
ILogger _log;
public LoggingHandler(HttpMessageHandler innerHandler, ILogger log)
: base(innerHandler)
{
_log = log;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
_log.LogInformation("Request: ");
_log.LogInformation("{0} {1}", request.Method, request.RequestUri);
foreach (var header in request.Headers)
{
var headerString = string.Join(" ",header.Value);
_log.LogInformation("{0} : {1}", header.Key, headerString);
}
if (request.Content != null)
{
_log.LogInformation(await request.Content.ReadAsStringAsync());
}
_log.LogInformation("");
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
_log.LogInformation("Response: ");
_log.LogInformation("{0} {1}", (int)response.StatusCode, response.ReasonPhrase);
if (response.Content != null)
{
_log.LogInformation(await response.Content.ReadAsStringAsync());
}
_log.LogInformation("");
return response;
}
}
}