|
@@ -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
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|