林豪 左 2 жил өмнө
parent
commit
ce90678617

+ 85 - 0
ServiceCenter/Attributes/LogAttribute .cs

@@ -0,0 +1,85 @@
+using Microsoft.AspNetCore.Mvc.Filters;
+using ServiceCenter.Logs;
+using System.Diagnostics;
+
+namespace ServiceCenter.Attributes
+{
+    public class LogAttribute : ActionFilterAttribute
+    {
+        private string LogFlag { get; set; }
+        private string ActionArguments { get; set; }
+
+        /// <summary>
+        /// 请求体中的所有值
+        /// </summary>
+        private string RequestBody { get; set; }
+
+        private Stopwatch Stopwatch { get; set; }
+
+        public LogAttribute(string logFlag)
+        {
+            LogFlag = logFlag;
+        }
+
+        public override void OnActionExecuting(ActionExecutingContext context)
+        {
+            base.OnActionExecuting(context);
+
+            // 后续添加了获取请求的请求体,如果在实际项目中不需要删除即可
+            long contentLen = context.HttpContext.Request.ContentLength == null ? 0 : context.HttpContext.Request.ContentLength.Value;
+            if (contentLen > 0)
+            {
+                // 读取请求体中所有内容
+                Stream stream = context.HttpContext.Request.Body;
+                if (context.HttpContext.Request.Method == "POST")
+                {
+                    stream.Position = 0;
+                }
+                byte[] buffer = new byte[contentLen];
+                stream.Read(buffer, 0, buffer.Length);
+                // 转化为字符串
+                RequestBody = System.Text.Encoding.UTF8.GetString(buffer);
+            }
+
+            ActionArguments = Newtonsoft.Json.JsonConvert.SerializeObject(context.ActionArguments);
+
+            Stopwatch = new Stopwatch();
+            Stopwatch.Start();
+        }
+
+        public override void OnActionExecuted(ActionExecutedContext context)
+        {
+            base.OnActionExecuted(context);
+            Stopwatch.Stop();
+
+            string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
+            string method = context.HttpContext.Request.Method;
+
+            string qs = ActionArguments;
+
+            dynamic result = context.Result.GetType().Name == "EmptyResult" ? new { Value = "无返回结果" } : context.Result as dynamic;
+
+            string res = "在返回结果前发生了异常";
+            try
+            {
+                if (result != null)
+                {
+                    res = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value);
+                }
+            }
+            catch (Exception)
+            {
+                res = "日志未获取到结果,返回的数据无法序列化";
+            }
+            var msg = $"\n 方法:{LogFlag} \n " +
+                    $"地址:{url} \n " +
+                    $"方式:{method} \n " +
+                    $"请求体:{RequestBody} \n " +
+                    $"参数:{qs}\n " +
+                    $"结果:{res}\n " +
+                    $"耗时:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒(指控制器内对应方法执行完毕的时间)";
+
+            LogHub.InterfacePublish("测试", msg);
+        }
+    }
+}

+ 4 - 4
WCS.WorkEngineering/WebApi/Controllers/AgvController.cs

@@ -1,6 +1,7 @@
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
 using PlcSiemens.Core.Extension;
+using ServiceCenter.Attributes;
 using ServiceCenter.Logs;
 using ServiceCenter.SqlSugars;
 using WCS.Core;
@@ -61,11 +62,11 @@ namespace WCS.WorkEngineering.WebApi.Controllers
         }
 
         /// <summary>
-        ///  背负式agv请求出库任务
+        ///  背负式AGV请求出库任务
         /// </summary>
         /// <param name="reqDto">请求参数</param>
         /// <returns></returns>
-        [HttpPost]
+        [HttpPost, Log("背负式AGV请求出库任务")]
         public ApplyEmptySpoolResponse ApplyEmptySpool([FromBody] AgvFillEmptySpaceRequest reqDto)
         {
             lock (LockHub.ApplyEmptySpoolLock)
@@ -127,7 +128,6 @@ namespace WCS.WorkEngineering.WebApi.Controllers
                             Status = AGVTaskStatus.NewBuild,
                             Station = dev.Entity.Code,
                             AddWho = "WCS"
-
                         };
                         //创建对应的AGV任务
                         db.Default.Insertable(agv).SplitTable().ExecuteCommand();
@@ -157,7 +157,7 @@ namespace WCS.WorkEngineering.WebApi.Controllers
         /// </summary>
         /// <param name="reqDto"></param>
         /// <returns></returns>
-        [HttpPost]
+        [HttpPost, Log("AGV执行回调")]
         public AgvCallbackResponse AgvCallback([FromBody] AgvCallbackRequest reqDto)
         {
             lock (LockHub.AgvCallbackLock)