| | |
| | | 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> |
| | | /// |
| | | /// 不写入 Sys_Log 表的接口路径(只写文件日志) |
| | | /// </summary> |
| | | private static readonly string[] _apiLogSkipPaths = |
| | | { |
| | | "InOrOutCompleted", |
| | | "SendLocationStatus", |
| | | "RequestOutbound", |
| | | "GetPalletCodeCell", |
| | | "GetOutBoundTrayTask" |
| | | }; |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | private readonly RequestDelegate _next; |
| | | |
| | | public ApiLogMiddleware(RequestDelegate next, ILogger<ApiLogMiddleware> logger) |
| | | { |
| | | _next = next; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 判断当前请求路径是否在跳过数据库日志列表中 |
| | | /// </summary> |
| | | private static bool IsApiLogSkipPath(string? path) |
| | | { |
| | | if (string.IsNullOrEmpty(path)) return false; |
| | | return _apiLogSkipPaths.Any(skip => path.Contains(skip)); |
| | | } |
| | | |
| | | //todo |
| | |
| | | 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()); |
| | |
| | | ms.Position = 0; |
| | | await ms.CopyToAsync(originalBody); |
| | | |
| | | if (!ignoreUrls.Any(x => context.Request.Path.Value?.Contains(x) ?? false)) |
| | | if (!ignoreUrls.Any(x => context.Request.Path.Value?.Contains(x) ?? false) |
| | | && !IsApiLogSkipPath(context.Request.Path.Value)) |
| | | { |
| | | Logger.Add(requestParam, responseParam); |
| | | } |
| | |
| | | catch (Exception ex) |
| | | { |
| | | // 记录异常 |
| | | |
| | | } |
| | | finally |
| | | { |
| | |
| | | return ""; |
| | | } |
| | | } |
| | | } |
| | | } |