From 01a2ba8ad626497ef239449845431f3c4ab24379 Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Sat, 10 Jun 2017 09:59:37 +0200 Subject: [PATCH] Added failureOnly setting to both email and Slack health check notifications - thanks to feedback at #cg17! --- .../BaseNotificationMethodElement.cs | 10 ++++++ .../config/HealthChecks.Release.config | 4 +-- .../Scheduling/HealthCheckNotifier.cs | 32 +++++++++++-------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Core/Configuration/HealthChecks/BaseNotificationMethodElement.cs b/src/Umbraco.Core/Configuration/HealthChecks/BaseNotificationMethodElement.cs index 3ef90c6471..b4c27e9d38 100644 --- a/src/Umbraco.Core/Configuration/HealthChecks/BaseNotificationMethodElement.cs +++ b/src/Umbraco.Core/Configuration/HealthChecks/BaseNotificationMethodElement.cs @@ -5,6 +5,7 @@ namespace Umbraco.Core.Configuration.HealthChecks public abstract class BaseNotificationMethodElement : ConfigurationElement { private const string VERBOSITY_KEY = "verbosity"; + private const string FAILUREONLY_KEY = "failureOnly"; [ConfigurationProperty(VERBOSITY_KEY, IsRequired = true)] public HealthCheckNotificationVerbosity Verbosity @@ -14,5 +15,14 @@ namespace Umbraco.Core.Configuration.HealthChecks return ((HealthCheckNotificationVerbosity)(base[VERBOSITY_KEY])); } } + + [ConfigurationProperty(FAILUREONLY_KEY, IsRequired = true)] + public bool FailureOnly + { + get + { + return ((bool)(base[FAILUREONLY_KEY])); + } + } } } diff --git a/src/Umbraco.Web.UI/config/HealthChecks.Release.config b/src/Umbraco.Web.UI/config/HealthChecks.Release.config index ab5a9d6ae6..bf637c6d01 100644 --- a/src/Umbraco.Web.UI/config/HealthChecks.Release.config +++ b/src/Umbraco.Web.UI/config/HealthChecks.Release.config @@ -10,8 +10,8 @@ https://our.umbraco.org/documentation/reference/config/umbracosettings/#web-rout - - + + diff --git a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs index 53affdb276..62dd98136c 100644 --- a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs +++ b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs @@ -69,26 +69,30 @@ namespace Umbraco.Web.Scheduling var results = new HealthCheckResults(checks); results.LogResults(); - // Send to email address if configured + // 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) + if (emailNotificationSettings != null && string.IsNullOrEmpty(emailNotificationSettings.RecipientEmail) == false + && (emailNotificationSettings.FailureOnly == false || emailNotificationSettings.FailureOnly && results.AllChecksSuccessful == false)) { - using (var client = new SmtpClient()) - using (var mailMessage = new MailMessage()) - { - mailMessage.To.Add(emailNotificationSettings.RecipientEmail); - mailMessage.Body = string.Format("

Results of the scheduled Umbraco Health Checks run on {0} at {1} are as follows:

{2}", - DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), results.ResultsAsHtml(emailNotificationSettings.Verbosity)); - mailMessage.Subject = emailNotificationSettings.Subject; - mailMessage.IsBodyHtml = true; + using (var client = new SmtpClient()) + using (var mailMessage = new MailMessage()) + { + mailMessage.To.Add(emailNotificationSettings.RecipientEmail); + mailMessage.Body = + string.Format( + "

Results of the scheduled Umbraco Health Checks run on {0} at {1} are as follows:

{2}", + DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), + results.ResultsAsHtml(emailNotificationSettings.Verbosity)); + mailMessage.Subject = emailNotificationSettings.Subject; + mailMessage.IsBodyHtml = true; - await client.SendMailAsync(mailMessage); - } + await client.SendMailAsync(mailMessage); + } } - // Send Slack incoming webhook if configured + // 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) + if (slackNotificationSettings != null && string.IsNullOrEmpty(slackNotificationSettings.WebHookUrl) == false && (slackNotificationSettings.FailureOnly == false || slackNotificationSettings.FailureOnly && results.AllChecksSuccessful == false)) { var slackClient = new SlackClient(slackNotificationSettings.WebHookUrl);