From 543e32eb6a84403375ba6a144a89aa57fc6c8133 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 25 Dec 2025 13:31:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(architecture):=20=E4=B8=BA=E6=9E=B6?= =?UTF-8?q?=E6=9E=84=E4=B8=8A=E4=B8=8B=E6=96=87=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=B7=A5=E5=8E=82=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在ArchitectureContext构造函数中添加ILoggerFactory参数 - 为IArchitectureContext接口添加LoggerFactory属性 - 在Architecture的Initialize和InitializeAsync方法中传递loggerFactory - 修改AbstractContextUtility初始化逻辑,使用NoopLoggerFactory创建默认logger - 新增ILogAware接口用于支持日志记录功能 - [no tag] --- GFramework.Core/architecture/Architecture.cs | 4 ++-- .../architecture/ArchitectureContext.cs | 17 +++++++++++------ .../architecture/IArchitectureContext.cs | 5 ++++- GFramework.Core/rule/ILogAware.cs | 15 +++++++++++++++ .../utility/AbstractContextUtility.cs | 2 +- 5 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 GFramework.Core/rule/ILogAware.cs diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 15ddf2f..3f38328 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -232,7 +232,7 @@ public abstract class Architecture( public void Initialize() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger); + _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger, Configuration.LoggerFactory); // 创建架构运行时实例 Runtime = new ArchitectureRuntime(_context); @@ -283,7 +283,7 @@ public abstract class Architecture( public async Task InitializeAsync() { _logger = Configuration.LoggerFactory.GetLogger(GetType().Name); - _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger); + _context ??= new ArchitectureContext(Container, TypeEventSystem, _logger, Configuration.LoggerFactory); // 创建架构运行时实例 Runtime = new ArchitectureRuntime(_context); diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index bbb5773..9450a70 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -13,16 +13,21 @@ namespace GFramework.Core.architecture; /// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理 /// public class ArchitectureContext( - IIocContainer container, - ITypeEventSystem typeEventSystem, - ILogger logger) + IIocContainer container, + ITypeEventSystem typeEventSystem, + ILogger logger, + ILoggerFactory? loggerFactory) : IArchitectureContext { private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container)); - private readonly ITypeEventSystem _typeEventSystem = typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem)); + + private readonly ITypeEventSystem _typeEventSystem = + typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem)); + public ILogger Logger { get; } = logger ?? throw new ArgumentNullException(nameof(logger)); + public ILoggerFactory LoggerFactory { get; } = loggerFactory ?? new NoopLoggerFactory(); internal IArchitectureRuntime Runtime { get; set; } = null!; - + #region Component Retrieval /// @@ -143,4 +148,4 @@ public class ArchitectureContext( } #endregion -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureContext.cs b/GFramework.Core/architecture/IArchitectureContext.cs index 592d546..a9b5485 100644 --- a/GFramework.Core/architecture/IArchitectureContext.cs +++ b/GFramework.Core/architecture/IArchitectureContext.cs @@ -81,7 +81,10 @@ public interface IArchitectureContext /// 获取日志记录器 /// ILogger Logger { get; } - + /// + /// 获取日志工厂 + /// + ILoggerFactory LoggerFactory { get; } /// /// 取消注册事件监听器 /// diff --git a/GFramework.Core/rule/ILogAware.cs b/GFramework.Core/rule/ILogAware.cs new file mode 100644 index 0000000..dec2952 --- /dev/null +++ b/GFramework.Core/rule/ILogAware.cs @@ -0,0 +1,15 @@ +using GFramework.Core.logging; + +namespace GFramework.Core.rule; + +/// +/// 定义一个支持日志记录的接口,允许实现类设置和使用日志记录器 +/// +public interface ILogAware +{ + /// + /// 设置日志记录器 + /// + /// 要设置的ILogger实例 + void SetLogger(ILogger logger); +} diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index 6bb1f9c..361f617 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -9,7 +9,7 @@ namespace GFramework.Core.utility; /// public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility { - private ILogger _logger = null!; + protected ILogger _logger = new NoopLoggerFactory().GetLogger(nameof(AbstractContextUtility)); /// /// 初始化上下文工具类