using AspectInjector.Broker; using DispenserCommon.DTO; using DispenserCommon.Enums; using DispenserCommon.Events; using DispenserCommon.Utils; using DispenserUI.Exceptions; using Serilog; namespace DispenserCommon.Aop; [Aspect(Scope.Global)] [Injection(typeof(Operation))] public class Operation(string name) : Attribute { public string Name { get; set; } = name; public Operation() : this("") { Name = ""; } /// /// 通过AOP实现方法执行过程拦截 /// /// /// /// /// [Advice(Kind.Around, Targets = Target.Method)] public object? Around( [Argument(Source.Arguments)] object[] args, [Argument(Source.Target)] Func target, [Argument(Source.Triggers)] Attribute[] triggers) { var trigger = (Operation)triggers[0]; var log = new ActionLog { Name = trigger.Name, Params = args }; try { Log.Information($"正在执行: {trigger.Name}"); return target(args); } catch (Exception e) { if (e is BizException { Level: ExceptionLevel.NORMAL }) { ToastUtil.Error($"{trigger.Name}操作失败: {e.Message}"); } log.Exception = e; Log.Error($"{trigger.Name}操作失败: {e.Message}"); return default; } finally { EventBus.Publish(EventType.OperationLog, log); } } }