46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using AspectInjector.Broker;
|
|
using DispenserCommon.Utils;
|
|
using Serilog;
|
|
|
|
namespace DispenserCommon.Aop;
|
|
|
|
/// <summary>
|
|
/// 用于捕获方法执行过程中的异常,并弹窗
|
|
/// </summary>
|
|
[Aspect(Scope.Global)]
|
|
[Injection(typeof(AlertWhenException))]
|
|
public class AlertWhenException(string title = "异常提示", string contentTitle = "") : Attribute
|
|
{
|
|
public AlertWhenException() : this("异常提示")
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过AOP实现方法执行前后的时间消耗
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="args"></param>
|
|
/// <param name="hostType"></param>
|
|
/// <param name="target"></param>
|
|
/// <param name="triggers"></param>
|
|
/// <returns></returns>
|
|
[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<object[], object> 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!;
|
|
}
|
|
}
|
|
} |