diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN.Abp.ExceptionHandling.Emailing.csproj b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN.Abp.ExceptionHandling.Emailing.csproj index 83d348032..a5a7733fa 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN.Abp.ExceptionHandling.Emailing.csproj +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN.Abp.ExceptionHandling.Emailing.csproj @@ -5,4 +5,26 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailExceptionHandlingOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailExceptionHandlingOptions.cs deleted file mode 100644 index 6ea7c1e61..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailExceptionHandlingOptions.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace LINGYUN.Abp.ExceptionHandling -{ - public class AbpEmailExceptionHandlingOptions - { - /// - /// 默认异常收件人 - /// - public string DefaultReceiveEmail { get; set; } - /// - /// 异常类型指定收件人处理映射列表 - /// - public IDictionary Handlers { get; set; } - public AbpEmailExceptionHandlingOptions() - { - Handlers = new Dictionary(); - } - /// - /// 把需要接受异常通知的用户加进处理列表 - /// - /// 处理的异常类型 - /// 接收邮件的用户类别,群发用,符号分隔 - 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; - } - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionHandlingModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionHandlingModule.cs deleted file mode 100644 index 25b16fcc5..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionHandlingModule.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Volo.Abp.Modularity; - -namespace LINGYUN.Abp.ExceptionHandling.Emailing -{ - [DependsOn(typeof(AbpExceptionHandlingModule))] - public class AbpEmailingExceptionHandlingModule : AbpModule - { - - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionSubscriber.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionSubscriber.cs deleted file mode 100644 index 7333ce221..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/AbpEmailingExceptionSubscriber.cs +++ /dev/null @@ -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 options, - IOptions 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); - } - } - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailExceptionHandlingOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailExceptionHandlingOptions.cs new file mode 100644 index 000000000..b2194a077 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailExceptionHandlingOptions.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; + +namespace LINGYUN.Abp.ExceptionHandling.Emailing +{ + public class AbpEmailExceptionHandlingOptions + { + /// + /// 发送堆栈信息 + /// + public bool SendStackTrace { get; set; } = false; + /// + /// 默认邮件标题 + /// + public string DefaultTitle { get; set; } + /// + /// 默认邮件内容头 + /// + public string DefaultContentHeader { get; set; } + /// + /// 默认邮件内容底 + /// + public string DefaultContentFooter { get; set; } + /// + /// 默认异常收件人 + /// + public string DefaultReceiveEmail { get; set; } + /// + /// 异常类型指定收件人处理映射列表 + /// + public IDictionary Handlers { get; set; } + public AbpEmailExceptionHandlingOptions() + { + Handlers = new Dictionary(); + } + + /// + /// 把需要接受异常通知的用户加进处理列表 + /// + /// 处理的异常类型 + /// 接收邮件的用户类别,群发用,符号分隔 + public void HandReceivedException(string receivedEmails) where TException : Exception + { + HandReceivedException(typeof(TException), receivedEmails); + } + /// + /// 把需要接受异常通知的用户加进处理列表 + /// + /// 处理的异常类型 + /// 接收邮件的用户类别,群发用,符号分隔 + 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; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionHandlingModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionHandlingModule.cs new file mode 100644 index 000000000..c0a46567b --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionHandlingModule.cs @@ -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(options => + { + options.FileSets.AddEmbedded(); + }); + + Configure(options => + { + options.Resources + .Add("en") + .AddVirtualJson("/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources"); + }); + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber.cs new file mode 100644 index 000000000..0442c1753 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber.cs @@ -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 options, + IOptions emailOptions, + IStringLocalizer 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; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/ExceptionHandlingResource.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/ExceptionHandlingResource.cs new file mode 100644 index 000000000..a6100a7f3 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/ExceptionHandlingResource.cs @@ -0,0 +1,9 @@ +using Volo.Abp.Localization; + +namespace LINGYUN.Abp.ExceptionHandling.Emailing.Localization +{ + [LocalizationResourceName("AbpExceptionHandlingEmailing")] + public class ExceptionHandlingResource + { + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/en.json b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/en.json new file mode 100644 index 000000000..21cf1a9d0 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/en.json @@ -0,0 +1,7 @@ +{ + "culture": "en", + "texts": { + "SendEmailTitle": "Application exception push", + "SendEmailHeader": "Application exception" + } +} \ No newline at end of file diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/zh-Hans.json b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/zh-Hans.json new file mode 100644 index 000000000..dbe1e8e66 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources/zh-Hans.json @@ -0,0 +1,7 @@ +{ + "culture": "zh-Hans", + "texts": { + "SendEmailTitle": "应用程序异常推送", + "SendEmailHeader": "应用程序异常" + } +} \ No newline at end of file diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplateDefinitionProvider.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplateDefinitionProvider.cs new file mode 100644 index 000000000..60738bbde --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplateDefinitionProvider.cs @@ -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("TextTemplate:ExceptionHandlingTemplates.SendEmail"), + defaultCultureName: "en" + ).WithVirtualFilePath("/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail", false) + ); + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplates.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplates.cs new file mode 100644 index 000000000..594199d33 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/ExceptionHandlingTemplates.cs @@ -0,0 +1,7 @@ +namespace LINGYUN.Abp.ExceptionHandling.Emailing.Templates +{ + public class ExceptionHandlingTemplates + { + public const string SendEmail = "Abp.ExceptionHandling.SendEmail"; + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/en.tpl b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/en.tpl new file mode 100644 index 000000000..2a08b0d59 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/en.tpl @@ -0,0 +1,48 @@ + + + + + {{ model.title }} + + + + + + + + + + {{ if model.sendstacktrace }} + + + + + + + {{ end }} + + + +
+
{{ model.header }} +
+
+
    +
  • Type  : {{ model.type }}
  • +
  • Message  : {{ model.message }}
  • +
  • Alarm level : {{ model.loglevel }}
  • +
  • TriggerTime : {{ model.triggertime }}
  • +
+
+ Stack trace +
+
+
{{ model.stacktrace }}
+
+
+
+ {{ model.footer }} +
+
+ + \ No newline at end of file diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/zh-Hans.tpl b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/zh-Hans.tpl new file mode 100644 index 000000000..b53f74bfd --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Emailing/LINGYUN/Abp/ExceptionHandling/Emailing/Templates/SendEmail/zh-Hans.tpl @@ -0,0 +1,48 @@ + + + + + {{ model.title }} + + + + + + + + + + {{ if model.sendstacktrace }} + + + + + + + {{ end }} + + + +
+
{{ model.header }} +
+
+
    +
  • 异常类型 : {{ model.type }}
  • +
  • 异常信息 : {{ model.message }}
  • +
  • 告警级别 : {{ model.loglevel }}
  • +
  • 触发时间 : {{ model.triggertime }}
  • +
+
+ 异常堆栈 +
+
+
{{ model.stacktrace }}
+
+
+
+ {{ model.footer }} +
+
+ + \ No newline at end of file diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionHandlingModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionHandlingModule.cs deleted file mode 100644 index a52a75a9e..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionHandlingModule.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Volo.Abp.Modularity; - -namespace LINGYUN.Abp.ExceptionHandling -{ - [DependsOn(typeof(AbpExceptionHandlingModule))] - public class AbpNotificationsExceptionHandlingModule : AbpModule - { - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationDefinitionProvider.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs similarity index 86% rename from aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationDefinitionProvider.cs rename to aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs index 577026ba2..2e1904193 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationDefinitionProvider.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationDefinitionProvider.cs @@ -1,6 +1,6 @@ using LINGYUN.Abp.Notifications; -namespace LINGYUN.Abp.ExceptionHandling +namespace LINGYUN.Abp.ExceptionHandling.Notifications { public class AbpExceptionHandlingNotificationDefinitionProvider : NotificationDefinitionProvider { diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationNames.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationNames.cs similarity index 72% rename from aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationNames.cs rename to aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpExceptionHandlingNotificationNames.cs index 36d32946f..4299d63c9 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpExceptionHandlingNotificationNames.cs +++ b/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 { diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionHandlingModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionHandlingModule.cs new file mode 100644 index 000000000..d48d84368 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionHandlingModule.cs @@ -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 + { + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionSubscriber.cs b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionSubscriber.cs similarity index 96% rename from aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionSubscriber.cs rename to aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionSubscriber.cs index 5a8d2e039..3dd7f4fda 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/AbpNotificationsExceptionSubscriber.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.ExceptionHandling.Notifications/LINGYUN/Abp/ExceptionHandling/Notifications/AbpNotificationsExceptionSubscriber.cs @@ -5,7 +5,7 @@ using System; using System.Threading.Tasks; using Volo.Abp.MultiTenancy; -namespace LINGYUN.Abp.ExceptionHandling +namespace LINGYUN.Abp.ExceptionHandling.Notifications { public class AbpNotificationsExceptionSubscriber : AbpExceptionSubscriberBase { diff --git a/aspnet-core/services/account/AuthServer.Host/AuthIdentityServerModule.cs b/aspnet-core/services/account/AuthServer.Host/AuthIdentityServerModule.cs index d8b85a023..46e1eaa28 100644 --- a/aspnet-core/services/account/AuthServer.Host/AuthIdentityServerModule.cs +++ b/aspnet-core/services/account/AuthServer.Host/AuthIdentityServerModule.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.Options; using StackExchange.Redis; using System; using System.Linq; +using System.Text; using Volo.Abp; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Auditing; @@ -29,6 +30,7 @@ using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement.EntityFrameworkCore; +using Volo.Abp.Security.Encryption; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.TenantManagement.EntityFrameworkCore; using Volo.Abp.Threading; @@ -80,6 +82,14 @@ namespace AuthServer.Host options.UseMySQL(); }); + // 加解密 + Configure(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + Configure(options => { // 滑动过期30天 diff --git a/aspnet-core/services/apigateway/LINGYUN.ApiGateway.HttpApi.Host/ApiGatewayHttpApiHostModule.cs b/aspnet-core/services/apigateway/LINGYUN.ApiGateway.HttpApi.Host/ApiGatewayHttpApiHostModule.cs index 08d857f2a..d51780f0b 100644 --- a/aspnet-core/services/apigateway/LINGYUN.ApiGateway.HttpApi.Host/ApiGatewayHttpApiHostModule.cs +++ b/aspnet-core/services/apigateway/LINGYUN.ApiGateway.HttpApi.Host/ApiGatewayHttpApiHostModule.cs @@ -14,6 +14,7 @@ using Microsoft.OpenApi.Models; using StackExchange.Redis; using System; using System.Collections.Generic; +using System.Text; using Volo.Abp; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; @@ -24,6 +25,7 @@ using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.Security.Claims; +using Volo.Abp.Security.Encryption; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.TenantManagement.EntityFrameworkCore; @@ -67,6 +69,14 @@ namespace LINGYUN.ApiGateway options.UseMySQL(); }); + // 加解密 + Configure(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + // 多租户 Configure(options => { diff --git a/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/LINGYUN/Abp/MessageService/AbpMessageServiceHttpApiHostModule.cs b/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/LINGYUN/Abp/MessageService/AbpMessageServiceHttpApiHostModule.cs index 30ea5dc1e..2d2d7ba0c 100644 --- a/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/LINGYUN/Abp/MessageService/AbpMessageServiceHttpApiHostModule.cs +++ b/aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/LINGYUN/Abp/MessageService/AbpMessageServiceHttpApiHostModule.cs @@ -4,9 +4,9 @@ using IdentityModel; using LINGYUN.Abp.BackgroundJobs.Hangfire; using LINGYUN.Abp.EventBus.CAP; using LINGYUN.Abp.ExceptionHandling; +using LINGYUN.Abp.ExceptionHandling.Notifications; using LINGYUN.Abp.Hangfire.Storage.MySql; using LINGYUN.Abp.IM.SignalR; -using LINGYUN.Abp.MessageService.BackgroundJobs; using LINGYUN.Abp.MessageService.EntityFrameworkCore; using LINGYUN.Abp.MessageService.Localization; using LINGYUN.Abp.MessageService.MultiTenancy; @@ -21,11 +21,11 @@ using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using StackExchange.Redis; using System; +using System.Text; using Volo.Abp; using Volo.Abp.AspNetCore.Authentication.JwtBearer; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Autofac; -using Volo.Abp.BackgroundJobs; using Volo.Abp.Caching; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Localization; @@ -33,9 +33,9 @@ using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.Security.Claims; +using Volo.Abp.Security.Encryption; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.TenantManagement.EntityFrameworkCore; -using Volo.Abp.Threading; using Volo.Abp.VirtualFileSystem; namespace LINGYUN.Abp.MessageService @@ -86,6 +86,14 @@ namespace LINGYUN.Abp.MessageService options.UseMySQL(); }); + // 加解密 + Configure(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + Configure(options => { // 加入需要处理的异常类型 diff --git a/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/LINGYUN.Platform.HttpApi.Host.csproj b/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/LINGYUN.Platform.HttpApi.Host.csproj index a3aaf328c..8e367fb01 100644 --- a/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/LINGYUN.Platform.HttpApi.Host.csproj +++ b/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/LINGYUN.Platform.HttpApi.Host.csproj @@ -63,6 +63,7 @@ + diff --git a/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs b/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs index c026d3afc..3b73a76d4 100644 --- a/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs +++ b/aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs @@ -1,6 +1,8 @@ using DotNetCore.CAP; using IdentityModel; using LINGYUN.Abp.EventBus.CAP; +using LINGYUN.Abp.ExceptionHandling; +using LINGYUN.Abp.ExceptionHandling.Emailing; using LINGYUN.Abp.Identity; using LINGYUN.Abp.IdentityServer; using LINGYUN.Abp.Location.Baidu; @@ -21,6 +23,7 @@ using Microsoft.OpenApi.Models; using StackExchange.Redis; using System; using System.Collections.Generic; +using System.Text; using Volo.Abp; using Volo.Abp.Account; using Volo.Abp.AspNetCore.Authentication.JwtBearer; @@ -42,6 +45,7 @@ using Volo.Abp.PermissionManagement.HttpApi; using Volo.Abp.PermissionManagement.Identity; using Volo.Abp.PermissionManagement.IdentityServer; using Volo.Abp.Security.Claims; +using Volo.Abp.Security.Encryption; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.TenantManagement.EntityFrameworkCore; using Volo.Abp.VirtualFileSystem; @@ -76,6 +80,7 @@ namespace LINGYUN.Platform typeof(AbpSettingManagementEntityFrameworkCoreModule), typeof(AbpPermissionManagementEntityFrameworkCoreModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule), + typeof(AbpEmailingExceptionHandlingModule), typeof(AbpCAPEventBusModule), typeof(AbpAliyunSmsModule), #if DEBUG @@ -117,6 +122,32 @@ namespace LINGYUN.Platform options.UseMySQL(); }); + // 加解密 + Configure(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + + // 自定义需要处理的异常 + Configure(options => + { + // 加入需要处理的异常类型 + options.Handlers.Add(); + }); + // 自定义需要发送邮件通知的异常类型 + Configure(options => + { + // 是否发送堆栈信息 + options.SendStackTrace = true; + // 未指定异常接收者的默认接收邮件 + options.DefaultReceiveEmail = "colin.in@foxmail.com"; + // 指定某种异常发送到哪个邮件 + options.HandReceivedException("colin.in@foxmail.com"); + }); + + Configure(options => { // 滑动过期30天 diff --git a/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests.csproj b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests.csproj new file mode 100644 index 000000000..a8e41193f --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + diff --git a/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber_Test.cs b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber_Test.cs new file mode 100644 index 000000000..22e068b02 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpEmailingExceptionSubscriber_Test.cs @@ -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(); + } + + [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)); + } + } + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestBase.cs b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestBase.cs new file mode 100644 index 000000000..3b705cd14 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestBase.cs @@ -0,0 +1,8 @@ +using LINGYUN.Abp.Tests; + +namespace LINGYUN.Abp.ExceptionHandling.Emailing +{ + public class AbpExceptionHandlingEmailingTestBase : AbpTestsBase + { + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestModule.cs b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestModule.cs new file mode 100644 index 000000000..580793e8e --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/AbpExceptionHandlingEmailingTestModule.cs @@ -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(options => + { + options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; + options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); + options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); + }); + + // 自定义需要处理的异常 + Configure(options => + { + // 加入需要处理的异常类型 + options.Handlers.Add(); + }); + // 自定义需要发送邮件通知的异常类型 + Configure(options => + { + // 是否发送堆栈信息 + options.SendStackTrace = true; + // 未指定异常接收者的默认接收邮件 + options.DefaultReceiveEmail = "colin.in@foxmail.com"; + // 指定某种异常发送到哪个邮件 + options.HandReceivedException("colin.in@foxmail.com"); + options.HandReceivedException("colin.in@foxmail.com"); + }); + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/TestSendEmailException.cs b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/TestSendEmailException.cs new file mode 100644 index 000000000..edc751030 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.ExceptionHandling.Emailing.Tests/LINGYUN/Abp/ExceptionHandling/Emailing/TestSendEmailException.cs @@ -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) + { + + } + } +}