コード例 #1
0
        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);
            }
        }
コード例 #2
0
ファイル: Log.cs プロジェクト: hmailserver/hmailserver-net
        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;
        }
コード例 #3
0
ファイル: Log.cs プロジェクト: hmailserver/hmailserver-net
 public void LogException(LogEvent logEvent, Exception exception)
 {
     var logEventInfo = CreateNlogEventInfo(logEvent);
     logEventInfo.Exception = exception;
     Logger.Log(logEventInfo);
 }
コード例 #4
0
ファイル: Log.cs プロジェクト: hmailserver/hmailserver-net
        public void LogInfo(LogEvent logEvent)
        {
            var logEventInfo = CreateNlogEventInfo(logEvent);

            Logger.Log(logEventInfo);
        }
コード例 #5
0
 public void LogException(LogEvent logEvent, Exception exception)
 {
     LogEntries.Add(new Tuple<LogEvent, Exception>(logEvent, exception));
 }
コード例 #6
0
 public void LogInfo(LogEvent logEvent)
 {
     LogEntries.Add(new Tuple<LogEvent, Exception>(logEvent, null));
 }
コード例 #7
0
        public void LogException(LogEvent @event, Exception exception)
        {

        }
コード例 #8
0
        public void LogInfo(LogEvent @event)
        {

        }
コード例 #9
0
        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);
                }

            }
        }