|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using GeekLearning.D64; |
| 9 | +using System.Diagnostics; |
| 10 | + |
| 11 | +namespace GeekLearning.Http.Logging |
| 12 | +{ |
| 13 | + public class HttpRequestLogger : DelegatingHandler |
| 14 | + { |
| 15 | + private ILogger logger; |
| 16 | + private HttpRequestLoggerOptions options; |
| 17 | + TimebasedId timebaseId; |
| 18 | + |
| 19 | + public HttpRequestLogger(HttpMessageHandler innerHandler, ILogger logger) : this(innerHandler, logger, new HttpRequestLoggerOptions()) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public HttpRequestLogger(HttpMessageHandler innerHandler, ILogger logger, HttpRequestLoggerOptions options) : base(innerHandler) |
| 24 | + { |
| 25 | + this.timebaseId = new TimebasedId(false); |
| 26 | + this.logger = logger; |
| 27 | + this.options = options; |
| 28 | + } |
| 29 | + |
| 30 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 31 | + { |
| 32 | + var correlationId = timebaseId.NewId(); |
| 33 | + |
| 34 | + if (request.Content != null) |
| 35 | + { |
| 36 | + await request.Content.LoadIntoBufferAsync(); |
| 37 | + var requestBody = await request.Content.ReadAsStringAsync(); |
| 38 | + this.logger.LogInformation( |
| 39 | + "`{0}` to `{1}` with correlationId `{2}`:\n{3}\n\n{4}", |
| 40 | + request.Method, |
| 41 | + request.RequestUri, |
| 42 | + correlationId, |
| 43 | + string.Join("\n", request.Headers.Select(h => $"{h.Key}: {string.Join(" ", h.Value)}")), |
| 44 | + TruncateMessageIfTooBig(requestBody)); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + this.logger.LogInformation( |
| 49 | + "`{0}` to `{1}` with correlationId `{2}`:\n{3}", |
| 50 | + request.Method, |
| 51 | + request.RequestUri, |
| 52 | + correlationId, |
| 53 | + string.Join("\n", request.Headers.Select(h => $"{h.Key}: {string.Join(" ", h.Value)}"))); |
| 54 | + } |
| 55 | + Stopwatch stopwatch = null; |
| 56 | + if (this.options.MeasureRequestTime) |
| 57 | + { |
| 58 | + stopwatch = new Stopwatch(); |
| 59 | + stopwatch.Start(); |
| 60 | + } |
| 61 | + var response = await base.SendAsync(request, cancellationToken); |
| 62 | + await response.Content.LoadIntoBufferAsync(); |
| 63 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 64 | + |
| 65 | + if (stopwatch != null) |
| 66 | + { |
| 67 | + stopwatch.Stop(); |
| 68 | + this.logger.LogInformation( |
| 69 | + "`REQUEST` `{0}` ran for `{1}` ms", |
| 70 | + correlationId, |
| 71 | + stopwatch.Elapsed.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + this.logger.LogInformation( |
| 76 | + "`RECEIVE` `{0}` `{1}` with correlationId `{2}`:\n{3}\n\n{4}", |
| 77 | + (int)response.StatusCode, |
| 78 | + response.ReasonPhrase, |
| 79 | + correlationId, |
| 80 | + string.Join("\n", response.Headers.Select(h => $"{h.Key}: {string.Join(" ", h.Value)}")), |
| 81 | + TruncateMessageIfTooBig(responseBody)); |
| 82 | + |
| 83 | + return response; |
| 84 | + } |
| 85 | + |
| 86 | + private string TruncateMessageIfTooBig(string body) |
| 87 | + { |
| 88 | + if (body.Length > options.MaxSize) |
| 89 | + { |
| 90 | + return body.Substring(0, options.MaxSize / 2) + "<< TRUNCATED IN LOGS >>" + body.Substring(body.Length - options.MaxSize / 2); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + return body; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + public class HttpRequestLogger<TInnerHandler> : HttpRequestLogger |
| 101 | + where TInnerHandler : HttpMessageHandler, new() |
| 102 | + { |
| 103 | + public HttpRequestLogger(ILogger logger) : base(new TInnerHandler(), logger) |
| 104 | + { |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments