Updated healthchecks to show a link to documentation instead of trying to fix something that can often not be fixed automatically.

This commit is contained in:
Bjarke Berg
2021-02-03 07:42:56 +01:00
parent 04058fb9c6
commit 8624a246ba
55 changed files with 745 additions and 459 deletions

View File

@@ -0,0 +1,90 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Mail;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
namespace Umbraco.Core.HealthChecks.NotificationMethods
{
[HealthCheckNotificationMethod("email")]
public class EmailNotificationMethod : NotificationMethodBase
{
private readonly ILocalizedTextService _textService;
private readonly IRequestAccessor _requestAccessor;
private readonly IEmailSender _emailSender;
private readonly IMarkdownToHtmlConverter _markdownToHtmlConverter;
private readonly ContentSettings _contentSettings;
public EmailNotificationMethod(
ILocalizedTextService textService,
IRequestAccessor requestAccessor,
IEmailSender emailSender,
IOptions<HealthChecksSettings> healthChecksSettings,
IOptions<ContentSettings> contentSettings,
IMarkdownToHtmlConverter markdownToHtmlConverter)
: base(healthChecksSettings)
{
var recipientEmail = Settings?["RecipientEmail"];
if (string.IsNullOrWhiteSpace(recipientEmail))
{
Enabled = false;
return;
}
RecipientEmail = recipientEmail;
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
_requestAccessor = requestAccessor;
_emailSender = emailSender;
_markdownToHtmlConverter = markdownToHtmlConverter;
_contentSettings = contentSettings.Value ?? throw new ArgumentNullException(nameof(contentSettings));
}
public string RecipientEmail { get; }
public override async Task SendAsync(HealthCheckResults results)
{
if (ShouldSend(results) == false)
{
return;
}
if (string.IsNullOrEmpty(RecipientEmail))
{
return;
}
var message = _textService.Localize("healthcheck/scheduledHealthCheckEmailBody", new[]
{
DateTime.Now.ToShortDateString(),
DateTime.Now.ToShortTimeString(),
_markdownToHtmlConverter.ToHtml(results, Verbosity)
});
// Include the umbraco Application URL host in the message subject so that
// you can identify the site that these results are for.
var host = _requestAccessor.GetApplicationUrl();
var subject = _textService.Localize("healthcheck/scheduledHealthCheckEmailSubject", new[] { host.ToString() });
var mailMessage = CreateMailMessage(subject, message);
await _emailSender.SendAsync(mailMessage);
}
private EmailMessage CreateMailMessage(string subject, string message)
{
var to = _contentSettings.Notifications.Email;
if (string.IsNullOrWhiteSpace(subject))
subject = "Umbraco Health Check Status";
var isBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</");
return new EmailMessage(to, RecipientEmail, subject, message, isBodyHtml);
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Threading.Tasks;
using Umbraco.Core.Composing;
namespace Umbraco.Core.HealthChecks.NotificationMethods
{
public interface IHealthCheckNotificationMethod : IDiscoverable
{
bool Enabled { get; }
Task SendAsync(HealthCheckResults results);
}
}

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Core.HealthChecks.NotificationMethods
{
public interface IMarkdownToHtmlConverter
{
string ToHtml(HealthCheckResults results, HealthCheckNotificationVerbosity verbosity);
}
}

View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Umbraco.Core.Configuration.Models;
namespace Umbraco.Core.HealthChecks.NotificationMethods
{
public abstract class NotificationMethodBase : IHealthCheckNotificationMethod
{
protected NotificationMethodBase(IOptions<HealthChecksSettings> healthCheckSettings)
{
var type = GetType();
var attribute = type.GetCustomAttribute<HealthCheckNotificationMethodAttribute>();
if (attribute == null)
{
Enabled = false;
return;
}
var notificationMethods = healthCheckSettings.Value.Notification.NotificationMethods;
if (!notificationMethods.TryGetValue(attribute.Alias, out var notificationMethod))
{
Enabled = false;
return;
}
Enabled = notificationMethod.Enabled;
FailureOnly = notificationMethod.FailureOnly;
Verbosity = notificationMethod.Verbosity;
Settings = notificationMethod.Settings;
}
public bool Enabled { get; protected set; }
public bool FailureOnly { get; protected set; }
public HealthCheckNotificationVerbosity Verbosity { get; protected set; }
public IDictionary<string, string> Settings { get; }
protected bool ShouldSend(HealthCheckResults results)
{
return Enabled && (!FailureOnly || !results.AllChecksSuccessful);
}
public abstract Task SendAsync(HealthCheckResults results);
}
}