using System.Reflection; using AspectInjector.Broker; using DispenserCommon.Utils; using Serilog; namespace DispenserCommon.Aop; [Aspect(Scope.Global)] [AttributeUsage(AttributeTargets.Class)] [Injection(typeof(GlobalTry), Inherited = true)] public class GlobalTry : Attribute { /// /// 通过AOP实现方法执行前后的时间消耗 /// /// /// /// /// /// [Advice(Kind.Around, Targets = Target.Method)] public object? HandleMethod([Argument(Source.Name)] string name, [Argument(Source.Arguments)] object[] args, [Argument(Source.Target)] Func target, [Argument(Source.Metadata)] MethodBase method) { try { var result = target(args); return result; } catch (Exception e) { Log.Error(e, $"方法 {name} 执行异常: {e.Message}"); ToastUtil.Error("操作异常: " + e.Message); return default; } } }