public async Task RunAsync() { var messageRepository = _container.GetInstance<IMessageRepository>(); while (true) { _cancellationToken.ThrowIfCancellationRequested(); var message = await messageRepository.GetMessageToDeliverAsync(); if (message != null) { try { await DeliverMessageAsync(message); } catch (Exception ex) { var logEvent = new LogEvent() { EventType = LogEventType.Application, LogLevel = LogLevel.Error, Message = ex.Message, Protocol = "SMTPD", }; _log.LogException(logEvent, ex); } continue; } await Task.Delay(TimeSpan.FromSeconds(5), _cancellationToken); } }
private static LogEventInfo CreateNlogEventInfo(LogEvent logEvent) { var logEventInfo = new LogEventInfo(); switch (logEvent.LogLevel) { case LogLevel.Info: logEventInfo.Level = NLog.LogLevel.Info; break; case LogLevel.Warning: logEventInfo.Level = NLog.LogLevel.Warn; break; case LogLevel.Error: logEventInfo.Level = NLog.LogLevel.Error; break; case LogLevel.Debug: logEventInfo.Level = NLog.LogLevel.Debug; break; default: throw new NotImplementedException(string.Format("Unsupported log level: {0}", logEvent.LogLevel)); } logEventInfo.Message = logEvent.Message; return logEventInfo; }
public void LogException(LogEvent logEvent, Exception exception) { var logEventInfo = CreateNlogEventInfo(logEvent); logEventInfo.Exception = exception; Logger.Log(logEventInfo); }
public void LogInfo(LogEvent logEvent) { var logEventInfo = CreateNlogEventInfo(logEvent); Logger.Log(logEventInfo); }
public void LogException(LogEvent logEvent, Exception exception) { LogEntries.Add(new Tuple<LogEvent, Exception>(logEvent, exception)); }
public void LogInfo(LogEvent logEvent) { LogEntries.Add(new Tuple<LogEvent, Exception>(logEvent, null)); }
public void LogException(LogEvent @event, Exception exception) { }
public void LogInfo(LogEvent @event) { }
private async Task DeliverMessageAsync(Message message) { var accountRepository = _container.GetInstance<IAccountRepository>(); var messageRepository = _container.GetInstance<IMessageRepository>(); var folderRepository = _container.GetInstance<IFolderRepository>(); var dnsClient = _container.GetInstance<IDnsClient>(); message.NumberOfDeliveryAttempts++; bool isLastAttempt = message.NumberOfDeliveryAttempts >= 3; var deliveryResults = new List<DeliveryResult>(); try { var remainingRecipients = new List<Recipient>(message.Recipients); var localDelivery = new LocalDelivery(accountRepository, messageRepository, folderRepository, _log); deliveryResults.AddRange(await localDelivery.DeliverAsync(message, remainingRecipients.Where(recipient => recipient.AccountId != 0).ToList())); var externalDelivery = new ExternalDelivery(messageRepository, dnsClient, _log); deliveryResults.AddRange(await externalDelivery.DeliverAsync(message, remainingRecipients.Where(recipient => recipient.AccountId == 0).ToList())); var failedRecipients = deliveryResults.Where(result => result.ReplyCodeSeverity == ReplyCodeSeverity.PermanentNegative || (isLastAttempt && result.ReplyCodeSeverity == ReplyCodeSeverity.TransientNegative)); await SubmitBounceMessageAsync(message, failedRecipients); var deliveryCompleted = deliveryResults.Any(result => result.ReplyCodeSeverity == ReplyCodeSeverity.TransientNegative); if (isLastAttempt || !deliveryCompleted) { await messageRepository.DeleteAsync(message); } } catch (Exception ex) { var logEvent = new LogEvent() { EventType = LogEventType.Application, LogLevel = LogLevel.Error, Protocol = "SMTPD", }; if (isLastAttempt) logEvent.Message = "Failed delivering message due to an error. Giving up."; else logEvent.Message = "Failed delivering message due to an error. Will retry later."; _log.LogException(logEvent, ex); if (isLastAttempt) { await messageRepository.DeleteAsync(message); } else { await messageRepository.UpdateAsync(message); } } }