From 966d1fe6077c885db064fcea98bb48cbccb464d6 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期一, 14 十月 2024 17:23:02 +0800
Subject: [PATCH] WCS

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/ApiLogMiddleware.cs |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 113 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/ApiLogMiddleware.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/ApiLogMiddleware.cs"
new file mode 100644
index 0000000..ee5aea4
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/ApiLogMiddleware.cs"
@@ -0,0 +1,113 @@
+锘縰sing Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.Helper;
+using WIDESEA_Core.LogHelper;
+
+namespace WIDESEA_Core.Middlewares
+{
+    /// <summary>
+    /// 璁板綍璇锋眰鍜屽搷搴旀暟鎹�
+    /// </summary>
+    public class ApiLogMiddleware
+    {
+        /// <summary>
+        /// 
+        /// </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 (AppSettings.app("Middleware", "RequestResponseLog", "Enabled").ObjToBool())
+            {
+                // 杩囨护锛屽彧鏈夋帴鍙�
+                if (context.Request.Path.Value.Contains("api"))
+                {
+                    context.Request.EnableBuffering();
+                    //Stream originalBody = context.Response.Body;
+
+                    try
+                    {
+                        // 瀛樺偍璇锋眰鏁版嵁
+                        //string requestParam = GetRequestData(context);
+                        //DateTime beginDate = DateTime.Now;
+
+                        //using var ms = new MemoryStream();
+                        //context.Response.Body = ms;
+
+                        await _next(context);
+
+                        // 瀛樺偍鍝嶅簲鏁版嵁
+                        //DateTime endDate = DateTime.Now;
+                        //string responseParam = GetResponsetData(context);
+
+                        //context.Response.Body.Position = 0;
+                        //await context.Response.Body.CopyToAsync(originalBody);
+
+                        //Logger.WriteApiLog2DB(context,requestParam, beginDate, responseParam, endDate, context.Response.StatusCode == 200 ? LoggerStatus.Success : LoggerStatus.Error);
+                    }
+                    catch (Exception ex)
+                    {
+                        // 璁板綍寮傚父                        
+                        _logger.LogError(ex.Message + "" + ex.InnerException);
+                    }
+                    finally
+                    {
+                        //context.Response.Body = originalBody;
+                    }
+                }
+                //else
+                //{
+                //    await _next(context);
+                //}
+            }
+            //else
+            //{
+            //    await _next(context);
+            //}
+        }
+
+        private string GetRequestData(HttpContext context)
+        {
+            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}";
+            }
+        }
+    }
+}

--
Gitblit v1.9.3