| | |
| | | /// |
| | | /// </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 (App.ExpDateTime != null && (DateTime.Now - App.ExpDateTime.GetValueOrDefault()).TotalSeconds > 0) |
| | | { |
| | | context.Response.StatusCode = HttpStatusCode.InternalServerError.ObjToInt(); |
| | | context.Response.ContentType = "application/json"; |
| | | //if (App.ExpDateTime != null) |
| | | //{ |
| | | // if ((DateTime.Now - App.ExpDateTime.GetValueOrDefault()).TotalSeconds > 0) |
| | | // { |
| | | // await ReturnExpiredResponse(context, "ç³»ç»å·²è¿æï¼è¯·è系管çå"); |
| | | // return; |
| | | // } |
| | | // //var Hours = (App.ExpDateTime.GetValueOrDefault() - DateTime.Now).TotalHours; |
| | | // //if (Hours < 72) |
| | | // //{ |
| | | // // // è®°å½è¦åæ¥å¿ |
| | | // // _logger.LogWarning($"ç³»ç»å³å°å°æï¼å©ä½æ¶é´ï¼{Hours:F2}å°æ¶ï¼å°ææ¶é´ï¼{App.ExpDateTime.GetValueOrDefault():yyyy-MM-dd HH:mm:ss}"); |
| | | |
| | | var json = new WebResponseContent(); |
| | | // // // å¨ååºå¤´ä¸æ·»å å°ææç¤º |
| | | // // context.Response.Headers.Add("X-Expiration-Warning", |
| | | // // $"ç³»ç»å°å¨ {Hours:F2} å°æ¶åå°æ ({App.ExpDateTime.GetValueOrDefault():yyyy-MM-dd HH:mm:ss})"); |
| | | |
| | | json.Message = HttpStatusCode.InternalServerError.ToString();//éè¯¯ä¿¡æ¯ |
| | | json.Code = 500;//500å¼å¸¸ |
| | | // // // 妿éè¦ç´æ¥è¿åæç¤ºä¿¡æ¯ï¼åæ¶ä¸é¢ç注é |
| | | // // await ReturnExpirationWarningResponse(context, Hours); |
| | | // // return; |
| | | // //} |
| | | |
| | | StreamWriter streamWriter = new StreamWriter(context.Response.Body); |
| | | await streamWriter.WriteAsync(json.Serialize()); |
| | | return; |
| | | } |
| | | //} |
| | | |
| | | // è¿æ»¤ï¼åªææ¥å£ |
| | | if (context.Request.Path.Value?.Contains("api") ?? false) |
| | | { |
| | | if (App.ExpDateTime != null) |
| | | { |
| | | if ((DateTime.Now - App.ExpDateTime.GetValueOrDefault()).TotalSeconds > 0 && !context.Request.Path.Value.Contains("getVierificationCode") && context.Request.Path.Value != "/api/User/login") |
| | | { |
| | | await ReturnExpiredResponse(context, "ç³»ç»å·²è¿æï¼è¯·è系管çå"); |
| | | return; |
| | | } |
| | | } |
| | | context.Request.EnableBuffering(); |
| | | Stream originalBody = context.Response.Body; |
| | | string requestParam = string.Empty; |
| | |
| | | |
| | | ms.Position = 0; |
| | | await ms.CopyToAsync(originalBody); |
| | | if (!((context.Request.Path.Value?.Contains("get")) ?? true) && !((context.Request.Path.Value?.Contains("Get")) ?? true) && !((context.Request.Path.Value?.Contains("query")) ?? true) && !((context.Request.Path.Value?.Contains("Query")) ?? true) && !((context.Request.Path.Value?.Contains("DownLoadApp")) ?? true) && !((context.Request.Path.Value?.Contains("downLoadApp")) ?? true) && !((context.Request.Path.Value?.Contains("UploadApp")) ?? true) && !((context.Request.Path.Value?.Contains("uploadApp")) ?? true)) |
| | | if (!ShouldSkipLogging(context.Request.Path.Value)) |
| | | Logger.Add(requestParam, responseParam); |
| | | } |
| | | catch (Exception ex) |
| | |
| | | { |
| | | await _next(context); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è¿åè¿æååº |
| | | /// </summary> |
| | | private async Task ReturnExpiredResponse(HttpContext context, string message) |
| | | { |
| | | context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; |
| | | context.Response.ContentType = "application/json; charset=utf-8"; |
| | | context.Response.Headers.ContentType= "application/json; charset=utf-8"; |
| | | var json = new WebResponseContent |
| | | { |
| | | Message = message, |
| | | Code = 500 |
| | | }; |
| | | |
| | | var jsonString = JsonConvert.SerializeObject(json); |
| | | await context.Response.WriteAsync(jsonString); |
| | | } |
| | | /// <summary> |
| | | /// è¿åå°æè¦åååºï¼å¯éï¼ |
| | | /// </summary> |
| | | private async Task ReturnExpirationWarningResponse(HttpContext context, double hoursRemaining) |
| | | { |
| | | context.Response.StatusCode = (int)HttpStatusCode.OK; |
| | | context.Response.ContentType = "application/json"; |
| | | |
| | | var expirationDate = App.ExpDateTime.GetValueOrDefault(); |
| | | var response = new |
| | | { |
| | | Code = 200, |
| | | Message = "ç³»ç»å³å°å°æè¦å", |
| | | Data = new |
| | | { |
| | | HoursRemaining = hoursRemaining, |
| | | ExpirationDate = expirationDate, |
| | | FormattedMessage = $"ç³»ç»å°å¨ {Math.Ceiling(hoursRemaining)} å°æ¶åå°æï¼å°ææ¶é´ï¼{expirationDate:yyyy-MM-dd HH:mm:ss}ã请è系管çåç»æã" |
| | | }, |
| | | Warning = true |
| | | }; |
| | | |
| | | var jsonString = JsonConvert.SerializeObject(response); |
| | | await context.Response.WriteAsync(jsonString); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 夿æ¯å¦è·³è¿æ¥å¿è®°å½ |
| | | /// </summary> |
| | | private bool ShouldSkipLogging(string path) |
| | | { |
| | | if (string.IsNullOrEmpty(path)) return false; |
| | | |
| | | var skipKeywords = new[] { "get", "Get", "query", "Query", "DownLoadApp", "downLoadApp", "UploadApp", "uploadApp" }; |
| | | return skipKeywords.Any(keyword => path.Contains(keyword, StringComparison.OrdinalIgnoreCase)); |
| | | } |
| | | |
| | | private string RequestDataLog(HttpContext context) |
| | |
| | | |
| | | request.Body.Position = 0; |
| | | |
| | | return data; ; |
| | | return data; |
| | | } |
| | | |
| | | private string ResponseDataLog(HttpResponse response) |