| using Microsoft.AspNetCore.Http; | 
| using Microsoft.Extensions.Logging; | 
| using Newtonsoft.Json; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_Core.Helper; | 
| using WIDESEA_Core.LogHelper; | 
|   | 
| namespace WIDESEA_Core.Middlewares | 
| { | 
|     /// <summary> | 
|     /// 记录请求和响应数据 | 
|     /// </summary> | 
|     public class ApiLogMiddleware | 
|     { | 
|         /// <summary> | 
|         ///  | 
|         /// </summary> | 
|         private readonly RequestDelegate _next; | 
|         private readonly ILogger<ApiLogMiddleware> _logger; | 
|   | 
|         public ApiLogMiddleware(RequestDelegate next, ILogger<ApiLogMiddleware> logger) | 
|         { | 
|             _next = next; | 
|             _logger = logger; | 
|         } | 
|   | 
|         //todo | 
|         public async Task InvokeAsync(HttpContext context) | 
|         { | 
|             //if (AppSettings.app("Middleware", "RequestResponseLog", "Enabled").ObjToBool()) | 
|             { | 
|                 // 过滤,只有接口 | 
|                 if (context.Request.Path.Value.Contains("api")) | 
|                 { | 
|                     context.Request.EnableBuffering(); | 
|                     //Stream originalBody = context.Response.Body; | 
|   | 
|                     try | 
|                     { | 
|                         // 存储请求数据 | 
|                         //string requestParam = GetRequestData(context); | 
|                         //DateTime beginDate = DateTime.Now; | 
|   | 
|                         //using var ms = new MemoryStream(); | 
|                         //context.Response.Body = ms; | 
|   | 
|                         await _next(context); | 
|   | 
|                         // 存储响应数据 | 
|                         //DateTime endDate = DateTime.Now; | 
|                         //string responseParam = GetResponsetData(context); | 
|   | 
|                         //context.Response.Body.Position = 0; | 
|                         //await context.Response.Body.CopyToAsync(originalBody); | 
|   | 
|                         //Logger.WriteApiLog2DB(context,requestParam, beginDate, responseParam, endDate, context.Response.StatusCode == 200 ? LoggerStatus.Success : LoggerStatus.Error); | 
|                     } | 
|                     catch (Exception ex) | 
|                     { | 
|                         // 记录异常                         | 
|                         _logger.LogError(ex.Message + "" + ex.InnerException); | 
|                     } | 
|                     finally | 
|                     { | 
|                         //context.Response.Body = originalBody; | 
|                     } | 
|                 } | 
|                 //else | 
|                 //{ | 
|                 //    await _next(context); | 
|                 //} | 
|             } | 
|             //else | 
|             //{ | 
|             //    await _next(context); | 
|             //} | 
|         } | 
|   | 
|         private string GetRequestData(HttpContext context) | 
|         { | 
|             try | 
|             { | 
|                 using StreamReader sr = new StreamReader(context.Request.Body); | 
|                 string request = JsonConvert.SerializeObject(sr.ReadToEnd()); ; | 
|                 context.Request.Body.Position = 0; | 
|                 return request; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 return $"请求参数获取错误,{ex.Message}"; | 
|             } | 
|         } | 
|   | 
|         private string GetResponsetData(HttpContext context) | 
|         { | 
|             try | 
|             { | 
|                 using StreamReader sr = new StreamReader(context.Response.Body); | 
|                 string response = JsonConvert.SerializeObject(sr.ReadToEnd()); ; | 
|                 context.Response.Body.Position = 0; | 
|                 return response; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 return $"响应参数获取错误,{ex.Message}"; | 
|             } | 
|         } | 
|     } | 
| } |