瀏覽代碼

修复日志记录时,不能准确记录入参

qinchaoyue 3 月之前
父節點
當前提交
efb1a21a9e

+ 1 - 1
src/Hotline.Api/Controllers/OrderController.cs

@@ -3693,7 +3693,7 @@ public class OrderController : BaseController
     /// <param name="dto"></param>
     /// <returns></returns>
     [HttpPost]
-    [LogFilter("新增工单")]
+    [LogFilterAlpha("新增工单")]
     public async Task<object> Add([FromBody] AddOrderDto dto)
     {
         dto.InitAddress();

+ 39 - 12
src/Hotline.Api/Filter/LogFilterAlphaAttribute.cs

@@ -1,6 +1,10 @@
 using Hotline.Settings;
+using Hotline.Share.Tools;
+using Hotline.Tools;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.Filters;
+using Newtonsoft.Json;
+using System.Text;
 
 namespace Hotline.Api.Filter;
 
@@ -34,13 +38,32 @@ public class LogFilterAlphaAttribute : ActionFilterAttribute
                 Name = this.Name,
             };
             GetMethod(context, systemLog);
-            var systemId = await repository.AddAsync(systemLog);
 
             var resultContext = await next();
 
-            var result = context.Result as ObjectResult;
-            if (result != null)
-                await repository.UpdateResultAsync(systemId, result);
+            if (resultContext.Exception != null)
+            {
+                StringBuilder sbException = new StringBuilder();
+                Exception exception = resultContext.Exception;
+                sbException.AppendLine(exception.Message);
+                if (exception.InnerException != null)
+                    sbException.AppendLine(exception.InnerException.Message);
+
+                sbException.AppendLine(LogTool.GetSubString(resultContext.Exception.StackTrace, 4000));
+                systemLog.ExecuteResult = sbException.ToString();
+                systemLog.Status = 0;
+            }
+            else
+            {
+                systemLog.Status = 1;
+                ObjectResult result = context.Result as ObjectResult;
+                if (result != null)
+                {
+                    systemLog.ExecuteResult = JsonConvert.SerializeObject(result.Value);
+                }
+            }
+
+            await repository.AddAsyncNoException(systemLog);
         }
     }
 
@@ -49,16 +72,20 @@ public class LogFilterAlphaAttribute : ActionFilterAttribute
         var request = context.HttpContext.Request;
         try
         {
-            systemLog.ExecuteUrl = request.Method.ToUpper() + "|" + request.Path;
-            if (request.Method.ToUpper() == "GET")
+            if (request != null)
             {
-                systemLog.ExecuteParam = request.QueryString.ToString();
-            }
-            else
-            {
-                if (request.ContentType?.IndexOf("multipart/form-data", StringComparison.Ordinal) == -1)
+                systemLog.ExecuteUrl = request.Method.ToUpper() + "|" + request.Path;
+                if (request.Method.ToUpper() == "GET")
+                {
+                    systemLog.ExecuteParam = request.QueryString.ToString();
+                }
+                else
                 {
-                    systemLog.ExecuteParam = context.ActionArguments.Values.FirstOrDefault();
+                    if (request.ContentType?.IndexOf("multipart/form-data", StringComparison.Ordinal) == -1 && context.ActionArguments.Values.FirstOrDefault() != null)
+                    {
+                        systemLog.ExecuteParam = context.ActionArguments.Values.FirstOrDefault();
+                        systemLog.ExecuteParam = systemLog.ExecuteParam.ToJson().FromJson<dynamic>();
+                    }
                 }
             }
         }

+ 12 - 0
src/Hotline.Repository.SqlSugar/System/SystemLogRepository.cs

@@ -71,6 +71,18 @@ public class SystemLogRepository : BaseRepository<SystemLog>, ISystemLogReposito
         }
     }
 
+    public async Task AddAsyncNoException(SystemLog entity)
+    {
+        try
+        {
+            await AddAsync(entity);
+        }
+        catch
+        {
+            // ignore
+        }
+    }
+
     public async Task UpdateResultAsync(string id, ObjectResult result)
     {
         try

+ 2 - 0
src/Hotline/Settings/ISystemLogRepository.cs

@@ -24,4 +24,6 @@ public interface ISystemLogRepository : IRepository<SystemLog>
     void Add(string name, object executeParam = null, string remark = "", [CallerMemberName]string executeUrl = "", int status = 0, string ipUrl = "", string executeResult = "");
 
     Task UpdateResultAsync(string id, ObjectResult result);
+
+    Task AddAsyncNoException(SystemLog entity);
 }