取消异常相关
This commit is contained in:
1 parent
20ed81ed9e
commit
d66af3d6cd
3 files changed
+22
-4
No files matched your search
@@ -11,6 +11,24 @@ public static class ExceptionExtensions {
|
||||
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>
|
||||
/// 将异常所有的 <see cref="Exception.InnerException"/> 展开。
|
||||
/// 若为 <see cref="AggregateException"/>,也会展开其中所有的子异常。
|
||||
|
||||
@@ -184,8 +184,8 @@ public class BaseLogger {
|
||||
|
||||
// 核心方法
|
||||
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;
|
||||
}
|
||||
if (MinLevel > level) return;
|
||||
|
||||
@@ -34,7 +34,7 @@ public static class ResilientUtils {
|
||||
attempt++;
|
||||
if (attempt < maxAttempts) {
|
||||
Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
|
||||
Thread.Sleep(delayMs);
|
||||
if (delayMs > 0) Thread.Sleep(delayMs);
|
||||
} else { // 超过最大尝试次数
|
||||
Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
|
||||
throw;
|
||||
@@ -54,7 +54,7 @@ public static class ResilientUtils {
|
||||
attempt++;
|
||||
if (attempt < maxAttempts) {
|
||||
Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
|
||||
Thread.Sleep(delayMs);
|
||||
if (delayMs > 0) Thread.Sleep(delayMs);
|
||||
} else { // 超过最大尝试次数
|
||||
Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
|
||||
throw;
|
||||
|
||||
Reference in new issue
Block a user