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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user