public async Task SendEmailAsync(IList <string> recepients, string subject, string body, CancellationToken cancellationToken = default) { if (!recepients.Any()) { return; } using var smtpClient = BuildSmtpClient(); _logger.LogInfo($"Sending email to:"); foreach (var to in recepients) { try { _logger.LogInfo(to); var mailMessage = GenerateMailMessage(to, subject, body); if (_appSettings.SendEmails) { await smtpClient.SendMailAsync(mailMessage); } else { _logger.LogInfo($"Did not send email to {to} due to config settings"); } } catch (Exception ex) { _logger.LogError($"Failed to send email to: {to}"); _logger.LogError(ex.ToString()); continue; } } _logger.LogInfo("Emails processed!"); }
private void LogError(Task task) { foreach (var ex in task.Exception.Flatten().InnerExceptions) { _logger.LogError(ex.ToString()); } }
public CircuitBreak(int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, int reTries, Func <Exception, bool> exceptionPredicate) { logger = ObjectContainer.Resolve <Logging.ILogger>(); circuitBreakerPolicy = Policy .Handle(exceptionPredicate) .CircuitBreaker( exceptionsAllowedBeforeBreaking: exceptionsAllowedBeforeBreaking, durationOfBreak: durationOfBreak, onBreak: (ex, breakDelay) => { logger.LogError(".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!", ex); }, onReset: () => { logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again."); }, onHalfOpen: () => { logger.LogDebug(".Breaker logging: Half-open; next call is a trial."); }); retryPolicy = Policy .Handle(exceptionPredicate) .Retry(reTries); }