using NLog; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public static class Retry { public static async Task Do( Action action, TimeSpan retryInterval, Logger logger, int maxAttemptCount = 3) { await Do(() => { action(); return null; }, retryInterval, logger, maxAttemptCount); } public static async Task Do( Func> action, TimeSpan retryInterval, Logger logger, int maxAttemptCount = 3) { var exceptions = new List(); for (int attempted = 0; attempted < maxAttemptCount; attempted++) { try { if (attempted > 0) { Thread.Sleep(retryInterval); } return await action(); } catch (Exception ex) { exceptions.Add(ex); logger.Error(ex); logger.Debug("Retry No " + (attempted + 1)); } } throw new AggregateException(exceptions); } }