using AspectInjector.Broker; using DispenserCommon.Utils; using Serilog; namespace DispenserCommon.Aop; /// /// 用于捕获方法执行过程中的异常,并弹窗 /// [Aspect(Scope.Global)] [Injection(typeof(AlertWhenException))] public class AlertWhenException(string title = "异常提示", string contentTitle = "") : Attribute { public AlertWhenException() : this("异常提示") { } /// /// 通过AOP实现方法执行前后的时间消耗 /// /// /// /// /// /// /// [Advice(Kind.Around)] public object Around([Argument(Source.Name)] string name, [Argument(Source.Arguments)] object[] args, [Argument(Source.Type)] Type hostType, [Argument(Source.Target)] Func target, [Argument(Source.Triggers)] Attribute[] triggers) { try { var result = target(args); return result; } catch (Exception e) { Log.Error(e, $"方法 {name} 执行异常"); MessageBoxHelper.Error(e.Message, contentTitle, title, false); return null!; } } }