public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            //try
            //{
            //    var policy = Policy.Handle<System.Exception>()
            //   .WaitAndRetryAsync(_retryCount,
            //       retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
            //       (ex, time, count, ctx) =>
            //       {
            //           Logger.LogWarning(ex, $"Handle Event [{context.GetType()}] Error. The Error Message is {ex.ToString()}, retryCount:{_retryCount},count:{count}");
            //           if (_rollbackBeforeRetry)
            //           {
            //               DbContext.RollbackAsync().Wait(); //异常后回滚事务.再重试
            //           }
            //       });

            //    await policy.ExecuteAsync(() =>
            //    {
            //        return next(context);
            //    });
            //}
            //catch (System.Exception ex)
            //{
            //    if (_rollbackBeforeRetry)
            //    {
            //        await DbContext.RollbackAsync(); //异常后回滚事务

            //    }
            //    if (_throwException)
            //    {
            //        throw;
            //    }
            //    else
            //    {
            //        Logger.LogError(ex, $"Handle Event [{context.GetType()}] Error. The Final Error Message is {ex.ToString()}, retryCount:{_retryCount}");

            //    }
            //}

            //这个抛异常会产生问题,这个要等作者修复。
            //https://github.com/dotnetcore/AspectCore-Framework/blob/master/extras/sample/AspectCore.Extensions.Autofac.WebSample/Startup.cs
            await next(context).ContinueWith(async task =>
            {
                if (task.IsCompleted && !task.IsFaulted)
                {
                    Logger.LogInformation("success");
                }
                else
                {
                    Logger.LogError(task.Exception, $"Handle Event [{context.GetType()}] Error. The Final Error Message is {task.Exception.ToString()}, retryCount:{_retryCount}");
                }
            });
        }
示例#2
0
        /// <summary>
        /// 自定义缓存的key
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        protected string CustomCacheKey(AspectContext context)
        {
            var typeName        = context.GetType().Name;
            var methodName      = context.ImplementationMethod.Name;
            var methodArguments = context.ImplementationMethod.GetParameters();//获取参数列表,最多三个

            string key = $"{typeName}:{methodName}:";

            foreach (var param in methodArguments)
            {
                key = $"{key}{param}:";
            }

            return(key.TrimEnd(':'));
        }