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) { } diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs index fb36a8bc12..0df0ba5fe8 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs @@ -14,19 +14,41 @@ 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 + { - // find out if all checks pass or not - AllChecksSuccessful = true; - foreach (var result in _results) - { - var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success); - if (checkIsSuccess == false) + + new HealthCheckStatus(message) + { + ResultType = StatusResultType.Error + } + }; + } + }); + + // find out if all checks pass or not + AllChecksSuccessful = true; + foreach (var result in _results) { - AllChecksSuccessful = false; - break; + var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success); + if (checkIsSuccess == false) + { + AllChecksSuccessful = false; + break; + } } - } + } internal void LogResults() @@ -86,17 +108,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) diff --git a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs index 78fe5a8b44..bd092e30b7 100644 --- a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs +++ b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs @@ -75,8 +75,8 @@ namespace Umbraco.Web.Scheduling using (var client = new SmtpClient()) using (var mailMessage = new MailMessage()) { - mailMessage.Body = "Results"; // TODO - get from results - mailMessage.To.Add(healthCheckConfig.NotificationSettings.EmailSettings.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()); mailMessage.Subject = "Umbraco Scheduled HeathChecks Results"; mailMessage.IsBodyHtml = true;