From 0f7165dada06fa551b6437ccba08b0c595d358f5 Mon Sep 17 00:00:00 2001 From: AndyButland Date: Mon, 5 Jun 2017 11:45:42 +0200 Subject: [PATCH 1/3] Retrieved HTML fragment for health check notification email from results, minor reformatting --- src/Umbraco.Web/HealthCheck/HealthCheckResults.cs | 6 +++--- src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs index 5da4ffcca7..1a48ee1b5e 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs @@ -57,16 +57,16 @@ namespace Umbraco.Web.HealthCheck var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success); if (checkIsSuccess) { - sb.AppendFormat("{0}Checks for'{1}' all completed succesfully.{2}", newItem, checkName, Environment.NewLine); + sb.AppendFormat("{0}Checks for '{1}' all completed succesfully.{2}", newItem, checkName, Environment.NewLine); } else { - sb.AppendFormat("{0}Checks for'{1}' completed with errors.{2}", newItem, checkName, Environment.NewLine); + sb.AppendFormat("{0}Checks for '{1}' completed with errors.{2}", newItem, checkName, Environment.NewLine); } foreach (var checkResult in checkResults) { - sb.AppendFormat("\t{0}Result:'{1}' , Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); + sb.AppendFormat("\t{0}Result: '{1}' , Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); } } return sb.ToString(); diff --git a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs index f998e76287..23c43b54d3 100644 --- a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs +++ b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs @@ -64,7 +64,8 @@ namespace Umbraco.Web.Scheduling using (var client = new SmtpClient()) using (var mailMessage = new MailMessage()) { - mailMessage.Body = "Results"; // TODO - get from results + 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()); mailMessage.To.Add(healthCheckConfig.NotificationSettings.RecipientEmail); mailMessage.Subject = "Umbraco Scheduled HeathChecks Results"; mailMessage.IsBodyHtml = true; From bf194a819a0f8cb158379f621773d6cda7fced75 Mon Sep 17 00:00:00 2001 From: AndyButland Date: Mon, 5 Jun 2017 13:35:08 +0200 Subject: [PATCH 2/3] Added try/catch around scheduled checks so one failure doesn't block the rest. Applied some formatting to email HTML notification. --- .../HealthCheck/HealthCheckResults.cs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs index 1a48ee1b5e..1781a0a423 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs @@ -13,7 +13,26 @@ namespace Umbraco.Web.HealthCheck internal HealthCheckResults(IEnumerable checks) { - _results = checks.ToDictionary(t => t.Name, t => t.GetStatus()); + _results = checks.ToDictionary( + t => t.Name, + t => { + try + { + return t.GetStatus(); + } + catch (Exception ex) + { + LogHelper.Error(string.Format("Error running scheduled health check: {0}", t.Name), ex); + var message = string.Format("Health check failed with exception: {0}. See logs for details.", ex.Message); + return new List + { + new HealthCheckStatus(message) + { + ResultType = StatusResultType.Error + } + }; + } + }); } internal void LogResults() @@ -66,17 +85,34 @@ namespace Umbraco.Web.HealthCheck foreach (var checkResult in checkResults) { - sb.AppendFormat("\t{0}Result: '{1}' , Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); + sb.AppendFormat("\t{0}Result: '{1}', Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); } } + return sb.ToString(); } internal string ResultsAsHtml() { - Markdown mark = new Markdown(); - return mark.Transform(ResultsAsMarkDown()); + var mark = new Markdown(); + var html = mark.Transform(ResultsAsMarkDown()); + html = ApplyHtmlHighlighting(html); + return html; } + + private string ApplyHtmlHighlighting(string html) + { + html = ApplyHtmlHighlightingForStatus(html, StatusResultType.Success, "5cb85c"); + html = ApplyHtmlHighlightingForStatus(html, StatusResultType.Warning, "f0ad4e"); + return ApplyHtmlHighlightingForStatus(html, StatusResultType.Error, "d9534f"); + } + + private string ApplyHtmlHighlightingForStatus(string html, StatusResultType status, string color) + { + return html + .Replace("Result: '" + status + "'", "Result " + status + ""); + } + private string SimpleHtmlToMarkDown(string html, bool slackMarkDown = false) { if (slackMarkDown) From 0a997ae04eae4f3cda1b404f3be67a26f066bd4e Mon Sep 17 00:00:00 2001 From: AndyButland Date: Mon, 5 Jun 2017 13:40:38 +0200 Subject: [PATCH 3/3] Switched health check resolver to use application scope --- src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs index 80630f20e3..98f79056ad 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResolver.cs @@ -20,7 +20,7 @@ namespace Umbraco.Web.HealthCheck internal class HealthCheckResolver : LazyManyObjectsResolverBase, IHealthCheckResolver { public HealthCheckResolver(ILogger logger, Func> lazyTypeList) - : base(new HealthCheckServiceProvider(), logger, lazyTypeList, ObjectLifetimeScope.Transient) + : base(new HealthCheckServiceProvider(), logger, lazyTypeList, ObjectLifetimeScope.Application) { }