From fe596f9db05103917c9257348edcbd3ecb5b46e8 Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期四, 16 四月 2026 19:12:38 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/RuiShengZhiNeng/GaoPuLiTiKu

---
 代码管理/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Filter/Log/ApiLogMiddlewareNew.cs |  144 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 144 insertions(+), 0 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Filter/Log/ApiLogMiddlewareNew.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Filter/Log/ApiLogMiddlewareNew.cs"
new file mode 100644
index 0000000..21d3de3
--- /dev/null
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Filter/Log/ApiLogMiddlewareNew.cs"
@@ -0,0 +1,144 @@
+锘縰sing Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Reflection;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.LogHelper;
+
+namespace WIDESEAWCS_Core.Middlewares
+{
+    /// <summary>
+    /// 璁板綍璇锋眰鍜屽搷搴旀暟鎹�
+    /// </summary>
+    public class ApiLogMiddlewareNew
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        private readonly RequestDelegate _next;
+
+        public ApiLogMiddlewareNew(RequestDelegate next, ILogger<ApiLogMiddlewareNew> logger)
+        {
+            _next = next;
+        }
+
+        //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";
+
+                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") ?? false)
+            {
+                context.Request.EnableBuffering();
+                Stream originalBody = context.Response.Body;
+                string requestParam = string.Empty;
+                string responseParam = string.Empty;
+                try
+                {
+                    (context.RequestServices.GetService(typeof(RequestLogModel)) as RequestLogModel).RequestDate = DateTime.Now;
+                    try
+                    {
+                        // 瀛樺偍璇锋眰鏁版嵁
+                        requestParam = RequestDataLog(context);
+                        context.Request.Body.Position = 0;
+                    }
+                    catch { }
+                    using MemoryStream ms = new();
+                    context.Response.Body = ms;
+
+                    await _next(context);
+
+                    try
+                    {
+                        // 瀛樺偍鍝嶅簲鏁版嵁
+                        responseParam = ResponseDataLog(context.Response);
+                    }
+                    catch { }
+
+                    ms.Position = 0;
+                    await ms.CopyToAsync(originalBody);
+                    #region 杩囨护鎺ュ彛
+                    string path = context.Request.Path.Value;
+                    if (!string.IsNullOrEmpty(path))
+                    {
+                        List<string> RequestPaths = new List<string>();
+                        string RequestPath = AppSettings.Configuration[nameof(RequestPath)];
+                        if (!string.IsNullOrEmpty(RequestPath)) RequestPaths = RequestPath.Split(',').ToList();
+                        bool containsAny = RequestPaths.Any(k => path.Contains(k, StringComparison.OrdinalIgnoreCase));
+                        if (!containsAny) Logger.Add(requestParam, responseParam);//璁板綍绯荤粺鏃ュ織
+                        //璁板綍鎺ュ彛鏃ュ織
+                        var ignoreKeywords = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "get" };
+                        containsAny = RequestPaths.Where(k => !ignoreKeywords.Contains(k))
+                           .Any(k => path.Contains(k, StringComparison.OrdinalIgnoreCase));
+                        if (containsAny && context != null) LoggerNew.Add(requestParam, responseParam, context);
+                    }
+                    #endregion
+                    //if (!(context.Request.Path.Value?.Contains("get") ?? true))
+                    //    Logger.Add(requestParam, responseParam);
+                }
+                catch (Exception ex)
+                {
+                    // 璁板綍寮傚父
+
+                }
+                finally
+                {
+                    context.Response.Body = originalBody;
+                }
+            }
+            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 ResponseDataLog(HttpResponse response)
+        {
+            response.Body.Position = 0;
+            using StreamReader stream = new StreamReader(response.Body, leaveOpen: true);
+            string body = stream.ReadToEnd();
+            response.Body.Position = 0;
+            return body;
+        }
+    }
+}

--
Gitblit v1.9.3