Added failureOnly setting to both email and Slack health check notifications - thanks to feedback at #cg17!

This commit is contained in:
Jeavon Leopold
2017-06-10 09:59:37 +02:00
parent 36a659f3da
commit 01a2ba8ad6
3 changed files with 30 additions and 16 deletions

View File

@@ -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]));
}
}
}
}

View File

@@ -10,8 +10,8 @@ https://our.umbraco.org/documentation/reference/config/umbracosettings/#web-rout
<!--<check id="1B5D221B-CE99-4193-97CB-5F3261EC73DF" disabledOn="" disabledBy="0" />-->
</disabledChecks>
<notificationSettings enabled="false" firstRunTime="" periodInHours="24" >
<!--<emailSettings recipientEmail="" subject="" verbosity="Summary" />-->
<!--<slackSettings webHookUrl="" channel="" username="" verbosity="Detailed" />-->
<!--<emailSettings recipientEmail="" subject="" verbosity="Summary" failureOnly="true" />-->
<!--<slackSettings webHookUrl="" channel="" username="" verbosity="Detailed" failureOnly="false" />-->
<disabledChecks>
<!--<check id="EB66BB3B-1BCD-4314-9531-9DA2C1D6D9A7" disabledOn="" disabledBy="0" />-->
</disabledChecks>

View File

@@ -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("<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;
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);
}
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);