Port 7.7 - WIP
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Net.Mail;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Services;
|
||||
|
||||
namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
{
|
||||
[HealthCheckNotificationMethod("email")]
|
||||
public class EmailNotificationMethod : NotificationMethodBase
|
||||
{
|
||||
private readonly ILocalizedTextService _textService;
|
||||
|
||||
internal EmailNotificationMethod(ILocalizedTextService textService)
|
||||
{
|
||||
var recipientEmail = Settings["recipientEmail"]?.Value;
|
||||
if (string.IsNullOrWhiteSpace(recipientEmail))
|
||||
{
|
||||
Enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
RecipientEmail = recipientEmail;
|
||||
|
||||
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
|
||||
}
|
||||
|
||||
public string RecipientEmail { get; }
|
||||
|
||||
public override async Task SendAsync(HealthCheckResults results, CancellationToken token)
|
||||
{
|
||||
if (ShouldSend(results) == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(RecipientEmail))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var message = _textService.Localize("healthcheck/scheduledHealthCheckEmailBody", new[]
|
||||
{
|
||||
DateTime.Now.ToShortDateString(),
|
||||
DateTime.Now.ToShortTimeString(),
|
||||
results.ResultsAsHtml(Verbosity)
|
||||
});
|
||||
|
||||
var subject = _textService.Localize("healthcheck/scheduledHealthCheckEmailSubject");
|
||||
|
||||
using (var client = new SmtpClient())
|
||||
using (var mailMessage = CreateMailMessage(subject, message))
|
||||
{
|
||||
if (client.DeliveryMethod == SmtpDeliveryMethod.Network)
|
||||
{
|
||||
await client.SendMailAsync(mailMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
client.Send(mailMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private MailMessage CreateMailMessage(string subject, string message)
|
||||
{
|
||||
var to = UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(subject))
|
||||
subject = "Umbraco Health Check Status";
|
||||
|
||||
return new MailMessage(to, RecipientEmail, subject, message)
|
||||
{
|
||||
IsBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
{
|
||||
public interface IHealthCheckNotificationMethod
|
||||
{
|
||||
Task SendAsync(HealthCheckResults results, CancellationToken token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NuGet;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.HealthChecks;
|
||||
|
||||
namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
{
|
||||
public abstract class NotificationMethodBase : IHealthCheckNotificationMethod
|
||||
{
|
||||
protected NotificationMethodBase()
|
||||
{
|
||||
var type = GetType();
|
||||
var attribute = type.GetCustomAttribute<HealthCheckNotificationMethodAttribute>();
|
||||
if (attribute == null)
|
||||
{
|
||||
Enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var healthCheckConfig = UmbracoConfig.For.HealthCheck();
|
||||
var notificationMethods = healthCheckConfig.NotificationSettings.NotificationMethods;
|
||||
var notificationMethod = notificationMethods[attribute.Alias];
|
||||
if (notificationMethod == null)
|
||||
{
|
||||
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 IReadOnlyDictionary<string, INotificationMethodSettings> Settings { get; }
|
||||
|
||||
protected bool ShouldSend(HealthCheckResults results)
|
||||
{
|
||||
return Enabled && (!FailureOnly || !results.AllChecksSuccessful);
|
||||
}
|
||||
|
||||
public abstract Task SendAsync(HealthCheckResults results, CancellationToken token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user