From fc09e930148fc5b844f4bdc2bf02ff90f20217fe Mon Sep 17 00:00:00 2001 From: huangye <381538764@qq.com> Date: Tue, 24 Dec 2024 16:01:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=EF=BC=9A=E8=A7=A3=E5=86=B3=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9B=91=E5=90=AC=E5=88=B0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=97=B6=EF=BC=8C=E4=B8=94=E9=85=8D=E7=BD=AE=E4=B8=8D?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=82=AE=E4=BB=B6=EF=BC=8C=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?Log=E8=83=BD=E8=AE=B0=E5=BD=95=E5=88=B0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin.NET.Core/Service/Job/JobMonitor.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Service/Job/JobMonitor.cs b/Admin.NET/Admin.NET.Core/Service/Job/JobMonitor.cs index 318cade9..d03deeee 100644 --- a/Admin.NET/Admin.NET.Core/Service/Job/JobMonitor.cs +++ b/Admin.NET/Admin.NET.Core/Service/Job/JobMonitor.cs @@ -13,12 +13,14 @@ public class JobMonitor : IJobMonitor { private readonly IEventPublisher _eventPublisher; private readonly SysConfigService _sysConfigService; + private readonly ILogger _logger; public JobMonitor(IServiceScopeFactory scopeFactory) { var serviceScope = scopeFactory.CreateScope(); _sysConfigService = serviceScope.ServiceProvider.GetRequiredService(); - _eventPublisher = serviceScope.ServiceProvider.GetRequiredService(); ; + _eventPublisher = serviceScope.ServiceProvider.GetRequiredService(); + _logger = serviceScope.ServiceProvider.GetRequiredService>(); ; } public Task OnExecutingAsync(JobExecutingContext context, CancellationToken stoppingToken) @@ -28,11 +30,19 @@ public class JobMonitor : IJobMonitor public async Task OnExecutedAsync(JobExecutedContext context, CancellationToken stoppingToken) { - // 将异常作业发送到邮件 - if (await _sysConfigService.GetConfigValueByCode(ConfigConst.SysErrorMail) && context.Exception != null) + if (context.Exception != null) { var errorInfo = $"【{context.Trigger.Description}】定时任务错误:{context.Exception}"; - await _eventPublisher.PublishAsync(CommonConst.SendErrorMail, errorInfo, stoppingToken); + if (await _sysConfigService.GetConfigValueByCode(ConfigConst.SysErrorMail)) + { + // 将异常作业发送到邮件 + await _eventPublisher.PublishAsync(CommonConst.SendErrorMail, errorInfo, stoppingToken); + } + else + { + //将异常信息存储本地记录 + _logger.LogError(errorInfo); + } } } } \ No newline at end of file From 2df3f324df87e9bb51f533b525fd0a7dfc16588c Mon Sep 17 00:00:00 2001 From: huangye <381538764@qq.com> Date: Tue, 24 Dec 2024 16:11:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=EF=BC=9A=E5=A2=9E=E5=8A=A0=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E8=B0=83=E5=BA=A6=E6=9C=AA=E6=8D=95=E8=8E=B7=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.NET/Admin.NET.Web.Core/Startup.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Admin.NET/Admin.NET.Web.Core/Startup.cs b/Admin.NET/Admin.NET.Web.Core/Startup.cs index 933be7e4..73712152 100644 --- a/Admin.NET/Admin.NET.Web.Core/Startup.cs +++ b/Admin.NET/Admin.NET.Web.Core/Startup.cs @@ -80,6 +80,12 @@ public class Startup : AppStartup { options.AddPersistence(); // 添加作业持久化器 options.AddMonitor(); // 添加作业执行监视器 + // 定义未捕获的异常,通常是 Task 异常 + options.UnobservedTaskExceptionHandler = (obj, args) => + { + if (args.Exception?.Message != null) + Log.Error($"JobSchedule 有未处理异常 :{args.Exception?.Message} ", args.Exception); + }; } }); // 脱敏检测