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;