/// <summary> /// 处理请求 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task InvokeAsync(HttpContext context) { // 过滤,只有接口 if (context.Request.Path.Value.Contains("api")) { //context.Request.EnableRewind(); context.Request.EnableBuffering(); Stream originalBody = context.Response.Body; try { // 存储请求数据 var reqMsg = await RequestDataLog(context.Request); using (var ms = new MemoryStream()) { context.Response.Body = ms; await _next(context); // 存储响应数据 var respMsg = await ResponseDataLog(context.Response, ms); _logHelper.ApiLog($"RequRespLogMildd:{reqMsg}{respMsg}"); ms.Position = 0; await ms.CopyToAsync(originalBody); } } catch (Exception ex) { // 记录异常 _logHelper.Error($"RequRespLogMildd:发生异常:{ex.Message}", ex); } finally { context.Response.Body = originalBody; } } else { await _next(context); } }