/// <summary>
 /// Deliver an error to the default recipient
 /// </summary>
 /// <param name="message"></param>
 private static void DefaultDelivery(MessageRoutingSystemError message)
 {
     var emailer = new EmailClient(null, null);
     emailer.Send(message);
 }
        /// <summary>
        /// Handle a MessageRoutingSystemError message
        /// </summary>
        /// <param name="message"></param>
        public void HandleMessage(MessageRoutingSystemError message)
        {
            PrintError(message);
            // Add the message to the database
            var messageId = _messageService.AddMessage(message.ApplicationName,
                                                      message.MachineName,
                                                      message.HelpText,
                                                      message.CommandLineArgs,
                                                      message.ExceptionType,
                                                      message.ExceptionMessage,
                                                      message.StackTrace,
                                                      message.InsertDate);

            var rules = _ruleService.GetRules().OrderBy(x => x.Order);
            bool messageMatchedToRule = false;

            foreach (var rule in rules)
            {
                var deliver = true;

                if (!IsMatch(rule, message)) continue;

                messageMatchedToRule = true;

                // check if rule is supposed to pool
                var isPool = rule.PoolMaxMessages != 0 || rule.PoolActiveTime != TimeSpan.Zero;
                if (isPool)
                {
                    deliver = PoolMessage(rule, messageId);
                }

                if (deliver)
                {
                    var emailer = new EmailClient(rule.Action, rule.CustomSubject);
                    var mailPriority = MailPriority.Normal;
                    if (rule.PriorityId != null)
                    {
                        var priority = _priorityService.GetPriorityById(rule.PriorityId.Value);
                        switch (priority.Name)
                        {
                            case "High":
                                mailPriority = MailPriority.High;
                                break;
                            case "Low":
                                mailPriority = MailPriority.Low;
                                break;
                            default:
                                mailPriority = MailPriority.Normal;
                                break;
                        }
                    }

                    emailer.Send(message, isPool, mailPriority);
                }

                // stop trying to match rules if continue processing is false
                if (!rule.ContinueProcessing)
                    break;
            }

            if (!messageMatchedToRule)
                DefaultDelivery(message);
        }