From fe9fc04c20ca449bad63f017ca0098e08bbbc8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Sun, 28 Jun 2026 23:34:24 +0800 Subject: [PATCH] =?UTF-8?q?Worker=20=E9=A2=9D=E5=A4=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E3=80=81=E8=BF=81=E7=A7=BB=20IsGenericInstanceOf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Extensions/GeneralExtension.cs | 15 +++++++++++ Shared/General/Logger.cs | 5 +++- Shared/General/Workers/RedoableWorker.cs | 32 ++++++++++++++++-------- 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 Shared/Extensions/GeneralExtension.cs diff --git a/Shared/Extensions/GeneralExtension.cs b/Shared/Extensions/GeneralExtension.cs new file mode 100644 index 0000000..4187af5 --- /dev/null +++ b/Shared/Extensions/GeneralExtension.cs @@ -0,0 +1,15 @@ +namespace MeloongCore.Extensions; +public static class GeneralExtension { + + /// + /// 判断对象是否为指定泛型类型的实例。 + /// + public static bool IsGenericInstanceOf(this object? instance, Type genericTypeDefinition) { + if (instance is null) return false; + for (var type = instance.GetType(); type is not null; type = type.BaseType) { + if (type.IsGenericType && type.GetGenericTypeDefinition() == genericTypeDefinition) return true; + } + return false; + } + +} diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index a9b3172..140af2a 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -219,7 +219,10 @@ public class BaseLogger { .Join("\r\n"); } public static string GetLogPrefix(LogLevel level, string filePath) - => $"{DateTime.Now:HH':'mm':'ss'.'fff} {level.ToString().First()} {(Thread.CurrentThread.Name is null ? "" : $"<{Thread.CurrentThread.ManagedThreadId} · {Thread.CurrentThread.Name}> ")}[{PathUtils.GetLastPart(filePath).BeforeFirst(".")}] "; + => $"{DateTime.Now:HH':'mm':'ss'.'fff} {level.ToString().First()} { + (Thread.CurrentThread.Name is not null ? $"<{Thread.CurrentThread.ManagedThreadId} · {Thread.CurrentThread.Name}> " : + (Thread.CurrentThread.IsThreadPoolThread ? " " : ""))}[{ + PathUtils.GetLastPart(filePath).BeforeFirst(".")}] "; /// /// 执行 。 diff --git a/Shared/General/Workers/RedoableWorker.cs b/Shared/General/Workers/RedoableWorker.cs index 7bd11cf..d283bba 100644 --- a/Shared/General/Workers/RedoableWorker.cs +++ b/Shared/General/Workers/RedoableWorker.cs @@ -33,12 +33,12 @@ public interface IWorker { ProgressProvider Progress { get; } } -// TODO: 现在的可观测性不佳,考虑增加调用方信息以及自身名称等,输出到日志,例如 CallerArgumentExpression /// /// 可合并和重启的工作器。 /// 在工作负载运行期间多次调用 会取消当前负载并使用最新令牌重新执行;多次调用也只重新执行一次。 /// -public abstract class RedoableWorkerBase(Func workload, ProgressProvider? progress = null) : IWorker { +public abstract class RedoableWorkerBase(Func workload, + ProgressProvider? progress = null, [CallerMemberName] string creatorMemberName = "") : IWorker { // ============================================ 状态与事件 ============================================ @@ -93,6 +93,7 @@ public abstract class RedoableWorkerBase(Func(Func(Func(Func(Func(Func public class RedoableWorker : RedoableWorkerBase { - public RedoableWorker(Func workload) : base(workload) { } - public RedoableWorker(Func workload) : base((c, _) => workload(c)) { } - public RedoableWorker(Func workload) : base((_, _) => workload()) { } + public RedoableWorker(Func workload, [CallerMemberName] string creatorMemberName = "") + : base(workload, creatorMemberName: creatorMemberName) { } + public RedoableWorker(Func workload, [CallerMemberName] string creatorMemberName = "") + : base((c, _) => workload(c), creatorMemberName: creatorMemberName) { } + public RedoableWorker(Func workload, [CallerMemberName] string creatorMemberName = "") + : base((_, _) => workload(), creatorMemberName: creatorMemberName) { } } /// public class RedoableWorker : RedoableWorkerBase { - public RedoableWorker(Action workload) : base((c, p) => { workload(c, p); return null; }) { } - public RedoableWorker(Action workload) : base((c, _) => { workload(c); return null; }) { } - public RedoableWorker(Action workload) : base((_, _) => { workload(); return null; }) { } + public RedoableWorker(Action workload, [CallerMemberName] string creatorMemberName = "") + : base((c, p) => { workload(c, p); return null; }, creatorMemberName: creatorMemberName) { } + public RedoableWorker(Action workload, [CallerMemberName] string creatorMemberName = "") + : base((c, _) => { workload(c); return null; }, creatorMemberName: creatorMemberName) { } + public RedoableWorker(Action workload, [CallerMemberName] string creatorMemberName = "") + : base((_, _) => { workload(); return null; }, creatorMemberName: creatorMemberName) { } }