Port 7.7 - WIP

This commit is contained in:
Stephan
2017-09-08 19:39:13 +02:00
parent 00d2ea928d
commit 1c96df83cd
99 changed files with 9987 additions and 909 deletions

View File

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

View File

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

View File

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