| | 1 | | using Serilog; |
| | 2 | |
|
| | 3 | | namespace EF.Blockchain.Server.Middleware; |
| | 4 | |
|
| | 5 | | public class RequestLoggingMiddleware |
| | 6 | | { |
| | 7 | | private readonly RequestDelegate _next; |
| | 8 | |
|
| 224 | 9 | | public RequestLoggingMiddleware(RequestDelegate next) => _next = next; |
| | 10 | |
|
| | 11 | | public async Task Invoke(HttpContext context) |
| 88 | 12 | | { |
| 88 | 13 | | var method = context.Request.Method; |
| 88 | 14 | | var path = context.Request.Path; |
| | 15 | |
|
| 88 | 16 | | context.Request.EnableBuffering(); |
| 88 | 17 | | string body = ""; |
| | 18 | |
|
| 88 | 19 | | if (context.Request.ContentLength > 0) |
| 32 | 20 | | { |
| 32 | 21 | | context.Request.Body.Position = 0; |
| 32 | 22 | | using var reader = new StreamReader(context.Request.Body, leaveOpen: true); |
| 32 | 23 | | body = await reader.ReadToEndAsync(); |
| 32 | 24 | | context.Request.Body.Position = 0; |
| 32 | 25 | | } |
| | 26 | |
|
| 88 | 27 | | var start = DateTime.Now; |
| 88 | 28 | | await _next(context); |
| 88 | 29 | | var duration = DateTime.Now - start; |
| | 30 | |
|
| 88 | 31 | | Log.Information("{Method} {Path} => {StatusCode} ({Duration} ms) | Body: {Body}", |
| 88 | 32 | | method, path, context.Response.StatusCode, duration.TotalMilliseconds, body); |
| 88 | 33 | | } |
| | 34 | | } |