Pārlūkot izejas kodu

😁1、增加全局异常过滤器及异常邮件发送 2、升级sqlsugar版本 3、其他优化调整

zuohuaijun 2 gadi atpakaļ
vecāks
revīzija
9c2f6ae4fe

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -37,7 +37,7 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.34.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.20.1" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.4.112" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.4.113" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.5" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 13 - 7
Admin.NET/Admin.NET.Core/Enum/MaritalStatusEnum.cs

@@ -6,12 +6,6 @@ namespace Admin.NET.Core;
 [Description("婚姻状况枚举")]
 public enum MaritalStatusEnum
 {
-    /// <summary>
-    /// 未知
-    /// </summary>
-    [Description("未知")]
-    None = 0,
-
     /// <summary>
     /// 未婚
     /// </summary>
@@ -30,9 +24,21 @@ public enum MaritalStatusEnum
     [Description("离异")]
     Divorce = 3,
 
+    /// <summary>
+    /// 再婚
+    /// </summary>
+    [Description("再婚")]
+    Remarry = 4,
+
     /// <summary>
     /// 丧偶
     /// </summary>
     [Description("丧偶")]
-    Widowed = 4,
+    Widowed = 5,
+
+    /// <summary>
+    /// 未知
+    /// </summary>
+    [Description("未知")]
+    None = 6,
 }

+ 26 - 13
Admin.NET/Admin.NET.Core/EventBus/AppEventSubscriber.cs

@@ -21,20 +21,33 @@ public class AppEventSubscriber : IEventSubscriber, ISingleton, IDisposable
         _serviceScope = scopeFactory.CreateScope();
     }
 
-    ///// <summary>
-    ///// 增加异常日志
-    ///// </summary>
-    ///// <param name="context"></param>
-    ///// <returns></returns>
-    //[EventSubscribe("Add:ExLog")]
-    //public async Task CreateExLog(EventHandlerExecutingContext context)
-    //{
-    //    var _rep = _serviceScope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogEx>>();
-    //    await _rep.InsertAsync((SysLogEx)context.Source.Payload);
+    /// <summary>
+    /// 增加异常日志
+    /// </summary>
+    /// <param name="context"></param>
+    /// <returns></returns>
+    [EventSubscribe("Add:ExLog")]
+    public async Task CreateExLog(EventHandlerExecutingContext context)
+    {
+        var _rep = _serviceScope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogEx>>();
+        await _rep.InsertAsync((SysLogEx)context.Source.Payload);
+    }
 
-    //    // 发送邮件
-    //    await scope.ServiceProvider.GetRequiredService<SysMessageService>().SendEmail(JSON.Serialize(context.Source.Payload));
-    //}
+    /// <summary>
+    /// 发送异常邮件
+    /// </summary>
+    /// <param name="context"></param>
+    /// <returns></returns>
+    [EventSubscribe("Send:ErrorMail")]
+    public async Task SendOrderErrorMail(EventHandlerExecutingContext context)
+    {
+        //var mailTempPath = Path.Combine(App.WebHostEnvironment.WebRootPath, "Temp\\ErrorMail.tp");
+        //var mailTemp = File.ReadAllText(mailTempPath);
+        //var mail = await _serviceScope.ServiceProvider.GetRequiredService<IViewEngine>().RunCompileFromCachedAsync(mailTemp, );
+
+        var title = "Admin.NET 框架异常";
+        await _serviceScope.ServiceProvider.GetRequiredService<SysMessageService>().SendEmail(JSON.Serialize(context.Source.Payload), title, true);
+    }
 
     /// <summary>
     /// 释放服务作用域

+ 4 - 5
Admin.NET/Admin.NET.Core/Extension/RepositoryExtension.cs

@@ -332,17 +332,16 @@ public static class RepositoryExtension
     }
 
     /// <summary>
-    /// 禁用过滤运行  适用于更新和删除  禁止使用异步,否则会出现异常
-    /// </summary> 
+    /// 禁用过滤器-适用于更新和删除操作(只对当前请求有效,禁止使用异步)
+    /// </summary>
     /// <param name="repository"></param>
     /// <param name="action">禁止异步</param>
     /// <returns></returns>
     public static void RunWithoutFilter(this ISugarRepository repository, Action action)
     {
-        // 清空并还原 ,不会影响其他请求,只是当前请求清空
-        repository.Context.QueryFilter.ClearAndBackup();//清空并备份过滤器
+        repository.Context.QueryFilter.ClearAndBackup(); // 清空并备份过滤器
         action.Invoke();
-        repository.Context.QueryFilter.Restore();//还原过滤器
+        repository.Context.QueryFilter.Restore(); // 还原过滤器
 
         // 用例
         //_rep.RunWithoutFilter(() =>

+ 43 - 0
Admin.NET/Admin.NET.Core/Logging/LogExceptionHandler.cs

@@ -0,0 +1,43 @@
+// 麻省理工学院许可证
+//
+// 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司  联系电话/微信:18020030720  QQ:515096995
+//
+// 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
+//
+// 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
+// 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
+
+using System.Security.Claims;
+
+namespace Admin.NET.Core.Logging;
+
+/// <summary>
+/// 全局异常处理
+/// </summary>
+public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton
+{
+    private readonly IEventPublisher _eventPublisher;
+
+    public LogExceptionHandler(IEventPublisher eventPublisher)
+    {
+        _eventPublisher = eventPublisher;
+    }
+
+    public async Task OnExceptionAsync(ExceptionContext context)
+    {
+        var sysLogEx = new SysLogEx
+        {
+            Account = App.User?.FindFirstValue(ClaimConst.Account),
+            RealName = App.User?.FindFirstValue(ClaimConst.RealName),
+            ControllerName = context.Exception.TargetSite.DeclaringType?.FullName,
+            ActionName = context.Exception.TargetSite.Name,
+            Exception = $"异常信息:{context.Exception.Message} 异常来源:{context.Exception.Source} 堆栈跟踪:{context.Exception.StackTrace}",
+            RequestParam = context.Exception.TargetSite.GetParameters().ToString(),
+            LogDateTime = DateTime.Now
+        };
+
+        await _eventPublisher.PublishAsync(new ChannelEventSource("Add:ExLog", sysLogEx));
+
+        await _eventPublisher.PublishAsync("Send:ErrorMail", sysLogEx);
+    }
+}