| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.Extensions.Logging; |
| | | using Newtonsoft.Json; |
| | | using Org.BouncyCastle.Asn1.Ocsp; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Text; |
| | | using System.Text.RegularExpressions; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.LogHelper; |
| | | |
| | |
| | | public class ApiLogMiddleware |
| | | { |
| | | /// <summary> |
| | | /// |
| | | /// |
| | | /// </summary> |
| | | private readonly RequestDelegate _next; |
| | | |
| | |
| | | var json = new WebResponseContent(); |
| | | |
| | | json.Message = HttpStatusCode.InternalServerError.ToString();//错误信息 |
| | | json.Code = 500;//500异常 |
| | | json.Code = 500;//500异常 |
| | | |
| | | StreamWriter streamWriter = new StreamWriter(context.Response.Body); |
| | | await streamWriter.WriteAsync(json.Serialize()); |
| | |
| | | try |
| | | { |
| | | // 存储请求数据 |
| | | requestParam = RequestDataLog(context); |
| | | requestParam = await RequestDataLogAsync(context); |
| | | context.Request.Body.Position = 0; |
| | | } |
| | | catch { } |
| | |
| | | try |
| | | { |
| | | // 存储响应数据 |
| | | responseParam = ResponseDataLog(context.Response); |
| | | responseParam = await ResponseDataLogAsync(context.Response); |
| | | } |
| | | catch { } |
| | | |
| | |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常 |
| | | |
| | | } |
| | | finally |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | private string RequestDataLog(HttpContext context) |
| | | private async Task<string> RequestDataLogAsync(HttpContext context) |
| | | { |
| | | var request = context.Request; |
| | | //request.EnableBuffering(); |
| | | //request.Body.Position = 0; |
| | | |
| | | var sr = new StreamReader(request.Body); |
| | | var sr = new StreamReader(request.Body, Encoding.UTF8); |
| | | |
| | | object obj; |
| | | string bodyData = sr.ReadToEndAsync().Result; |
| | | string bodyData = await sr.ReadToEndAsync(); |
| | | if (request.ContentLength <= 100000) |
| | | { |
| | | obj = new |
| | | { |
| | | QueryString = request.QueryString.ToString(), |
| | | BodyData = bodyData |
| | | BodyData = JsonConvert.DeserializeObject(bodyData) |
| | | //BodyData = bodyData |
| | | }; |
| | | } |
| | | else |
| | |
| | | return data; ; |
| | | } |
| | | |
| | | private string ResponseDataLog(HttpResponse response) |
| | | private async Task<string> ResponseDataLogAsync(HttpResponse response) |
| | | { |
| | | if (response.ContentLength <= 100000) |
| | | { |
| | | response.Body.Position = 0; |
| | | using StreamReader stream = new StreamReader(response.Body, leaveOpen: true); |
| | | string body = stream.ReadToEnd(); |
| | | using StreamReader stream = new StreamReader(response.Body, Encoding.UTF8, leaveOpen: true); |
| | | string body = await stream.ReadToEndAsync(); |
| | | response.Body.Position = 0; |
| | | return body; |
| | | } |
| | | return ""; |
| | | } |
| | | } |
| | | } |
| | | } |