1. 程式人生 > >Polly公共處理 -重試(Retry)

Polly公共處理 -重試(Retry)

summary ilog .exe try HR 結果 domain png exceptio

封裝處理下Polly重試

private ILogger<PollyHelper> _logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public PollyHelper(ILogger<PollyHelper> logger)
        {
            _logger = logger;
        }
        /// <summary>
/// /// </summary> /// <param name="exception"></param> /// <param name="action"></param> public void PollyRetry<T>(Action action) where T : Exception { var policy = Policy .Handle<T>() .Retry(
2, (ex, count) => { _logger.LogError("執行失敗!重試次數 {0}\r\n異常來自 {1}", count, ex.GetType().Name); }); policy.Execute(action); }

調用重試

 _pollyHelper.PollyRetry<UserServicesDomainException>(() =>
            {
                
throw new UserServicesDomainException("斯大林附近打開了房間"); });

執行結果如下

技術分享圖片

Polly公共處理 -重試(Retry)