|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Security.Claims; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Blog.Core.AuthHelper.OverWrite; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using System.IO; |
| 10 | +using Blog.Core.Common.LogHelper; |
| 11 | +using StackExchange.Profiling; |
| 12 | + |
| 13 | +namespace Blog.Core.Middlewares |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// 中间件 |
| 17 | + /// 记录请求和响应数据 |
| 18 | + /// </summary> |
| 19 | + public class RequRespLogMildd |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// |
| 23 | + /// </summary> |
| 24 | + private readonly RequestDelegate _next; |
| 25 | + /// <summary> |
| 26 | + /// |
| 27 | + /// </summary> |
| 28 | + /// <param name="next"></param> |
| 29 | + public RequRespLogMildd(RequestDelegate next) |
| 30 | + { |
| 31 | + _next = next; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + public async Task InvokeAsync(HttpContext context) |
| 37 | + { |
| 38 | + // 过滤,只有接口 |
| 39 | + if (context.Request.Path.Value.Contains("api")) |
| 40 | + { |
| 41 | + context.Request.EnableBuffering(); |
| 42 | + Stream originalBody = context.Response.Body; |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + // 存储请求数据 |
| 47 | + RequestDataLog(context.Request); |
| 48 | + |
| 49 | + using (var ms = new MemoryStream()) |
| 50 | + { |
| 51 | + context.Response.Body = ms; |
| 52 | + |
| 53 | + await _next(context); |
| 54 | + |
| 55 | + // 存储响应数据 |
| 56 | + ResponseDataLog(context.Response, ms); |
| 57 | + |
| 58 | + ms.Position = 0; |
| 59 | + await ms.CopyToAsync(originalBody); |
| 60 | + } |
| 61 | + } |
| 62 | + catch (Exception ex) |
| 63 | + { |
| 64 | + // 记录异常 |
| 65 | + //ErrorLogData(context.Response, ex); |
| 66 | + } |
| 67 | + finally |
| 68 | + { |
| 69 | + context.Response.Body = originalBody; |
| 70 | + } |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + await _next(context); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void RequestDataLog(HttpRequest request) |
| 79 | + { |
| 80 | + var sr = new StreamReader(request.Body); |
| 81 | + |
| 82 | + var content = $" QueryData:{request.Path + request.QueryString}\r\n BodyData:{sr.ReadToEnd()}"; |
| 83 | + |
| 84 | + if (!string.IsNullOrEmpty(content)) |
| 85 | + { |
| 86 | + Parallel.For(0, 1, e => |
| 87 | + { |
| 88 | + LogLock log = new LogLock(); |
| 89 | + LogLock.OutSql2Log("RequestResponseLog", new string[] { "Request Data:", content }); |
| 90 | + |
| 91 | + }); |
| 92 | + |
| 93 | + request.Body.Position = 0; |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + private void ResponseDataLog(HttpResponse response, MemoryStream ms) |
| 100 | + { |
| 101 | + ms.Position = 0; |
| 102 | + var ResponseBody = new StreamReader(ms).ReadToEnd(); |
| 103 | + |
| 104 | + if (!string.IsNullOrEmpty(ResponseBody)) |
| 105 | + { |
| 106 | + Parallel.For(0, 1, e => |
| 107 | + { |
| 108 | + LogLock log = new LogLock(); |
| 109 | + LogLock.OutSql2Log("RequestResponseLog", new string[] { "Response Data:", ResponseBody }); |
| 110 | + |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments