龙腾猫跃 преди 4 месеца
родител
ревизия
d66af3d6cd
променени са 3 файла, в които са добавени 22 реда и са изтрити 4 реда
  1. 18 0
      Shared/Extensions/ExceptionExtensions.cs
  2. 2 2
      Shared/General/Logger.cs
  3. 2 2
      Shared/Utils/ResilientUtils.cs

+ 18 - 0
Shared/Extensions/ExceptionExtensions.cs

@@ -11,6 +11,24 @@ public static class ExceptionExtensions {
         return ex;
         return ex;
     }
     }
 
 
+    /// <summary>
+    /// 该异常或其子异常是否为 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/>。
+    /// </summary>
+    public static bool IsCanceled(this Exception ex) {
+        foreach (var inner in ex.Flatten()) {
+            if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) return true;
+        }
+        return false;
+    }
+    /// <summary>
+    /// 当该异常或其子异常是 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/> 时,抛出对应异常。
+    /// </summary>
+    public static void ThrowOnCanceled(this Exception ex) {
+        foreach (var inner in ex.Flatten()) {
+            if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) throw inner;
+        }
+    }
+
     /// <summary>
     /// <summary>
     /// 将异常所有的 <see cref="Exception.InnerException"/> 展开。
     /// 将异常所有的 <see cref="Exception.InnerException"/> 展开。
     /// 若为 <see cref="AggregateException"/>,也会展开其中所有的子异常。
     /// 若为 <see cref="AggregateException"/>,也会展开其中所有的子异常。

+ 2 - 2
Shared/General/Logger.cs

@@ -184,8 +184,8 @@ public class BaseLogger {
 
 
     // 核心方法
     // 核心方法
     public virtual void Log(Exception? ex, string? message, LogLevel level, LogBehavior behavior, string filePath) {
     public virtual void Log(Exception? ex, string? message, LogLevel level, LogBehavior behavior, string filePath) {
-        if (ex is ThreadInterruptedException) {
-            Thread.CurrentThread.Interrupt();
+        if (ex?.IsCanceled() == true) {
+            Log(null, $"操作已取消({message})", LogLevel.Info, LogBehavior.None, filePath);
             return;
             return;
         }
         }
         if (MinLevel > level) return;
         if (MinLevel > level) return;

+ 2 - 2
Shared/Utils/ResilientUtils.cs

@@ -34,7 +34,7 @@ public static class ResilientUtils {
                 attempt++;
                 attempt++;
                 if (attempt < maxAttempts) {
                 if (attempt < maxAttempts) {
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
-                    Thread.Sleep(delayMs);
+                    if (delayMs > 0) Thread.Sleep(delayMs);
                 } else { // 超过最大尝试次数
                 } else { // 超过最大尝试次数
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
                     throw;
                     throw;
@@ -54,7 +54,7 @@ public static class ResilientUtils {
                 attempt++;
                 attempt++;
                 if (attempt < maxAttempts) {
                 if (attempt < maxAttempts) {
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
-                    Thread.Sleep(delayMs);
+                    if (delayMs > 0) Thread.Sleep(delayMs);
                 } else { // 超过最大尝试次数
                 } else { // 超过最大尝试次数
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
                     Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
                     throw;
                     throw;