committed by
GitHub
29 changed files with 563 additions and 108 deletions
@ -1,46 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
|
|
||||
namespace LINGYUN.Abp.ExceptionHandling |
|
||||
{ |
|
||||
public class AbpEmailExceptionHandlingOptions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// 默认异常收件人
|
|
||||
/// </summary>
|
|
||||
public string DefaultReceiveEmail { get; set; } |
|
||||
/// <summary>
|
|
||||
/// 异常类型指定收件人处理映射列表
|
|
||||
/// </summary>
|
|
||||
public IDictionary<Exception, string> Handlers { get; set; } |
|
||||
public AbpEmailExceptionHandlingOptions() |
|
||||
{ |
|
||||
Handlers = new Dictionary<Exception, string>(); |
|
||||
} |
|
||||
/// <summary>
|
|
||||
/// 把需要接受异常通知的用户加进处理列表
|
|
||||
/// </summary>
|
|
||||
/// <param name="ex">处理的异常类型</param>
|
|
||||
/// <param name="receivedEmails">接收邮件的用户类别,群发用,符号分隔</param>
|
|
||||
public void HandReceivedException(Exception ex, string receivedEmails) |
|
||||
{ |
|
||||
if (Handlers.ContainsKey(ex)) |
|
||||
{ |
|
||||
Handlers[ex] += receivedEmails; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
Handlers.Add(ex, receivedEmails); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public string GetReceivedEmailOrDefault(Exception ex) |
|
||||
{ |
|
||||
if (Handlers.TryGetValue(ex, out string receivedUsers)) |
|
||||
{ |
|
||||
return receivedUsers; |
|
||||
} |
|
||||
return DefaultReceiveEmail; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,10 +0,0 @@ |
|||||
using Volo.Abp.Modularity; |
|
||||
|
|
||||
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
|
||||
{ |
|
||||
[DependsOn(typeof(AbpExceptionHandlingModule))] |
|
||||
public class AbpEmailingExceptionHandlingModule : AbpModule |
|
||||
{ |
|
||||
|
|
||||
} |
|
||||
} |
|
||||
@ -1,37 +0,0 @@ |
|||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using Volo.Abp.Emailing; |
|
||||
|
|
||||
namespace LINGYUN.Abp.ExceptionHandling |
|
||||
{ |
|
||||
public class AbpEmailingExceptionSubscriber : AbpExceptionSubscriberBase |
|
||||
{ |
|
||||
protected IEmailSender EmailSender { get; } |
|
||||
protected AbpEmailExceptionHandlingOptions EmailOptions { get; } |
|
||||
public AbpEmailingExceptionSubscriber( |
|
||||
IEmailSender emailSender, |
|
||||
IServiceScopeFactory serviceScopeFactory, |
|
||||
IOptions<AbpExceptionHandlingOptions> options, |
|
||||
IOptions<AbpEmailExceptionHandlingOptions> emailOptions) |
|
||||
: base(serviceScopeFactory, options) |
|
||||
{ |
|
||||
EmailSender = emailSender; |
|
||||
EmailOptions = emailOptions.Value; |
|
||||
} |
|
||||
|
|
||||
protected override async Task SendErrorNotifierAsync(ExceptionSendNotifierContext context) |
|
||||
{ |
|
||||
var receivedUsers = EmailOptions.GetReceivedEmailOrDefault(context.Exception); |
|
||||
|
|
||||
if (!receivedUsers.IsNullOrWhiteSpace()) |
|
||||
{ |
|
||||
// TODO: 使用 Template 格式化推送
|
|
||||
await EmailSender.SendAsync(receivedUsers, |
|
||||
context.Exception.GetType().FullName, |
|
||||
context.Exception.Message); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,72 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
public class AbpEmailExceptionHandlingOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 发送堆栈信息
|
||||
|
/// </summary>
|
||||
|
public bool SendStackTrace { get; set; } = false; |
||||
|
/// <summary>
|
||||
|
/// 默认邮件标题
|
||||
|
/// </summary>
|
||||
|
public string DefaultTitle { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 默认邮件内容头
|
||||
|
/// </summary>
|
||||
|
public string DefaultContentHeader { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 默认邮件内容底
|
||||
|
/// </summary>
|
||||
|
public string DefaultContentFooter { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 默认异常收件人
|
||||
|
/// </summary>
|
||||
|
public string DefaultReceiveEmail { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 异常类型指定收件人处理映射列表
|
||||
|
/// </summary>
|
||||
|
public IDictionary<Type, string> Handlers { get; set; } |
||||
|
public AbpEmailExceptionHandlingOptions() |
||||
|
{ |
||||
|
Handlers = new Dictionary<Type, string>(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 把需要接受异常通知的用户加进处理列表
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TException">处理的异常类型</typeparam>
|
||||
|
/// <param name="receivedEmails">接收邮件的用户类别,群发用,符号分隔</param>
|
||||
|
public void HandReceivedException<TException>(string receivedEmails) where TException : Exception |
||||
|
{ |
||||
|
HandReceivedException(typeof(TException), receivedEmails); |
||||
|
} |
||||
|
/// <summary>
|
||||
|
/// 把需要接受异常通知的用户加进处理列表
|
||||
|
/// </summary>
|
||||
|
/// <param name="ex">处理的异常类型</param>
|
||||
|
/// <param name="receivedEmails">接收邮件的用户类别,群发用,符号分隔</param>
|
||||
|
public void HandReceivedException(Type exceptionType, string receivedEmails) |
||||
|
{ |
||||
|
if (Handlers.ContainsKey(exceptionType)) |
||||
|
{ |
||||
|
Handlers[exceptionType] += receivedEmails; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Handlers.Add(exceptionType, receivedEmails); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public string GetReceivedEmailOrDefault(Type exceptionType) |
||||
|
{ |
||||
|
if (Handlers.TryGetValue(exceptionType, out string receivedUsers)) |
||||
|
{ |
||||
|
return receivedUsers; |
||||
|
} |
||||
|
return DefaultReceiveEmail; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
using LINGYUN.Abp.ExceptionHandling.Emailing.Localization; |
||||
|
using Volo.Abp.Emailing; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpExceptionHandlingModule), |
||||
|
typeof(AbpEmailingModule))] |
||||
|
public class AbpEmailingExceptionHandlingModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
Configure<AbpVirtualFileSystemOptions>(options => |
||||
|
{ |
||||
|
options.FileSets.AddEmbedded<AbpEmailingExceptionHandlingModule>(); |
||||
|
}); |
||||
|
|
||||
|
Configure<AbpLocalizationOptions>(options => |
||||
|
{ |
||||
|
options.Resources |
||||
|
.Add<ExceptionHandlingResource>("en") |
||||
|
.AddVirtualJson("/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
using LINGYUN.Abp.ExceptionHandling.Emailing.Localization; |
||||
|
using LINGYUN.Abp.ExceptionHandling.Emailing.Templates; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Localization; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Emailing; |
||||
|
using Volo.Abp.TextTemplating; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
public class AbpEmailingExceptionSubscriber : AbpExceptionSubscriberBase |
||||
|
{ |
||||
|
protected IEmailSender EmailSender { get; } |
||||
|
protected IStringLocalizer StringLocalizer { get; } |
||||
|
protected ITemplateRenderer TemplateRenderer { get; } |
||||
|
protected AbpEmailExceptionHandlingOptions EmailOptions { get; } |
||||
|
public AbpEmailingExceptionSubscriber( |
||||
|
IEmailSender emailSender, |
||||
|
ITemplateRenderer templateRenderer, |
||||
|
IServiceScopeFactory serviceScopeFactory, |
||||
|
IOptions<AbpExceptionHandlingOptions> options, |
||||
|
IOptions<AbpEmailExceptionHandlingOptions> emailOptions, |
||||
|
IStringLocalizer<ExceptionHandlingResource> stringLocalizer) |
||||
|
: base(serviceScopeFactory, options) |
||||
|
{ |
||||
|
EmailSender = emailSender; |
||||
|
EmailOptions = emailOptions.Value; |
||||
|
StringLocalizer = stringLocalizer; |
||||
|
TemplateRenderer = templateRenderer; |
||||
|
} |
||||
|
|
||||
|
protected override async Task SendErrorNotifierAsync(ExceptionSendNotifierContext context) |
||||
|
{ |
||||
|
// 需不需要用 SettingProvider 来获取?
|
||||
|
var receivedUsers = EmailOptions.GetReceivedEmailOrDefault(context.Exception.GetType()); |
||||
|
|
||||
|
if (!receivedUsers.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
var emailTitle = EmailOptions.DefaultTitle ?? L("SendEmailTitle"); |
||||
|
var templateContent = await TemplateRenderer |
||||
|
.RenderAsync(ExceptionHandlingTemplates.SendEmail, |
||||
|
new |
||||
|
{ |
||||
|
title = emailTitle, |
||||
|
header = EmailOptions.DefaultContentHeader ?? L("SendEmailHeader"), |
||||
|
type = context.Exception.GetType().FullName, |
||||
|
message = context.Exception.Message, |
||||
|
loglevel = context.LogLevel.ToString(), |
||||
|
triggertime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), |
||||
|
sendstacktrace = EmailOptions.SendStackTrace, |
||||
|
stacktrace = context.Exception.ToString(), |
||||
|
footer = EmailOptions.DefaultContentFooter ?? "Copyright to LINGYUN © 2020" |
||||
|
}); |
||||
|
|
||||
|
await EmailSender.SendAsync(receivedUsers, |
||||
|
emailTitle, |
||||
|
templateContent); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected string L(string name, params object[] args) |
||||
|
{ |
||||
|
return StringLocalizer[name, args].Value; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.Localization; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing.Localization |
||||
|
{ |
||||
|
[LocalizationResourceName("AbpExceptionHandlingEmailing")] |
||||
|
public class ExceptionHandlingResource |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
{ |
||||
|
"culture": "en", |
||||
|
"texts": { |
||||
|
"SendEmailTitle": "Application exception push", |
||||
|
"SendEmailHeader": "Application exception" |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
{ |
||||
|
"culture": "zh-Hans", |
||||
|
"texts": { |
||||
|
"SendEmailTitle": "应用程序异常推送", |
||||
|
"SendEmailHeader": "应用程序异常" |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
using LINGYUN.Abp.ExceptionHandling.Emailing.Localization; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Volo.Abp.TextTemplating; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing.Templates |
||||
|
{ |
||||
|
public class ExceptionHandlingTemplateDefinitionProvider : TemplateDefinitionProvider |
||||
|
{ |
||||
|
public override void Define(ITemplateDefinitionContext context) |
||||
|
{ |
||||
|
context.Add( |
||||
|
new TemplateDefinition( |
||||
|
ExceptionHandlingTemplates.SendEmail, |
||||
|
displayName: LocalizableString.Create<ExceptionHandlingResource>("TextTemplate:ExceptionHandlingTemplates.SendEmail"), |
||||
|
defaultCultureName: "en" |
||||
|
).WithVirtualFilePath("/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail", false) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing.Templates |
||||
|
{ |
||||
|
public class ExceptionHandlingTemplates |
||||
|
{ |
||||
|
public const string SendEmail = "Abp.ExceptionHandling.SendEmail"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<title>{{ model.title }}</title> |
||||
|
</head> |
||||
|
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> |
||||
|
<table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<br /> <b><font color="#0B610B">{{ model.header }}</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<ul> |
||||
|
<li>Type : {{ model.type }}</li> |
||||
|
<li>Message : {{ model.message }}</li> |
||||
|
<li>Alarm level : {{ model.loglevel }}</li> |
||||
|
<li>TriggerTime : {{ model.triggertime }}</li> |
||||
|
</ul> |
||||
|
</td> |
||||
|
</tr> |
||||
|
{{ if model.sendstacktrace }} |
||||
|
<tr> |
||||
|
<td> |
||||
|
<b><font color="#0B610B">Stack trace</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">{{ model.stacktrace }}</pre> |
||||
|
<br /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
{{ end }} |
||||
|
<tr> |
||||
|
<td> |
||||
|
<br /> |
||||
|
<b style="float: right"><font color="#0B610B">{{ model.footer }}</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
</table> |
||||
|
</body> |
||||
|
</html> |
||||
@ -0,0 +1,48 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="zh-Hans"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<title>{{ model.title }}</title> |
||||
|
</head> |
||||
|
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> |
||||
|
<table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<br /> <b><font color="#0B610B">{{ model.header }}</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<ul> |
||||
|
<li>异常类型 : {{ model.type }}</li> |
||||
|
<li>异常信息 : {{ model.message }}</li> |
||||
|
<li>告警级别 : {{ model.loglevel }}</li> |
||||
|
<li>触发时间 : {{ model.triggertime }}</li> |
||||
|
</ul> |
||||
|
</td> |
||||
|
</tr> |
||||
|
{{ if model.sendstacktrace }} |
||||
|
<tr> |
||||
|
<td> |
||||
|
<b><font color="#0B610B">异常堆栈</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
<tr> |
||||
|
<td> |
||||
|
<pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">{{ model.stacktrace }}</pre> |
||||
|
<br /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
{{ end }} |
||||
|
<tr> |
||||
|
<td> |
||||
|
<br /> |
||||
|
<b style="float: right"><font color="#0B610B">{{ model.footer }}</font></b> |
||||
|
<hr size="2" width="100%" align="center" /> |
||||
|
</td> |
||||
|
</tr> |
||||
|
</table> |
||||
|
</body> |
||||
|
</html> |
||||
@ -1,9 +0,0 @@ |
|||||
using Volo.Abp.Modularity; |
|
||||
|
|
||||
namespace LINGYUN.Abp.ExceptionHandling |
|
||||
{ |
|
||||
[DependsOn(typeof(AbpExceptionHandlingModule))] |
|
||||
public class AbpNotificationsExceptionHandlingModule : AbpModule |
|
||||
{ |
|
||||
} |
|
||||
} |
|
||||
2
aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationDefinitionProvider.cs → aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs
2
aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationDefinitionProvider.cs → aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs
@ -1,6 +1,6 @@ |
|||||
using LINGYUN.Abp.Notifications; |
using LINGYUN.Abp.Notifications; |
||||
|
|
||||
namespace LINGYUN.Abp.ExceptionHandling |
namespace LINGYUN.Abp.ExceptionHandling.Notifications |
||||
{ |
{ |
||||
public class AbpExceptionHandlingNotificationDefinitionProvider : NotificationDefinitionProvider |
public class AbpExceptionHandlingNotificationDefinitionProvider : NotificationDefinitionProvider |
||||
{ |
{ |
||||
2
aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationNames.cs → aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationNames.cs
2
aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationNames.cs → aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationNames.cs
@ -1,4 +1,4 @@ |
|||||
namespace LINGYUN.Abp.ExceptionHandling |
namespace LINGYUN.Abp.ExceptionHandling.Notifications |
||||
{ |
{ |
||||
public class AbpExceptionHandlingNotificationNames |
public class AbpExceptionHandlingNotificationNames |
||||
{ |
{ |
||||
@ -0,0 +1,12 @@ |
|||||
|
using LINGYUN.Abp.Notifications; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Notifications |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpExceptionHandlingModule), |
||||
|
typeof(AbpNotificationModule))] |
||||
|
public class AbpNotificationsExceptionHandlingModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
<IsPackable>false</IsPackable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\modules\common\LINGYUN.Abp.ExceptionHandling.Emailing\LINGYUN.Abp.ExceptionHandling.Emailing.csproj" /> |
||||
|
<ProjectReference Include="..\LINGYUN.Abp.TestBase\LINGYUN.Abp.TestsBase.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,44 @@ |
|||||
|
using Microsoft.Extensions.Logging; |
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.ExceptionHandling; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
public class AbpEmailingExceptionSubscriber_Test : AbpExceptionHandlingEmailingTestBase |
||||
|
{ |
||||
|
private readonly IExceptionNotifier _notifier; |
||||
|
public AbpEmailingExceptionSubscriber_Test() |
||||
|
{ |
||||
|
_notifier = GetRequiredService<IExceptionNotifier>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Send_Error_Notifier_Test() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
int x = 10; |
||||
|
int y = 0; |
||||
|
int zeroDiv = x / y; |
||||
|
} |
||||
|
catch(Exception ex) |
||||
|
{ |
||||
|
await _notifier.NotifyAsync( |
||||
|
new ExceptionNotificationContext( |
||||
|
new TestSendEmailException( |
||||
|
"Test exception notufy with en", ex), LogLevel.Warning)); |
||||
|
using (CultureHelper.Use("zh-Hans")) |
||||
|
{ |
||||
|
await _notifier.NotifyAsync( |
||||
|
new ExceptionNotificationContext( |
||||
|
new TestSendEmailException( |
||||
|
"测试中文异常模板推送", ex), LogLevel.Warning)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using LINGYUN.Abp.Tests; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
public class AbpExceptionHandlingEmailingTestBase : AbpTestsBase<AbpExceptionHandlingEmailingTestModule> |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
using LINGYUN.Abp.Tests; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using System.Text; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.Security.Encryption; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpEmailingExceptionHandlingModule), |
||||
|
typeof(AbpTestsBaseModule), |
||||
|
typeof(AbpAutofacModule) |
||||
|
)] |
||||
|
public class AbpExceptionHandlingEmailingTestModule : AbpModule |
||||
|
{ |
||||
|
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
var configurationOptions = new AbpConfigurationBuilderOptions |
||||
|
{ |
||||
|
BasePath = @"D:\Projects\Development\Abp\ExceptionHandling\Emailing", |
||||
|
EnvironmentName = "Development" |
||||
|
}; |
||||
|
|
||||
|
context.Services.ReplaceConfiguration(ConfigurationHelper.BuildConfiguration(configurationOptions)); |
||||
|
} |
||||
|
|
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
// 加解密
|
||||
|
Configure<AbpStringEncryptionOptions>(options => |
||||
|
{ |
||||
|
options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; |
||||
|
options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); |
||||
|
options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); |
||||
|
}); |
||||
|
|
||||
|
// 自定义需要处理的异常
|
||||
|
Configure<AbpExceptionHandlingOptions>(options => |
||||
|
{ |
||||
|
// 加入需要处理的异常类型
|
||||
|
options.Handlers.Add<AbpException>(); |
||||
|
}); |
||||
|
// 自定义需要发送邮件通知的异常类型
|
||||
|
Configure<AbpEmailExceptionHandlingOptions>(options => |
||||
|
{ |
||||
|
// 是否发送堆栈信息
|
||||
|
options.SendStackTrace = true; |
||||
|
// 未指定异常接收者的默认接收邮件
|
||||
|
options.DefaultReceiveEmail = "colin.in@foxmail.com"; |
||||
|
// 指定某种异常发送到哪个邮件
|
||||
|
options.HandReceivedException<AbpException>("colin.in@foxmail.com"); |
||||
|
options.HandReceivedException<TestSendEmailException>("colin.in@foxmail.com"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
|
{ |
||||
|
public class TestSendEmailException : Exception, IHasNotifierErrorMessage |
||||
|
{ |
||||
|
public TestSendEmailException(string message) |
||||
|
: base(message) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public TestSendEmailException(string message, Exception innerException) |
||||
|
: base(message, innerException) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue