Amended registration of health check scheduled notifiers (email and Slack) to use a resolver method and allow others to be added without modifying core

This commit is contained in:
AndyButland
2017-06-18 16:41:46 +02:00
parent 01a2ba8ad6
commit 9f68bd4e52
21 changed files with 492 additions and 170 deletions

View File

@@ -69,47 +69,11 @@ namespace Umbraco.Web.Scheduling
var results = new HealthCheckResults(checks);
results.LogResults();
// Send to email address if configured observing if the configuration is set to only notify if there are any failures
var emailNotificationSettings = healthCheckConfig.NotificationSettings.EmailSettings;
if (emailNotificationSettings != null && string.IsNullOrEmpty(emailNotificationSettings.RecipientEmail) == false
&& (emailNotificationSettings.FailureOnly == false || emailNotificationSettings.FailureOnly && results.AllChecksSuccessful == false))
// Send using registered notification methods
var registeredNotificationMethods = HealthCheckNotificationMethodResolver.Current.NotificationMethods;
foreach (var notificationMethod in registeredNotificationMethods)
{
using (var client = new SmtpClient())
using (var mailMessage = new MailMessage())
{
mailMessage.To.Add(emailNotificationSettings.RecipientEmail);
mailMessage.Body =
string.Format(
"<html><body><p>Results of the scheduled Umbraco Health Checks run on {0} at {1} are as follows:</p>{2}</body></html>",
DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(),
results.ResultsAsHtml(emailNotificationSettings.Verbosity));
mailMessage.Subject = emailNotificationSettings.Subject;
mailMessage.IsBodyHtml = true;
await client.SendMailAsync(mailMessage);
}
}
// Send Slack incoming webhook if configured observing if the configuration is set to only notify if there are any failures
var slackNotificationSettings = healthCheckConfig.NotificationSettings.SlackSettings;
if (slackNotificationSettings != null && string.IsNullOrEmpty(slackNotificationSettings.WebHookUrl) == false && (slackNotificationSettings.FailureOnly == false || slackNotificationSettings.FailureOnly && results.AllChecksSuccessful == false))
{
var slackClient = new SlackClient(slackNotificationSettings.WebHookUrl);
var icon = Emoji.Warning;
if (results.AllChecksSuccessful)
{
icon = Emoji.WhiteCheckMark;
}
var slackMessage = new SlackMessage
{
Channel = slackNotificationSettings.Channel,
Text = results.ResultsAsMarkDown(slackNotificationSettings.Verbosity, true),
IconEmoji = icon,
Username = slackNotificationSettings.UserName
};
slackClient.Post(slackMessage);
await notificationMethod.SendAsync(results);
}
}