Parcourir la source

日志过滤器

田爽 il y a 1 an
Parent
commit
948b76037d

+ 3 - 1
src/Hotline.Api/Controllers/BaseController.cs

@@ -1,10 +1,12 @@
-using Microsoft.AspNetCore.Mvc;
+using Hotline.Api.Filter;
+using Microsoft.AspNetCore.Mvc;
 
 namespace Hotline.Api.Controllers;
 
 [ApiController]
 [Produces("application/json")]
 [Route("api/v1/[controller]")]
+//[ServiceFilter(typeof(LogFilterAttribute))]
 public class BaseController : ControllerBase
 {
 }

+ 10 - 8
src/Hotline.Api/Controllers/OrderController.cs

@@ -43,6 +43,7 @@ using Hotline.Push.FWMessage;
 using Hotline.Share.Dtos.Push;
 using Hotline.Share.Enums.Push;
 using XF.Domain.Cache;
+using Hotline.Api.Filter;
 
 namespace Hotline.Api.Controllers;
 
@@ -194,17 +195,16 @@ public class OrderController : BaseController
         _enterpriseService = enterpriseService;
         _pushDomainService = pushDomainService;
         _cacheResponse = cacheResponse;
-
 	}
 
-    #region 工单发布
+	#region 工单发布
 
-    /// <summary>
-    /// 查询(工单发布)
-    /// </summary>
-    /// <param name="dto"></param>
-    /// <returns></returns>
-    [HttpGet("publish")]
+	/// <summary>
+	/// 查询(工单发布)
+	/// </summary>
+	/// <param name="dto"></param>
+	/// <returns></returns>
+	[HttpGet("publish")]
     public async Task<PagedDto<PublishDto>> PublishOrderList([FromQuery] QueryOrderPublishDto dto)
     {
         var (total, items) = await _orderRepository.Queryable()
@@ -3186,6 +3186,8 @@ public class OrderController : BaseController
     /// <param name="dto"></param>
     /// <returns></returns>
     [HttpGet("enterprise/list")]
+    //[ServiceFilter(typeof(LogFilterAttribute))]
+    //[LogFilterAttribute("查询企业列表")]
     public async Task<EnterpriseListData> GetEnterpriseList([FromQuery] PagedKeywordRequest dto)
     {
         return await _enterpriseService.GetEnterpriseList(dto.Keyword!, dto.PageIndex, dto.PageSize, HttpContext.RequestAborted, _cacheResponse);

+ 93 - 2
src/Hotline.Api/Filter/LogFilterAttribute.cs

@@ -1,6 +1,97 @@
-namespace Hotline.Api.Filter
+using Hotline.Settings;
+using Hotline.Tools;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+using System.Text;
+using Newtonsoft.Json;
+using Hotline.KnowledgeBase;
+using XF.Domain.Repository;
+using Serilog;
+using Hotline.Api.Controllers;
+using Microsoft.Data.SqlClient;
+
+namespace Hotline.Api.Filter
 {
-	public class LogFilterAttribute
+	public class LogFilterAttribute : ActionFilterAttribute
 	{
+		private readonly IRepository<SystemLog>? _systemLogRepository;
+		public LogFilterAttribute(IRepository<SystemLog>? systemLogRepository)
+		{
+			_systemLogRepository = systemLogRepository;
+		}
+
+		public LogFilterAttribute(string name = "", bool record = true)
+		{
+			this.name = name;
+			this.record = record;
+		}
+
+		public string name { get; set; }
+
+		public bool record { get; set; }
+
+
+		public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) 
+		{
+			//进入方法
+			var resultContext =  await next();
+			if (record)
+			{
+				#region 记录日志
+				SystemLog log = new SystemLog();
+				log.Name = name;
+				var Method = "";
+				switch (context.HttpContext.Request.Method.ToUpper())
+				{
+					case "GET":
+						Method = "GET";
+						log.ExecuteParam = context.HttpContext.Request.QueryString.Value;
+						break;
+
+					case "POST":
+						Method = "POST";
+						if (context.ActionArguments?.Count > 0)
+						{
+							log.ExecuteUrl += context.HttpContext.Request.QueryString.Value;
+							log.ExecuteParam = JsonConvert.SerializeObject(context.ActionArguments);
+						}
+						else
+						{
+							log.ExecuteParam = context.HttpContext.Request.QueryString.Value;
+						}
+						break;
+				}
+				if (resultContext.Exception != null)
+				{
+					StringBuilder sbException = new StringBuilder();
+					Exception exception = resultContext.Exception;
+					sbException.AppendLine(exception.Message);
+					while (exception.InnerException != null)
+					{
+						sbException.AppendLine(exception.InnerException.Message);
+						exception = exception.InnerException;
+					}
+					sbException.AppendLine(LogTool.GetSubString(resultContext.Exception.StackTrace, 4000));
+
+
+					log.ExecuteResult = sbException.ToString();
+					log.Status = 0;
+				}
+				else
+				{
+					log.Status = 1;
+					log.ExecuteUrl = Method + "|" + context.HttpContext.Request.Path;
+					ObjectResult result = context.Result as ObjectResult;
+					if (result != null)
+					{
+						log.ExecuteResult = JsonConvert.SerializeObject(result.Value);
+					}
+				}
+				//resultContext.
+				await _systemLogRepository.AddAsync(log);
+
+				#endregion
+			}
+		}
 	}
 }

+ 2 - 0
src/Hotline.Api/StartupExtensions.cs

@@ -25,6 +25,7 @@ using System.Runtime.CompilerServices;
 using Hotline.Ai.Jths;
 using Hotline.Api.Sdk;
 using Hotline.YbEnterprise.Sdk;
+using Hotline.Api.Filter;
 
 namespace Hotline.Api;
 
@@ -136,6 +137,7 @@ internal static class StartupExtensions
         services.AddSingleton<IAuthorizationPolicyProvider, AuthorizationPolicyProvider>();
         services.AddSingleton<IAuthorizationHandler, PermissionHandler>();
 
+        //services.AddScoped<LogFilterAttribute>();
         return builder.Build();
     }
 

+ 43 - 0
src/Hotline/Settings/SystemLog.cs

@@ -0,0 +1,43 @@
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Repository;
+
+namespace Hotline.Settings
+{
+	[Description("操作日志")]
+	public class SystemLog : FullStateEntity
+	{
+		/// <summary>
+		/// 操作名称
+		/// </summary>
+		public string Name { get; set; }	
+
+		/// <summary>
+		/// 执行状态(0失败 1成功) 
+		///</summary>
+		public int? Status { get; set; }
+		/// <summary>
+		/// 备注 
+		///</summary>
+		public string? Remark { get; set; }
+		/// <summary>
+		/// 接口地址 
+		///</summary>
+		public string? ExecuteUrl { get; set; }
+		/// <summary>
+		/// 请求参数 
+		///</summary>
+		[SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
+		public string? ExecuteParam { get; set; }
+		/// <summary>
+		/// 请求结果 
+		///</summary>
+		[SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
+		public string? ExecuteResult { get; set; }
+	}
+}

+ 36 - 0
src/Hotline/Tools/LogTool.cs

@@ -0,0 +1,36 @@
+using Hotline.Settings;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hotline.Tools
+{
+	public class LogTool
+	{
+		/// <summary>
+		/// 截取指定长度的字符串
+		/// </summary>
+		/// <param name="value"></param>
+		/// <param name="length"></param>
+		/// <param name="ellipsis"></param>
+		/// <returns></returns>
+		public static string GetSubString(string value, int length, bool ellipsis = false)
+		{
+			if (string.IsNullOrEmpty(value))
+			{
+				return value;
+			}
+			if (value.Length > length)
+			{
+				value = value.Substring(0, length);
+				if (ellipsis)
+				{
+					value += "...";
+				}
+			}
+			return value;
+		}
+	}
+}