| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.LogHelper; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | |
| | | namespace WIDESEA_Core.Middlewares |
| | | { |
| | |
| | | //todo |
| | | public async Task InvokeAsync(HttpContext context) |
| | | { |
| | | //if (AppSettings.app("Middleware", "RequestResponseLog", "Enabled").ObjToBool()) |
| | | if (App.ExpDateTime != null && (DateTime.Now - App.ExpDateTime.GetValueOrDefault()).TotalSeconds > 0) |
| | | { |
| | | context.Response.StatusCode = HttpStatusCode.InternalServerError.ObjToInt(); |
| | | context.Response.ContentType = "application/json"; |
| | | |
| | | var json = new WebResponseContent(); |
| | | |
| | | json.Message = HttpStatusCode.InternalServerError.ToString();//éè¯¯ä¿¡æ¯ |
| | | json.Code = 500;//500å¼å¸¸ |
| | | |
| | | StreamWriter streamWriter = new StreamWriter(context.Response.Body); |
| | | await streamWriter.WriteAsync(json.Serialize()); |
| | | return; |
| | | } |
| | | |
| | | // è¿æ»¤ï¼åªææ¥å£ |
| | | if (context.Request.Path.Value.Contains("api")) |
| | | if (context.Request.Path.Value?.Contains("api") ?? false) |
| | | { |
| | | context.Request.EnableBuffering(); |
| | | //Stream originalBody = context.Response.Body; |
| | | Stream originalBody = context.Response.Body; |
| | | string requestParam = string.Empty; |
| | | string responseParam = string.Empty; |
| | | try |
| | | { |
| | | string? apiIgnore = AppSettings.GetValue("ApiLogIgnore")?.ToString(); |
| | | string[] ignoreUrls = !string.IsNullOrEmpty(apiIgnore) ? apiIgnore.Split(",") : new string[] { "get" }; |
| | | |
| | | (context.RequestServices.GetService(typeof(RequestLogModel)) as RequestLogModel).RequestDate = DateTime.Now; |
| | | try |
| | | { |
| | | // åå¨è¯·æ±æ°æ® |
| | | //string requestParam = GetRequestData(context); |
| | | //DateTime beginDate = DateTime.Now; |
| | | |
| | | //using var ms = new MemoryStream(); |
| | | //context.Response.Body = ms; |
| | | requestParam = RequestDataLog(context); |
| | | context.Request.Body.Position = 0; |
| | | } |
| | | catch { } |
| | | using MemoryStream ms = new(); |
| | | context.Response.Body = ms; |
| | | |
| | | await _next(context); |
| | | |
| | | try |
| | | { |
| | | // åå¨ååºæ°æ® |
| | | //DateTime endDate = DateTime.Now; |
| | | //string responseParam = GetResponsetData(context); |
| | | responseParam = ResponseDataLog(context.Response); |
| | | } |
| | | catch { } |
| | | |
| | | //context.Response.Body.Position = 0; |
| | | //await context.Response.Body.CopyToAsync(originalBody); |
| | | ms.Position = 0; |
| | | await ms.CopyToAsync(originalBody); |
| | | |
| | | //Logger.WriteApiLog2DB(context,requestParam, beginDate, responseParam, endDate, context.Response.StatusCode == 200 ? LoggerStatus.Success : LoggerStatus.Error); |
| | | if (!ignoreUrls.Any(x => context.Request.Path.Value?.ToLower().Contains(x.ToLower()) ?? false)) |
| | | { |
| | | Logger.Add(requestParam, responseParam); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // è®°å½å¼å¸¸ |
| | | _logger.LogError(ex.Message + "" + ex.InnerException); |
| | | |
| | | } |
| | | finally |
| | | { |
| | | //context.Response.Body = originalBody; |
| | | context.Response.Body = originalBody; |
| | | } |
| | | } |
| | | else |
| | |
| | | await _next(context); |
| | | } |
| | | } |
| | | //else |
| | | //{ |
| | | // await _next(context); |
| | | //} |
| | | private string RequestDataLog(HttpContext context) |
| | | { |
| | | var request = context.Request; |
| | | var sr = new StreamReader(request.Body); |
| | | |
| | | object obj = new |
| | | { |
| | | QueryString = request.QueryString.ToString(), |
| | | BodyData = sr.ReadToEndAsync().Result |
| | | }; |
| | | |
| | | string data = JsonConvert.SerializeObject(obj); |
| | | |
| | | request.Body.Position = 0; |
| | | |
| | | return data; |
| | | } |
| | | |
| | | private string GetRequestData(HttpContext context) |
| | | private string ResponseDataLog(HttpResponse response) |
| | | { |
| | | 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}"; |
| | | } |
| | | response.Body.Position = 0; |
| | | using StreamReader stream = new StreamReader(response.Body, leaveOpen: true); |
| | | string body = stream.ReadToEnd(); |
| | | response.Body.Position = 0; |
| | | return body; |
| | | } |
| | | } |
| | | } |