1、前言
上一篇,我們講了審計日志,重點是重點業務表的審計字段。還有一種系統審計,就是重點業務對象的改動記錄,是以審計日志表中的記錄形式存在的。這種審計記錄一般需要精確定位到某個終結點,最合適的實現方式就是操作過濾器。
2、實現
自定義操作過濾器:
public class LogAttribute : ActionFilterAttribute { public string LogName { get; private set; } public LogAttribute(string logName) { this.LogName = logName; } public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { await base.OnActionExecutionAsync(context, next); var user = context.HttpContext.RequestServices.GetService<CurrentUser>(); var log = new SysOperationLogDto { ClassName = context.Controller.ToString(), CreateTime = DateTime.Now, LogName = this.LogName, LogType = "業務日志", Method = ((Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName, Message = JsonSerializer.Serialize(context.ActionArguments), Succeed = "成功", UserId = user.ID }; var logService = context.HttpContext.RequestServices.GetService<ILogService>(); await logService.AppendOperationLog(log); } }
基本思路很簡單,調用控制器終結點方法時,記錄操作日志,操作日志中主要包含控制器名稱,方法名稱,調用入參等信息。