From 3560ab0e54c35648fb17733b552c9301f407d0f3 Mon Sep 17 00:00:00 2001 From: Falcon <12919280+falconfly@user.noreply.gitee.com> Date: Mon, 24 Feb 2025 10:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E4=BB=BB=E5=8A=A1=E5=8F=91?= =?UTF-8?q?=E7=94=9F=E5=BC=82=E5=B8=B8=E6=97=B6=E5=A2=9E=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E5=BC=82=E5=B8=B8=E9=80=80=E5=87=BA?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs b/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs index 6a28a24..8000939 100644 --- a/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs +++ b/Falcon.SugarApi/TimedBackgroundTask/TimedTask.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; +using System.Text; using System.Threading; using System.Threading.Tasks; @@ -65,10 +66,14 @@ namespace Falcon.SugarApi.TimedBackgroundTask /// protected virtual void OnCompleted(TimedTask t,CancellationToken stoppingToken) { } /// - /// 执行中发生未处理异常 + /// 执行中发生未处理异常。返回True任务会继续执行,False则任务退出。 /// protected virtual bool OnException(TimedTask t,Exception ex,CancellationToken stoppingToken) { - return false; + StringBuilder sb = new(); + sb.AppendLine($"运行后台任务发生未处理的异常,后台任务会继续执行,异常信息可能会重复出现!"); + sb.AppendLine(ex.ToString()); + this.Logger?.LogError(sb.ToString()); + return true; } /// /// 系统服务 @@ -115,7 +120,9 @@ namespace Falcon.SugarApi.TimedBackgroundTask } } catch(Exception ex) { - this.OnException(this,ex,stoppingToken); + if(!this.OnException(this,ex,stoppingToken)) { + break; + } } SetNextTick(); this._isRunning = false;