mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(architecture): 重构架构基类以支持依赖注入和服务配置
- 将Architecture类重构为使用构造函数注入IArchitectureConfiguration、IArchitectureServices和IArchitectureContext - 移除泛型参数和单例模式,改为使用依赖注入容器管理实例 - 添加异步初始化方法InitializeAsync支持异步初始化场景 - 引入ArchitectureOptions类统一管理架构配置选项 - 创建DefaultArchitectureConfiguration和DefaultArchitectureServices默认实现 - 新增IArchitectureContext接口提供统一的上下文访问 - 添加IAsyncInitializable接口支持异步初始化能力 - 简化架构生命周期阶段,移除Created、BeforeInit和AfterInit阶段 - 更新事件系统为ITypeEventSystem接口实现 - 重构命令和控制器接口,统一使用IContextAware替代多个能力接口 - 移除FunctionalArchitectureOptions和相关委托配置方式 - 优化日志记录使用配置中的LoggerFactory实例
This commit is contained in:
parent
9e053d525f
commit
3e672cf56f
@ -13,29 +13,47 @@ namespace GFramework.Core.architecture;
|
||||
/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。
|
||||
/// 使用单例模式确保全局唯一实例,并支持命令、查询和事件机制。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">派生类类型,用于实现单例</typeparam>
|
||||
public abstract class Architecture<T> : IArchitecture
|
||||
where T : Architecture<T>, new()
|
||||
public abstract class Architecture(
|
||||
IArchitectureConfiguration? configuration = null,
|
||||
IArchitectureServices? services = null,
|
||||
IArchitectureContext? context = null
|
||||
)
|
||||
: IArchitecture
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取架构选项的虚拟属性
|
||||
/// 获取架构配置对象
|
||||
/// </summary>
|
||||
/// <returns>返回IArchitectureOptions接口实例,包含架构配置选项</returns>
|
||||
/// <remarks>
|
||||
/// 默认实现返回FunctionalArchitectureOptions实例,其中包含两个委托:
|
||||
/// 第一个委托始终返回true,第二个委托始终返回false
|
||||
/// </remarks>
|
||||
protected virtual IArchitectureOptions Options { get; } = new FunctionalArchitectureOptions(
|
||||
() => true,
|
||||
() => false
|
||||
);
|
||||
|
||||
#region Fields and Properties
|
||||
/// <value>
|
||||
/// 返回一个IArchitectureConfiguration接口的实例,默认为DefaultArchitectureConfiguration类型
|
||||
/// </value>
|
||||
private IArchitectureConfiguration Configuration { get; } = configuration ?? new DefaultArchitectureConfiguration();
|
||||
|
||||
/// <summary>
|
||||
/// 控制反转容器,用于存储和获取各种服务(如系统、模型、工具)
|
||||
/// 获取架构服务对象
|
||||
/// </summary>
|
||||
private readonly IocContainer _mContainer = new();
|
||||
/// <value>
|
||||
/// 返回一个IArchitectureServices接口的实例,默认为DefaultArchitectureServices类型
|
||||
/// </value>
|
||||
private IArchitectureServices Services { get; } = services ?? new DefaultArchitectureServices();
|
||||
|
||||
/// <summary>
|
||||
/// 获取依赖注入容器
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// 通过Services属性获取的IArchitectureServices中的Container属性
|
||||
/// </value>
|
||||
private IIocContainer Container => Services.Container;
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型事件系统
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// 通过Services属性获取的IArchitectureServices中的TypeEventSystem属性
|
||||
/// </value>
|
||||
private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem;
|
||||
|
||||
|
||||
#region Fields and Properties
|
||||
|
||||
/// <summary>
|
||||
/// 存储尚未初始化的模型集合,在初始化阶段统一调用Init方法
|
||||
@ -52,22 +70,11 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// </summary>
|
||||
private readonly HashSet<ISystem> _allSystems = [];
|
||||
|
||||
/// <summary>
|
||||
/// 类型化事件系统,负责事件的发布与订阅管理
|
||||
/// </summary>
|
||||
private readonly TypeEventSystem _mTypeEventSystem = new();
|
||||
|
||||
/// <summary>
|
||||
/// 标记架构是否已初始化完成
|
||||
/// </summary>
|
||||
private bool _mInited;
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构实例的静态属性
|
||||
/// </summary>
|
||||
/// <returns>返回IArchitecture类型的架构实例</returns>
|
||||
public static IArchitecture Instance => MArchitectureLazy.Value;
|
||||
|
||||
/// <summary>
|
||||
/// 生命周期感知对象列表
|
||||
/// </summary>
|
||||
@ -79,76 +86,13 @@ public abstract class Architecture<T> : IArchitecture
|
||||
private ArchitecturePhase CurrentPhase { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 静态只读字段,用于延迟初始化架构实例
|
||||
/// 使用Lazy确保线程安全的单例模式实现
|
||||
/// 日志记录器实例,用于记录应用程序的运行日志
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 初始化过程包括:
|
||||
/// 1. 创建T类型的实例
|
||||
/// 2. 调用用户自定义的Init方法
|
||||
/// 3. 执行注册的补丁逻辑
|
||||
/// 4. 初始化所有已注册的模型和系统
|
||||
/// 5. 清理临时集合并标记初始化完成
|
||||
/// </remarks>
|
||||
/// <returns>T类型的架构实例</returns>
|
||||
private static readonly Lazy<T> MArchitectureLazy = new(() =>
|
||||
{
|
||||
var arch = new T();
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
|
||||
// == Architecture Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.Created);
|
||||
logger.Info($"Architecture {typeof(T).Name} created");
|
||||
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeInit);
|
||||
logger.Info("Starting architecture initialization");
|
||||
|
||||
// 调用用户实现的初始化
|
||||
arch.Init();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterInit);
|
||||
logger.Info("Architecture initialization completed");
|
||||
private ILogger _logger = null!;
|
||||
|
||||
|
||||
// == Model Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeModelInit);
|
||||
logger.Info($"Initializing {arch._mModels.Count} models");
|
||||
|
||||
// 初始化所有已注册但尚未初始化的模型
|
||||
foreach (var model in arch._mModels)
|
||||
{
|
||||
logger.Debug($"Initializing model: {model.GetType().Name}");
|
||||
model.Init();
|
||||
}
|
||||
arch._mModels.Clear();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterModelInit);
|
||||
logger.Info("All models initialized");
|
||||
|
||||
|
||||
// == System Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeSystemInit);
|
||||
logger.Info($"Initializing {arch._mSystems.Count} systems");
|
||||
|
||||
// 初始化所有已注册但尚未初始化的系统
|
||||
foreach (var system in arch._mSystems)
|
||||
{
|
||||
logger.Debug($"Initializing system: {system.GetType().Name}");
|
||||
system.Init();
|
||||
}
|
||||
arch._mSystems.Clear();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterSystemInit);
|
||||
logger.Info("All systems initialized");
|
||||
|
||||
|
||||
// == Finalize ==
|
||||
// 冻结IOC容器,不允许 anymore
|
||||
arch._mContainer.Freeze();
|
||||
arch._mInited = true;
|
||||
arch.EnterPhase(ArchitecturePhase.Ready);
|
||||
// 发送架构生命周期就绪事件
|
||||
arch.SendEvent(new ArchitectureEvents.ArchitectureLifecycleReadyEvent());
|
||||
logger.Info($"Architecture {typeof(T).Name} is ready - all components initialized");
|
||||
return arch;
|
||||
}, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
private IArchitectureContext? _context = context;
|
||||
|
||||
public IArchitectureContext Context => _context!;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -161,9 +105,8 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <exception cref="InvalidOperationException">当阶段转换不被允许时抛出异常</exception>
|
||||
private void EnterPhase(ArchitecturePhase next)
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
|
||||
if (Options.StrictPhaseValidation &&
|
||||
var logger = Configuration.LoggerFactory.GetLogger(nameof(Architecture));
|
||||
if (Configuration.Options.StrictPhaseValidation &&
|
||||
(!ArchitectureConstants.PhaseTransitions.TryGetValue(CurrentPhase, out var allowed) ||
|
||||
!allowed.Contains(next)))
|
||||
{
|
||||
@ -175,16 +118,16 @@ public abstract class Architecture<T> : IArchitecture
|
||||
|
||||
var previousPhase = CurrentPhase;
|
||||
CurrentPhase = next;
|
||||
|
||||
|
||||
if (previousPhase != next)
|
||||
{
|
||||
logger.Info($"Architecture phase changed: {previousPhase} -> {next}");
|
||||
}
|
||||
|
||||
|
||||
NotifyPhase(next);
|
||||
|
||||
// 通知所有架构阶段感知对象阶段变更
|
||||
foreach (var obj in _mContainer.GetAll<IArchitecturePhaseAware>())
|
||||
foreach (var obj in Container.GetAll<IArchitecturePhaseAware>())
|
||||
{
|
||||
logger.Debug($"Notifying phase-aware object {obj.GetType().Name} of phase change to {next}");
|
||||
obj.OnArchitecturePhase(next);
|
||||
@ -207,7 +150,7 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="hook">生命周期钩子实例</param>
|
||||
public void RegisterLifecycleHook(IArchitectureLifecycle hook)
|
||||
{
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Configuration.Options.AllowLateRegistration)
|
||||
throw new InvalidOperationException(
|
||||
"Cannot register lifecycle hook after architecture is Ready");
|
||||
_lifecycleHooks.Add(hook);
|
||||
@ -228,8 +171,8 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// </remarks>
|
||||
public virtual void Destroy()
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
|
||||
var logger = Configuration.LoggerFactory.GetLogger(nameof(Architecture));
|
||||
|
||||
// 检查当前阶段,如果已经处于销毁或已销毁状态则直接返回
|
||||
if (CurrentPhase >= ArchitecturePhase.Destroying)
|
||||
{
|
||||
@ -268,10 +211,10 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="module">要安装的模块</param>
|
||||
public void InstallModule(IArchitectureModule module)
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
var logger = Configuration.LoggerFactory.GetLogger(nameof(Architecture));
|
||||
logger.Debug($"Installing module: {module.GetType().Name}");
|
||||
RegisterLifecycleHook(module);
|
||||
_mContainer.RegisterPlurality(module);
|
||||
Container.RegisterPlurality(module);
|
||||
module.Install(this);
|
||||
logger.Info($"Module installed: {module.GetType().Name}");
|
||||
}
|
||||
@ -280,6 +223,106 @@ public abstract class Architecture<T> : IArchitecture
|
||||
|
||||
#region Component Registration
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_logger = Configuration.LoggerFactory.GetLogger(GetType().Name);
|
||||
_context ??= new DefaultArchitectureContext(Container, TypeEventSystem, _logger);
|
||||
// 调用用户实现的初始化
|
||||
Init();
|
||||
|
||||
// == Model Init ==
|
||||
EnterPhase(ArchitecturePhase.BeforeModelInit);
|
||||
_logger.Info($"Initializing {_mModels.Count} models");
|
||||
|
||||
// 初始化所有已注册但尚未初始化的模型
|
||||
foreach (var model in _mModels)
|
||||
{
|
||||
_logger.Debug($"Initializing model: {model.GetType().Name}");
|
||||
model.Init();
|
||||
}
|
||||
|
||||
_mModels.Clear();
|
||||
EnterPhase(ArchitecturePhase.AfterModelInit);
|
||||
_logger.Info("All models initialized");
|
||||
|
||||
|
||||
// == System Init ==
|
||||
EnterPhase(ArchitecturePhase.BeforeSystemInit);
|
||||
_logger.Info($"Initializing {_mSystems.Count} systems");
|
||||
|
||||
// 初始化所有已注册但尚未初始化的系统
|
||||
foreach (var system in _mSystems)
|
||||
{
|
||||
_logger.Debug($"Initializing system: {system.GetType().Name}");
|
||||
system.Init();
|
||||
}
|
||||
|
||||
_mSystems.Clear();
|
||||
EnterPhase(ArchitecturePhase.AfterSystemInit);
|
||||
_logger.Info("All systems initialized");
|
||||
|
||||
|
||||
// == Finalize ==
|
||||
// 冻结IOC容器,不允许 anymore
|
||||
Container.Freeze();
|
||||
_mInited = true;
|
||||
EnterPhase(ArchitecturePhase.Ready);
|
||||
// 发送架构生命周期就绪事件
|
||||
SendEvent(new ArchitectureEvents.ArchitectureLifecycleReadyEvent());
|
||||
_logger.Info($"Architecture {GetType().Name} is ready - all components initialized");
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
_logger = Configuration.LoggerFactory.GetLogger(GetType().Name);
|
||||
|
||||
// 调用用户实现的初始化
|
||||
Init();
|
||||
|
||||
// == Model Init ==
|
||||
EnterPhase(ArchitecturePhase.BeforeModelInit);
|
||||
_logger.Info($"Initializing {_mModels.Count} models");
|
||||
|
||||
// 异步初始化所有已注册但尚未初始化的模型
|
||||
foreach (var model in _mModels)
|
||||
{
|
||||
_logger.Debug($"Initializing model: {model.GetType().Name}");
|
||||
if (model is IAsyncInitializable asyncModel)
|
||||
await asyncModel.InitializeAsync();
|
||||
else
|
||||
model.Init();
|
||||
}
|
||||
|
||||
_mModels.Clear();
|
||||
EnterPhase(ArchitecturePhase.AfterModelInit);
|
||||
_logger.Info("All models initialized");
|
||||
|
||||
// == System Init ==
|
||||
EnterPhase(ArchitecturePhase.BeforeSystemInit);
|
||||
_logger.Info($"Initializing {_mSystems.Count} systems");
|
||||
|
||||
// 异步初始化所有已注册但尚未初始化的系统
|
||||
foreach (var system in _mSystems)
|
||||
{
|
||||
_logger.Debug($"Initializing system: {system.GetType().Name}");
|
||||
if (system is IAsyncInitializable asyncSystem)
|
||||
await asyncSystem.InitializeAsync();
|
||||
else
|
||||
system.Init();
|
||||
}
|
||||
|
||||
_mSystems.Clear();
|
||||
EnterPhase(ArchitecturePhase.AfterSystemInit);
|
||||
_logger.Info("All systems initialized");
|
||||
|
||||
// == Finalize ==
|
||||
Container.Freeze();
|
||||
_mInited = true;
|
||||
EnterPhase(ArchitecturePhase.Ready);
|
||||
SendEvent(new ArchitectureEvents.ArchitectureLifecycleReadyEvent());
|
||||
_logger.Info($"Architecture {GetType().Name} is ready - all components initialized");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册一个系统到架构中。
|
||||
/// 若当前未初始化,则暂存至待初始化列表;否则立即初始化该系统。
|
||||
@ -288,28 +331,26 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="system">要注册的系统实例</param>
|
||||
public void RegisterSystem<TSystem>(TSystem system) where TSystem : ISystem
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Configuration.Options.AllowLateRegistration)
|
||||
{
|
||||
var errorMsg = "Cannot register system after Architecture is Ready";
|
||||
logger.Error(errorMsg);
|
||||
_logger.Error(errorMsg);
|
||||
throw new InvalidOperationException(errorMsg);
|
||||
}
|
||||
|
||||
logger.Debug($"Registering system: {typeof(TSystem).Name}");
|
||||
|
||||
_logger.Debug($"Registering system: {typeof(TSystem).Name}");
|
||||
system.SetArchitecture(this);
|
||||
_mContainer.RegisterPlurality(system);
|
||||
Container.RegisterPlurality(system);
|
||||
_allSystems.Add(system);
|
||||
if (!_mInited)
|
||||
_mSystems.Add(system);
|
||||
else
|
||||
{
|
||||
logger.Debug($"Immediately initializing system: {typeof(TSystem).Name}");
|
||||
_logger.Debug($"Immediately initializing system: {typeof(TSystem).Name}");
|
||||
system.Init();
|
||||
}
|
||||
|
||||
logger.Info($"System registered: {typeof(TSystem).Name}");
|
||||
|
||||
_logger.Info($"System registered: {typeof(TSystem).Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -320,28 +361,26 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="model">要注册的模型实例</param>
|
||||
public void RegisterModel<TModel>(TModel model) where TModel : IModel
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready && !Configuration.Options.AllowLateRegistration)
|
||||
{
|
||||
var errorMsg = "Cannot register model after Architecture is Ready";
|
||||
logger.Error(errorMsg);
|
||||
_logger.Error(errorMsg);
|
||||
throw new InvalidOperationException(errorMsg);
|
||||
}
|
||||
|
||||
logger.Debug($"Registering model: {typeof(TModel).Name}");
|
||||
|
||||
_logger.Debug($"Registering model: {typeof(TModel).Name}");
|
||||
model.SetArchitecture(this);
|
||||
_mContainer.RegisterPlurality(model);
|
||||
Container.RegisterPlurality(model);
|
||||
|
||||
if (!_mInited)
|
||||
_mModels.Add(model);
|
||||
else
|
||||
{
|
||||
logger.Debug($"Immediately initializing model: {typeof(TModel).Name}");
|
||||
_logger.Debug($"Immediately initializing model: {typeof(TModel).Name}");
|
||||
model.Init();
|
||||
}
|
||||
|
||||
logger.Info($"Model registered: {typeof(TModel).Name}");
|
||||
|
||||
_logger.Info($"Model registered: {typeof(TModel).Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -352,47 +391,14 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="utility">要注册的工具实例</param>
|
||||
public void RegisterUtility<TUtility>(TUtility utility) where TUtility : IUtility
|
||||
{
|
||||
var logger = Log.CreateLogger("Architecture");
|
||||
logger.Debug($"Registering utility: {typeof(TUtility).Name}");
|
||||
_mContainer.RegisterPlurality(utility);
|
||||
logger.Info($"Utility registered: {typeof(TUtility).Name}");
|
||||
_logger.Debug($"Registering utility: {typeof(TUtility).Name}");
|
||||
Container.RegisterPlurality(utility);
|
||||
_logger.Info($"Utility registered: {typeof(TUtility).Name}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Component Retrieval
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的系统实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TSystem">目标系统类型</typeparam>
|
||||
/// <returns>对应的系统实例</returns>
|
||||
public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
|
||||
{
|
||||
return _mContainer.Get<TSystem>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的模型实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">目标模型类型</typeparam>
|
||||
/// <returns>对应的模型实例</returns>
|
||||
public TModel? GetModel<TModel>() where TModel : class, IModel
|
||||
{
|
||||
return _mContainer.Get<TModel>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的工具实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TUtility">目标工具类型</typeparam>
|
||||
/// <returns>对应的工具实例</returns>
|
||||
public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
|
||||
{
|
||||
return _mContainer.Get<TUtility>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Command Execution
|
||||
|
||||
@ -476,7 +482,7 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
public void SendEvent<TEvent>() where TEvent : new()
|
||||
{
|
||||
_mTypeEventSystem.Send<TEvent>();
|
||||
TypeEventSystem.Send<TEvent>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -486,7 +492,7 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="e">要发布的事件实例</param>
|
||||
public void SendEvent<TEvent>(TEvent e)
|
||||
{
|
||||
_mTypeEventSystem.Send(e);
|
||||
TypeEventSystem.Send(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -497,7 +503,7 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <returns>可用于取消订阅的对象</returns>
|
||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
return _mTypeEventSystem.Register(onEvent);
|
||||
return TypeEventSystem.Register(onEvent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -507,7 +513,7 @@ public abstract class Architecture<T> : IArchitecture
|
||||
/// <param name="onEvent">之前绑定的事件处理器</param>
|
||||
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
|
||||
{
|
||||
_mTypeEventSystem.UnRegister(onEvent);
|
||||
TypeEventSystem.UnRegister(onEvent);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -8,10 +8,7 @@ public static class ArchitectureConstants
|
||||
public static readonly ImmutableDictionary<ArchitecturePhase, ArchitecturePhase[]> PhaseTransitions =
|
||||
new Dictionary<ArchitecturePhase, ArchitecturePhase[]>
|
||||
{
|
||||
{ ArchitecturePhase.None, [ArchitecturePhase.Created] },
|
||||
{ ArchitecturePhase.Created, [ArchitecturePhase.BeforeInit] },
|
||||
{ ArchitecturePhase.BeforeInit, [ArchitecturePhase.AfterInit] },
|
||||
{ ArchitecturePhase.AfterInit, [ArchitecturePhase.BeforeModelInit] },
|
||||
{ ArchitecturePhase.None, [ArchitecturePhase.BeforeModelInit] },
|
||||
{ ArchitecturePhase.BeforeModelInit, [ArchitecturePhase.AfterModelInit] },
|
||||
{ ArchitecturePhase.AfterModelInit, [ArchitecturePhase.BeforeSystemInit] },
|
||||
{ ArchitecturePhase.BeforeSystemInit, [ArchitecturePhase.AfterSystemInit] },
|
||||
|
||||
20
GFramework.Core/architecture/ArchitectureOptions.cs
Normal file
20
GFramework.Core/architecture/ArchitectureOptions.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构选项配置类,用于定义架构行为的相关配置选项
|
||||
/// </summary>
|
||||
public sealed class ArchitectureOptions(
|
||||
bool strictPhaseValidation = true,
|
||||
bool allowLateRegistration = false
|
||||
)
|
||||
{
|
||||
/// <summary>
|
||||
/// 严格阶段验证开关,当设置为true时启用严格的阶段验证机制
|
||||
/// </summary>
|
||||
public bool StrictPhaseValidation = strictPhaseValidation;
|
||||
|
||||
/// <summary>
|
||||
/// 允许延迟注册开关,当设置为true时允许在初始化完成后进行组件注册
|
||||
/// </summary>
|
||||
public bool AllowLateRegistration = allowLateRegistration;
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
public class ArchitectureOptionsDelegates
|
||||
{
|
||||
/// <summary>
|
||||
/// 架构可配置选项委托
|
||||
/// </summary>
|
||||
/// <returns>是否严格验证阶段转换</returns>
|
||||
public delegate bool StrictPhaseValidationDelegate();
|
||||
|
||||
/// <summary>
|
||||
/// 架构可配置选项委托
|
||||
/// </summary>
|
||||
/// <returns>是否允许在 Ready 阶段后注册系统/模型</returns>
|
||||
public delegate bool AllowLateRegistrationDelegate();
|
||||
}
|
||||
@ -13,21 +13,6 @@ public enum ArchitecturePhase
|
||||
/// 无效阶段,表示未定义的阶段
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// 对象创建阶段,对应 new T() 操作完成后的状态
|
||||
/// </summary>
|
||||
Created,
|
||||
|
||||
/// <summary>
|
||||
/// 初始化之前阶段,在 Init() 方法调用之前的状态
|
||||
/// </summary>
|
||||
BeforeInit,
|
||||
|
||||
/// <summary>
|
||||
/// 初始化之后阶段,在 Init() 方法调用之后的状态
|
||||
/// </summary>
|
||||
AfterInit,
|
||||
|
||||
/// <summary>
|
||||
/// 模型初始化之前阶段
|
||||
/// </summary>
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
using GFramework.Core.logging;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 默认架构配置类,实现IArchitectureConfiguration接口
|
||||
/// 提供日志工厂、日志级别和架构选项的默认配置
|
||||
/// </summary>
|
||||
public class DefaultArchitectureConfiguration: IArchitectureConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置日志工厂实例
|
||||
/// 默认使用控制台日志工厂
|
||||
/// </summary>
|
||||
public ILoggerFactory LoggerFactory { get; set; } = new ConsoleLoggerFactory();
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置日志级别
|
||||
/// 默认设置为Info级别
|
||||
/// </summary>
|
||||
public LogLevel LogLevel { get; set; } = LogLevel.Info;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置架构选项
|
||||
/// 默认创建新的ArchitectureOptions实例
|
||||
/// </summary>
|
||||
public ArchitectureOptions Options { get; set; } = new();
|
||||
}
|
||||
88
GFramework.Core/architecture/DefaultArchitectureContext.cs
Normal file
88
GFramework.Core/architecture/DefaultArchitectureContext.cs
Normal file
@ -0,0 +1,88 @@
|
||||
|
||||
using System.ComponentModel;
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
using GFramework.Core.logging;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.query;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.utility;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
public class DefaultArchitectureContext(
|
||||
IIocContainer container,
|
||||
ITypeEventSystem typeEventSystem,
|
||||
ILogger logger)
|
||||
: IArchitectureContext
|
||||
{
|
||||
private readonly ITypeEventSystem _typeEventSystem = typeEventSystem;
|
||||
public ILogger Logger { get; } = logger;
|
||||
|
||||
#region Component Retrieval
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的系统实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TSystem">目标系统类型</typeparam>
|
||||
/// <returns>对应的系统实例</returns>
|
||||
public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
|
||||
{
|
||||
return container.Get<TSystem>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的模型实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">目标模型类型</typeparam>
|
||||
/// <returns>对应的模型实例</returns>
|
||||
public TModel? GetModel<TModel>() where TModel : class, IModel
|
||||
{
|
||||
return container.Get<TModel>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从IOC容器中获取指定类型的工具实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TUtility">目标工具类型</typeparam>
|
||||
/// <returns>对应的工具实例</returns>
|
||||
public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
|
||||
{
|
||||
return container.Get<TUtility>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void SendCommand(ICommand command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendEvent<TEvent>()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendEvent<TEvent>(TEvent e)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
GFramework.Core/architecture/DefaultArchitectureServices.cs
Normal file
11
GFramework.Core/architecture/DefaultArchitectureServices.cs
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
public class DefaultArchitectureServices: IArchitectureServices
|
||||
{
|
||||
public IIocContainer Container { get; } = new IocContainer();
|
||||
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem();
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 函数式架构选项实现,支持匿名实现
|
||||
/// </summary>
|
||||
public class FunctionalArchitectureOptions(Func<bool> strictPhaseValidation, Func<bool> allowLateRegistration)
|
||||
: IArchitectureOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化 FunctionalArchitectureOptions 类的新实例
|
||||
/// </summary>
|
||||
/// <param name="strictPhaseValidation">用于确定是否启用严格阶段验证的函数</param>
|
||||
/// <param name="allowLateRegistration">用于确定是否允许延迟注册的函数</param>
|
||||
private readonly Func<bool> _strictPhaseValidation = strictPhaseValidation ?? throw new ArgumentNullException(nameof(strictPhaseValidation));
|
||||
private readonly Func<bool> _allowLateRegistration = allowLateRegistration ?? throw new ArgumentNullException(nameof(allowLateRegistration));
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值,该值指示是否启用严格阶段验证
|
||||
/// </summary>
|
||||
public bool StrictPhaseValidation => _strictPhaseValidation();
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个值,该值指示是否允许延迟注册
|
||||
/// </summary>
|
||||
public bool AllowLateRegistration => _allowLateRegistration();
|
||||
}
|
||||
@ -11,8 +11,25 @@ namespace GFramework.Core.architecture;
|
||||
/// 架构接口,定义了应用程序架构的核心功能,包括系统、模型、工具的注册和获取,
|
||||
/// 以及命令、查询、事件的发送和处理机制
|
||||
/// </summary>
|
||||
public interface IArchitecture
|
||||
public interface IArchitecture: IAsyncInitializable
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化方法,用于执行对象的初始化操作
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该方法通常用于设置初始状态、初始化成员变量或执行必要的预处理操作
|
||||
/// </remarks>
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁方法,用于执行对象的清理和销毁操作
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该方法通常用于释放资源、清理内存或执行必要的清理操作
|
||||
/// </remarks>
|
||||
void Destroy();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 注册系统实例到架构中
|
||||
/// </summary>
|
||||
@ -34,26 +51,7 @@ public interface IArchitecture
|
||||
/// <param name="utility">要注册的工具实例</param>
|
||||
void RegisterUtility<T>(T utility) where T : IUtility;
|
||||
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的系统实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">系统类型,必须是class且实现ISystem接口</typeparam>
|
||||
/// <returns>指定类型的系统实例</returns>
|
||||
T? GetSystem<T>() where T : class, ISystem;
|
||||
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的模型实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">模型类型,必须是class且实现IModel接口</typeparam>
|
||||
/// <returns>指定类型的模型实例</returns>
|
||||
T? GetModel<T>() where T : class, IModel;
|
||||
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的工具实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">工具类型,必须是class且实现IUtility接口</typeparam>
|
||||
/// <returns>指定类型的工具实例</returns>
|
||||
T? GetUtility<T>() where T : class, IUtility;
|
||||
|
||||
/// <summary>
|
||||
/// 发送并执行指定的命令
|
||||
|
||||
19
GFramework.Core/architecture/IArchitectureConfiguration.cs
Normal file
19
GFramework.Core/architecture/IArchitectureConfiguration.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using GFramework.Core.logging;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 定义架构配置的接口,提供日志工厂、日志级别和架构选项的配置功能
|
||||
/// </summary>
|
||||
public interface IArchitectureConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置日志工厂,用于创建日志记录器实例
|
||||
/// </summary>
|
||||
ILoggerFactory LoggerFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置架构选项,包含架构相关的配置参数
|
||||
/// </summary>
|
||||
ArchitectureOptions Options { get; set; }
|
||||
}
|
||||
84
GFramework.Core/architecture/IArchitectureContext.cs
Normal file
84
GFramework.Core/architecture/IArchitectureContext.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.logging;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.query;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.utility;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构上下文接口,提供对系统、模型、工具类的访问以及命令、查询、事件的发送和注册功能
|
||||
/// </summary>
|
||||
public interface IArchitectureContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取指定类型的系统实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TSystem">系统类型,必须继承自ISystem接口</typeparam>
|
||||
/// <returns>系统实例,如果不存在则返回null</returns>
|
||||
TSystem? GetSystem<TSystem>() where TSystem : class, ISystem;
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定类型的模型实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">模型类型,必须继承自IModel接口</typeparam>
|
||||
/// <returns>模型实例,如果不存在则返回null</returns>
|
||||
TModel? GetModel<TModel>() where TModel : class, IModel;
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定类型的工具类实例
|
||||
/// </summary>
|
||||
/// <typeparam name="TUtility">工具类类型,必须继承自IUtility接口</typeparam>
|
||||
/// <returns>工具类实例,如果不存在则返回null</returns>
|
||||
TUtility? GetUtility<TUtility>() where TUtility : class, IUtility;
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个命令
|
||||
/// </summary>
|
||||
/// <param name="command">要发送的命令</param>
|
||||
void SendCommand(ICommand command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个带返回值的命令
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||
/// <param name="command">要发送的命令</param>
|
||||
/// <returns>命令执行结果</returns>
|
||||
TResult SendCommand<TResult>(ICommand<TResult> command);
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个查询请求
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果类型</typeparam>
|
||||
/// <param name="query">要发送的查询</param>
|
||||
/// <returns>查询结果</returns>
|
||||
TResult SendQuery<TResult>(IQuery<TResult> query);
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
void SendEvent<TEvent>();
|
||||
|
||||
/// <summary>
|
||||
/// 发送一个带参数的事件
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="e">事件参数</param>
|
||||
void SendEvent<TEvent>(TEvent e);
|
||||
|
||||
/// <summary>
|
||||
/// 注册事件处理器
|
||||
/// </summary>
|
||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
||||
/// <param name="handler">事件处理委托</param>
|
||||
/// <returns>事件注销接口</returns>
|
||||
IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler);
|
||||
|
||||
/// <summary>
|
||||
/// 获取日志记录器
|
||||
/// </summary>
|
||||
ILogger Logger { get; }
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构可配置选项接口
|
||||
/// </summary>
|
||||
public interface IArchitectureOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否严格验证阶段转换
|
||||
/// </summary>
|
||||
bool StrictPhaseValidation { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否允许在 Ready 阶段后注册系统/模型
|
||||
/// </summary>
|
||||
bool AllowLateRegistration { get; }
|
||||
}
|
||||
22
GFramework.Core/architecture/IArchitectureServices.cs
Normal file
22
GFramework.Core/architecture/IArchitectureServices.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.ioc;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构服务接口,定义了框架核心架构所需的服务组件
|
||||
/// </summary>
|
||||
public interface IArchitectureServices
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取依赖注入容器
|
||||
/// </summary>
|
||||
/// <returns>IIocContainer类型的依赖注入容器实例</returns>
|
||||
IIocContainer Container { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型事件系统
|
||||
/// </summary>
|
||||
/// <returns>ITypeEventSystem类型的事件系统实例</returns>
|
||||
ITypeEventSystem TypeEventSystem { get; }
|
||||
}
|
||||
13
GFramework.Core/architecture/IAsyncInitializable.cs
Normal file
13
GFramework.Core/architecture/IAsyncInitializable.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 定义异步初始化接口,用于需要异步初始化的组件或服务
|
||||
/// </summary>
|
||||
public interface IAsyncInitializable
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步初始化方法,用于执行组件或服务的异步初始化逻辑
|
||||
/// </summary>
|
||||
/// <returns>表示异步初始化操作的Task</returns>
|
||||
Task InitializeAsync();
|
||||
}
|
||||
@ -1,33 +1,12 @@
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 抽象命令类,实现 ICommand 接口,为具体命令提供基础架构支持
|
||||
/// </summary>
|
||||
public abstract class AbstractCommand : ICommand
|
||||
public abstract class AbstractCommand : ContextAwareBase, ICommand
|
||||
{
|
||||
private IArchitecture _mArchitecture;
|
||||
|
||||
/// <summary>
|
||||
/// 获取命令所属的架构实例
|
||||
/// </summary>
|
||||
/// <returns>IArchitecture 架构接口实例</returns>
|
||||
IArchitecture IBelongToArchitecture.GetArchitecture()
|
||||
{
|
||||
return _mArchitecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置命令所属的架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要设置的架构实例</param>
|
||||
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
|
||||
{
|
||||
_mArchitecture = architecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令,调用抽象方法 OnExecute 来实现具体逻辑
|
||||
/// </summary>
|
||||
@ -46,28 +25,8 @@ public abstract class AbstractCommand : ICommand
|
||||
/// 带返回值的抽象命令类,实现 ICommand{TResult} 接口,为需要返回结果的命令提供基础架构支持
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行后返回的结果类型</typeparam>
|
||||
public abstract class AbstractCommand<TResult> : ICommand<TResult>
|
||||
public abstract class AbstractCommand<TResult> : ContextAwareBase, ICommand<TResult>
|
||||
{
|
||||
private IArchitecture _mArchitecture;
|
||||
|
||||
/// <summary>
|
||||
/// 获取命令所属的架构实例
|
||||
/// </summary>
|
||||
/// <returns>IArchitecture 架构接口实例</returns>
|
||||
IArchitecture IBelongToArchitecture.GetArchitecture()
|
||||
{
|
||||
return _mArchitecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置命令所属的架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要设置的架构实例</param>
|
||||
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
|
||||
{
|
||||
_mArchitecture = architecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行命令,调用抽象方法 OnExecute 来实现具体逻辑并返回结果
|
||||
/// </summary>
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个可以发送命令的接口,继承自IBelongToArchitecture接口。
|
||||
/// 该接口用于标识那些具备发送命令能力的架构组件。
|
||||
/// </summary>
|
||||
public interface ICanSendCommand : IBelongToArchitecture;
|
||||
@ -1,9 +1,4 @@
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.query;
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.utility;
|
||||
|
||||
namespace GFramework.Core.command;
|
||||
|
||||
@ -11,8 +6,7 @@ namespace GFramework.Core.command;
|
||||
/// 命令接口,定义了无返回值命令的基本契约
|
||||
/// 该接口继承了多个框架能力接口,使命令可以访问架构、系统、模型、工具,并能够发送事件、命令和查询
|
||||
/// </summary>
|
||||
public interface ICommand : ICanSetArchitecture, ICanGetSystem, ICanGetModel, ICanGetUtility,
|
||||
ICanSendEvent, ICanSendCommand, ICanSendQuery
|
||||
public interface ICommand: IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的核心方法
|
||||
@ -26,9 +20,7 @@ public interface ICommand : ICanSetArchitecture, ICanGetSystem, ICanGetModel, IC
|
||||
/// 该接口继承了多个框架能力接口,使命令可以访问架构、系统、模型、工具,并能够发送事件、命令和查询
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行后返回的结果类型</typeparam>
|
||||
public interface ICommand<out TResult> : ICanSetArchitecture, ICanGetSystem, ICanGetModel,
|
||||
ICanGetUtility,
|
||||
ICanSendEvent, ICanSendCommand, ICanSendQuery
|
||||
public interface ICommand<out TResult>: IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行命令的核心方法
|
||||
|
||||
@ -1,9 +1,4 @@
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.query;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.utility;
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.controller;
|
||||
|
||||
@ -12,5 +7,4 @@ namespace GFramework.Core.controller;
|
||||
/// 该接口继承了多个框架核心接口,用于支持控制器的各种能力
|
||||
/// 包括架构归属、命令发送、系统获取、模型获取、事件注册、查询发送和工具获取等功能
|
||||
/// </summary>
|
||||
public interface IController : ICanSendCommand, ICanGetSystem, ICanGetModel,
|
||||
ICanRegisterEvent, ICanSendQuery, ICanGetUtility;
|
||||
public interface IController : IContextAware;
|
||||
@ -1,10 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.events;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个可以注册事件的接口,继承自IBelongToArchitecture接口。
|
||||
/// 该接口用于标识那些能够注册事件的对象,通常在框架的事件系统中使用。
|
||||
/// 实现此接口的类型表明它属于某个架构组件,并具备事件注册的能力。
|
||||
/// </summary>
|
||||
public interface ICanRegisterEvent : IBelongToArchitecture;
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.events;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个可以发送事件的接口,继承自IBelongToArchitecture接口。
|
||||
/// 该接口用于标识那些具备发送事件能力的类型,通常作为架构中事件发送功能的基础接口。
|
||||
/// </summary>
|
||||
public interface ICanSendEvent : IBelongToArchitecture;
|
||||
35
GFramework.Core/events/ITypeEventSystem.cs
Normal file
35
GFramework.Core/events/ITypeEventSystem.cs
Normal file
@ -0,0 +1,35 @@
|
||||
namespace GFramework.Core.events;
|
||||
|
||||
/// <summary>
|
||||
/// 类型事件系统接口,定义基于类型的事件发送、注册和注销功能
|
||||
/// </summary>
|
||||
public interface ITypeEventSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送事件,自动创建事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
||||
void Send<T>() where T : new();
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="e">事件实例</param>
|
||||
void Send<T>(T e);
|
||||
|
||||
/// <summary>
|
||||
/// 注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">事件处理回调函数</param>
|
||||
/// <returns>反注册接口,用于注销事件监听</returns>
|
||||
IUnRegister Register<T>(Action<T> onEvent);
|
||||
|
||||
/// <summary>
|
||||
/// 注销事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">要注销的事件处理回调函数</param>
|
||||
void UnRegister<T>(Action<T> onEvent);
|
||||
}
|
||||
@ -1,54 +1,40 @@
|
||||
using GFramework.Core.logging;
|
||||
|
||||
namespace GFramework.Core.events;
|
||||
namespace GFramework.Core.events;
|
||||
|
||||
/// <summary>
|
||||
/// TypeEventSystem
|
||||
/// 类型事件系统,提供基于类型的事件发送、注册和注销功能
|
||||
/// </summary>
|
||||
public class TypeEventSystem
|
||||
public class TypeEventSystem : ITypeEventSystem
|
||||
{
|
||||
public static readonly TypeEventSystem Global = new();
|
||||
private readonly EasyEvents _mEvents = new();
|
||||
|
||||
public void Send<T>() where T : new()
|
||||
{
|
||||
var logger = Log.CreateLogger("Event");
|
||||
var eventType = typeof(T);
|
||||
|
||||
logger.Debug($"Sending event: {eventType.Name}");
|
||||
_mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
|
||||
logger.Info($"Event sent: {eventType.Name}");
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送事件,自动创建事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
||||
public void Send<T>() where T : new() => _mEvents.GetEvent<EasyEvent<T>>().Trigger(new T());
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="e">事件实例</param>
|
||||
public void Send<T>(T e)
|
||||
{
|
||||
var logger = Log.CreateLogger("Event");
|
||||
var eventType = typeof(T);
|
||||
|
||||
logger.Debug($"Sending event: {eventType.Name}");
|
||||
_mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
|
||||
logger.Info($"Event sent: {eventType.Name}");
|
||||
_mEvents.GetEvent<EasyEvent<T>>().Trigger(e);
|
||||
}
|
||||
|
||||
public IUnRegister Register<T>(Action<T> onEvent)
|
||||
{
|
||||
var logger = Log.CreateLogger("Event");
|
||||
var eventType = typeof(T);
|
||||
|
||||
logger.Debug($"Registering event handler for: {eventType.Name}");
|
||||
var result = _mEvents.GetOrAddEvent<EasyEvent<T>>().Register(onEvent);
|
||||
logger.Info($"Event handler registered for: {eventType.Name}");
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 注册事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">事件处理回调函数</param>
|
||||
/// <returns>反注册接口,用于注销事件监听</returns>
|
||||
public IUnRegister Register<T>(Action<T> onEvent) => _mEvents.GetOrAddEvent<EasyEvent<T>>().Register(onEvent);
|
||||
|
||||
public void UnRegister<T>(Action<T> onEvent)
|
||||
{
|
||||
var logger = Log.CreateLogger("Event");
|
||||
var eventType = typeof(T);
|
||||
|
||||
logger.Debug($"Unregistering event handler for: {eventType.Name}");
|
||||
var e = _mEvents.GetEvent<EasyEvent<T>>();
|
||||
e?.UnRegister(onEvent);
|
||||
logger.Info($"Event handler unregistered for: {eventType.Name}");
|
||||
}
|
||||
/// <summary>
|
||||
/// 注销事件监听器
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="onEvent">要注销的事件处理回调函数</param>
|
||||
public void UnRegister<T>(Action<T> onEvent) => _mEvents.GetEvent<EasyEvent<T>>().UnRegister(onEvent);
|
||||
}
|
||||
@ -1,56 +0,0 @@
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.utility;
|
||||
|
||||
namespace GFramework.Core.extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 提供获取模型对象扩展方法的静态类
|
||||
/// </summary>
|
||||
public static class CanGetModelExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的模型实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要获取的模型类型,必须实现IModel接口</typeparam>
|
||||
/// <param name="self">实现ICanGetModel接口的对象实例</param>
|
||||
/// <returns>指定类型的模型实例</returns>
|
||||
public static T GetModel<T>(this ICanGetModel self) where T : class, IModel
|
||||
{
|
||||
return self.GetArchitecture().GetModel<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提供获取系统对象扩展方法的静态类
|
||||
/// </summary>
|
||||
public static class CanGetSystemExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的系统实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要获取的系统类型,必须实现ISystem接口</typeparam>
|
||||
/// <param name="self">实现ICanGetSystem接口的对象实例</param>
|
||||
/// <returns>指定类型的系统实例</returns>
|
||||
public static T GetSystem<T>(this ICanGetSystem self) where T : class, ISystem
|
||||
{
|
||||
return self.GetArchitecture().GetSystem<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提供获取工具对象扩展方法的静态类
|
||||
/// </summary>
|
||||
public static class CanGetUtilityExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 从架构中获取指定类型的工具实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要获取的工具类型,必须实现IUtility接口</typeparam>
|
||||
/// <param name="self">实现ICanGetUtility接口的对象实例</param>
|
||||
/// <returns>指定类型的工具实例</returns>
|
||||
public static T GetUtility<T>(this ICanGetUtility self) where T : class, IUtility
|
||||
{
|
||||
return self.GetArchitecture().GetUtility<T>();
|
||||
}
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
using GFramework.Core.events;
|
||||
|
||||
namespace GFramework.Core.extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 事件注册扩展类,提供ICanRegisterEvent接口的扩展方法用于注册和注销事件
|
||||
/// </summary>
|
||||
public static class CanRegisterEventExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册指定类型的事件处理函数
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件数据类型</typeparam>
|
||||
/// <param name="self">实现ICanRegisterEvent接口的对象实例</param>
|
||||
/// <param name="onEvent">事件处理回调函数</param>
|
||||
/// <returns>返回事件注销器接口,可用于后续注销事件</returns>
|
||||
public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent)
|
||||
{
|
||||
return self.GetArchitecture().RegisterEvent(onEvent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注销指定类型的事件处理函数
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件数据类型</typeparam>
|
||||
/// <param name="self">实现ICanRegisterEvent接口的对象实例</param>
|
||||
/// <param name="onEvent">要注销的事件处理回调函数</param>
|
||||
public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent)
|
||||
{
|
||||
self.GetArchitecture().UnRegisterEvent(onEvent);
|
||||
}
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
using GFramework.Core.command;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.query;
|
||||
|
||||
namespace GFramework.Core.extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 提供发送命令功能的扩展类
|
||||
/// </summary>
|
||||
public static class CanSendCommandExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送指定类型的命令,该命令类型必须实现ICommand接口且具有无参构造函数
|
||||
/// </summary>
|
||||
/// <typeparam name="T">命令类型,必须实现ICommand接口且具有无参构造函数</typeparam>
|
||||
/// <param name="self">实现ICanSendCommand接口的对象实例</param>
|
||||
public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
|
||||
{
|
||||
self.GetArchitecture().SendCommand(new T());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的命令实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">命令类型,必须实现ICommand接口</typeparam>
|
||||
/// <param name="self">实现ICanSendCommand接口的对象实例</param>
|
||||
/// <param name="command">要发送的命令实例</param>
|
||||
public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
|
||||
{
|
||||
self.GetArchitecture().SendCommand(command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送带有返回值的命令并获取执行结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||
/// <param name="self">实现ICanSendCommand接口的对象实例</param>
|
||||
/// <param name="command">要发送的命令实例,必须实现ICommand<TResult>接口</param>
|
||||
/// <returns>命令执行后的返回结果</returns>
|
||||
public static TResult SendCommand<TResult>(this ICanSendCommand self, ICommand<TResult> command)
|
||||
{
|
||||
return self.GetArchitecture().SendCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提供发送事件功能的扩展类
|
||||
/// </summary>
|
||||
public static class CanSendEventExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送指定类型的事件,该事件类型必须具有无参构造函数
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
||||
/// <param name="self">实现ICanSendEvent接口的对象实例</param>
|
||||
public static void SendEvent<T>(this ICanSendEvent self) where T : new()
|
||||
{
|
||||
self.GetArchitecture().SendEvent<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送指定的事件实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">事件类型</typeparam>
|
||||
/// <param name="self">实现ICanSendEvent接口的对象实例</param>
|
||||
/// <param name="e">要发送的事件实例</param>
|
||||
public static void SendEvent<T>(this ICanSendEvent self, T e)
|
||||
{
|
||||
self.GetArchitecture().SendEvent(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提供发送查询功能的扩展类
|
||||
/// </summary>
|
||||
public static class CanSendQueryExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送查询请求并获取查询结果
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
/// <param name="self">实现ICanSendQuery接口的对象实例</param>
|
||||
/// <param name="query">要发送的查询实例,必须实现IQuery<TResult>接口</param>
|
||||
/// <returns>查询操作的返回结果</returns>
|
||||
public static TResult SendQuery<TResult>(this ICanSendQuery self, IQuery<TResult> query)
|
||||
{
|
||||
return self.GetArchitecture().SendQuery(query);
|
||||
}
|
||||
}
|
||||
117
GFramework.Core/ioc/IIocContainer.cs
Normal file
117
GFramework.Core/ioc/IIocContainer.cs
Normal file
@ -0,0 +1,117 @@
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.system;
|
||||
|
||||
namespace GFramework.Core.ioc;
|
||||
|
||||
/// <summary>
|
||||
/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作
|
||||
/// </summary>
|
||||
public interface IIocContainer:IContextAware{
|
||||
/// <summary>
|
||||
/// 初始化方法
|
||||
/// </summary>
|
||||
void Init();
|
||||
#region Register Methods
|
||||
|
||||
/// <summary>
|
||||
/// 注册单例
|
||||
/// 一个类型只允许一个实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要注册为单例的类型</typeparam>
|
||||
/// <param name="instance">要注册的单例实例</param>
|
||||
/// <exception cref="InvalidOperationException">当该类型已经注册过单例时抛出异常</exception>
|
||||
void RegisterSingleton<T>(T instance);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 注册一个实例及其所有可赋值的接口类型到容器中
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实例的类型</typeparam>
|
||||
/// <param name="instance">要注册的实例对象,不能为null</param>
|
||||
void RegisterPlurality<T>(T instance);
|
||||
|
||||
/// <summary>
|
||||
/// 注册系统实例,将其绑定到其所有实现的接口上
|
||||
/// </summary>
|
||||
/// <param name="system">系统实例对象</param>
|
||||
void RegisterSystem(ISystem system);
|
||||
|
||||
/// <summary>
|
||||
/// 注册指定类型的实例到容器中
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要注册的实例类型</typeparam>
|
||||
/// <param name="instance">要注册的实例对象,不能为null</param>
|
||||
void Register<T>(T instance);
|
||||
|
||||
/// <summary>
|
||||
/// 注册指定类型的实例到容器中
|
||||
/// </summary>
|
||||
/// <param name="type">要注册的实例类型</param>
|
||||
/// <param name="instance">要注册的实例对象</param>
|
||||
void Register(Type type, object instance);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get Methods
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个实例(通常用于具体类型)
|
||||
/// 如果存在多个,只返回第一个
|
||||
/// </summary>
|
||||
/// <typeparam name="T">期望获取的实例类型</typeparam>
|
||||
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
||||
T? Get<T>() where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定类型的必需实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">期望获取的实例类型</typeparam>
|
||||
/// <returns>找到的唯一实例</returns>
|
||||
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
||||
T GetRequired<T>() where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">期望获取的实例类型</typeparam>
|
||||
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
|
||||
IReadOnlyList<T> GetAll<T>() where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 获取并排序(系统调度专用)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">期望获取的实例类型</typeparam>
|
||||
/// <param name="comparison">比较器委托,定义排序规则</param>
|
||||
/// <returns>按指定方式排序后的实例列表</returns>
|
||||
IReadOnlyList<T> GetAllSorted<T>(Comparison<T> comparison) where T : class;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
/// <summary>
|
||||
/// 检查容器中是否包含指定类型的实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要检查的类型</typeparam>
|
||||
/// <returns>如果容器中包含指定类型的实例则返回true,否则返回false</returns>
|
||||
bool Contains<T>();
|
||||
|
||||
/// <summary>
|
||||
/// 判断容器中是否包含某个具体的实例对象
|
||||
/// </summary>
|
||||
/// <param name="instance">待查询的实例对象</param>
|
||||
/// <returns>若容器中包含该实例则返回true,否则返回false</returns>
|
||||
bool ContainsInstance(object instance);
|
||||
|
||||
/// <summary>
|
||||
/// 清空容器中的所有实例
|
||||
/// </summary>
|
||||
void Clear();
|
||||
|
||||
/// <summary>
|
||||
/// 冻结容器,防止后续修改
|
||||
/// </summary>
|
||||
void Freeze();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using GFramework.Core.logging;
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.system;
|
||||
|
||||
namespace GFramework.Core.ioc;
|
||||
@ -6,7 +7,7 @@ namespace GFramework.Core.ioc;
|
||||
/// <summary>
|
||||
/// IOC容器类,用于管理对象的注册和获取
|
||||
/// </summary>
|
||||
public class IocContainer
|
||||
public class IocContainer : ContextAwareBase, IIocContainer
|
||||
{
|
||||
#region Core
|
||||
|
||||
@ -22,6 +23,8 @@ public class IocContainer
|
||||
/// </summary>
|
||||
private readonly Dictionary<Type, HashSet<object>> _typeIndex = new();
|
||||
|
||||
private ILogger _logger = null!;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lock
|
||||
@ -48,6 +51,11 @@ public class IocContainer
|
||||
|
||||
#region Register
|
||||
|
||||
public void Init()
|
||||
{
|
||||
_logger = Context.Logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册单例
|
||||
/// 一个类型只允许一个实例
|
||||
@ -58,27 +66,25 @@ public class IocContainer
|
||||
public void RegisterSingleton<T>(T instance)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var logger = Log.CreateLogger("IOC");
|
||||
|
||||
_lock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (_frozen)
|
||||
{
|
||||
var errorMsg = "IocContainer is frozen";
|
||||
logger.Error(errorMsg);
|
||||
_logger.Error(errorMsg);
|
||||
throw new InvalidOperationException(errorMsg);
|
||||
}
|
||||
|
||||
if (_typeIndex.TryGetValue(type, out var set) && set.Count > 0)
|
||||
{
|
||||
var errorMsg = $"Singleton already registered for type: {type.Name}";
|
||||
logger.Error(errorMsg);
|
||||
_logger.Error(errorMsg);
|
||||
throw new InvalidOperationException(errorMsg);
|
||||
}
|
||||
|
||||
RegisterInternal(type, instance!);
|
||||
logger.Debug($"Singleton registered: {type.Name}");
|
||||
_logger.Debug($"Singleton registered: {type.Name}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
@ -95,8 +101,6 @@ public class IocContainer
|
||||
public void RegisterPlurality<T>(T instance)
|
||||
{
|
||||
var concreteType = instance!.GetType();
|
||||
var logger = Log.CreateLogger("IOC");
|
||||
|
||||
// 获取实例类型直接实现的所有接口,并筛选出可以赋值给T类型的接口
|
||||
var interfaces = concreteType.GetInterfaces()
|
||||
.Where(typeof(T).IsAssignableFrom);
|
||||
@ -106,13 +110,13 @@ public class IocContainer
|
||||
{
|
||||
// 注册具体类型
|
||||
RegisterInternal(concreteType, instance);
|
||||
logger.Debug($"Registered concrete type: {concreteType.Name}");
|
||||
_logger.Debug($"Registered concrete type: {concreteType.Name}");
|
||||
|
||||
// 注册所有匹配的接口类型
|
||||
foreach (var itf in interfaces)
|
||||
{
|
||||
RegisterInternal(itf, instance);
|
||||
logger.Debug($"Registered interface: {itf.Name} for {concreteType.Name}");
|
||||
_logger.Debug($"Registered interface: {itf.Name} for {concreteType.Name}");
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -130,8 +134,8 @@ public class IocContainer
|
||||
{
|
||||
if (_frozen)
|
||||
{
|
||||
var errorMsg = "IocContainer is frozen";
|
||||
Log.CreateLogger("IOC").Error(errorMsg);
|
||||
const string errorMsg = "IocContainer is frozen";
|
||||
_logger.Error(errorMsg);
|
||||
throw new InvalidOperationException(errorMsg);
|
||||
}
|
||||
|
||||
@ -209,19 +213,17 @@ public class IocContainer
|
||||
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
||||
public T? Get<T>() where T : class
|
||||
{
|
||||
var logger = Log.CreateLogger("IOC");
|
||||
|
||||
_lock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_typeIndex.TryGetValue(typeof(T), out var set) && set.Count > 0)
|
||||
{
|
||||
var result = set.First() as T;
|
||||
logger.Debug($"Retrieved instance: {typeof(T).Name}");
|
||||
_logger.Debug($"Retrieved instance: {typeof(T).Name}");
|
||||
return result;
|
||||
}
|
||||
|
||||
logger.Debug($"No instance found for type: {typeof(T).Name}");
|
||||
_logger.Debug($"No instance found for type: {typeof(T).Name}");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
@ -239,23 +241,22 @@ public class IocContainer
|
||||
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
||||
public T GetRequired<T>() where T : class
|
||||
{
|
||||
var logger = Log.CreateLogger("IOC");
|
||||
var list = GetAll<T>();
|
||||
|
||||
switch (list.Count)
|
||||
{
|
||||
case 0:
|
||||
var notFoundMsg = $"No instance registered for {typeof(T).Name}";
|
||||
logger.Error(notFoundMsg);
|
||||
_logger.Error(notFoundMsg);
|
||||
throw new InvalidOperationException(notFoundMsg);
|
||||
|
||||
case 1:
|
||||
logger.Debug($"Retrieved required instance: {typeof(T).Name}");
|
||||
_logger.Debug($"Retrieved required instance: {typeof(T).Name}");
|
||||
return list[0];
|
||||
|
||||
default:
|
||||
var multipleMsg = $"Multiple instances registered for {typeof(T).Name}";
|
||||
logger.Error(multipleMsg);
|
||||
_logger.Error(multipleMsg);
|
||||
throw new InvalidOperationException(multipleMsg);
|
||||
}
|
||||
}
|
||||
@ -358,14 +359,12 @@ public class IocContainer
|
||||
/// </summary>
|
||||
public void Freeze()
|
||||
{
|
||||
var logger = Log.CreateLogger("IOC");
|
||||
|
||||
// 获取写锁以确保线程安全的状态修改
|
||||
_lock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_frozen = true;
|
||||
logger.Info("IOC Container frozen - no further registrations allowed");
|
||||
_logger.Info("IOC Container frozen - no further registrations allowed");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@ -1,92 +1,406 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 控制台日志记录器实现,支持日志级别控制和格式化输出
|
||||
/// 控制台日志记录器
|
||||
/// </summary>
|
||||
public sealed class ConsoleLogger : ILog
|
||||
public sealed class ConsoleLogger : ILogger
|
||||
{
|
||||
private readonly LogLevel _minLevel;
|
||||
private readonly string? _category;
|
||||
private readonly TextWriter? _writer;
|
||||
private readonly string _name;
|
||||
private readonly TextWriter _writer;
|
||||
private readonly bool _useColors;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// 初始化控制台日志记录器实例
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <param name="minLevel">最小日志级别(默认Info)</param>
|
||||
/// <param name="writer">输出流(默认控制台)</param>
|
||||
/// <param name="useColors">是否使用颜色输出(默认true)</param>
|
||||
/// <param name="name">日志记录器名称,默认为根日志记录器名称</param>
|
||||
/// <param name="minLevel">最小日志级别,默认为Info级别</param>
|
||||
/// <param name="writer">文本写入器,默认为控制台输出</param>
|
||||
/// <param name="useColors">是否使用颜色输出,默认为true</param>
|
||||
public ConsoleLogger(
|
||||
string? category = null,
|
||||
string? name = null,
|
||||
LogLevel minLevel = LogLevel.Info,
|
||||
TextWriter? writer = null,
|
||||
bool useColors = true)
|
||||
{
|
||||
_category = category;
|
||||
_name = name ?? ILogger.RootLoggerName;
|
||||
_minLevel = minLevel;
|
||||
_writer = writer ?? Console.Out;
|
||||
_useColors = useColors && _writer == Console.Out;
|
||||
}
|
||||
|
||||
#region Metadata
|
||||
|
||||
/// <summary>
|
||||
/// 记录日志消息
|
||||
/// 获取日志记录器名称
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
/// <param name="exception">异常信息(可选)</param>
|
||||
/// <param name="context">上下文信息(可选)</param>
|
||||
public void Log(LogLevel level, string message, Exception? exception = null, object? context = null)
|
||||
{
|
||||
if (!IsEnabled(level))
|
||||
return;
|
||||
/// <returns>日志记录器名称</returns>
|
||||
public string Name() => _name;
|
||||
|
||||
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
var levelStr = level.ToString().ToUpper().PadRight(7);
|
||||
var categoryStr = _category != null ? $"[{_category}] " : "";
|
||||
var contextStr = context != null ? $" | Context: {FormatContext(context)}" : "";
|
||||
var exceptionStr = exception != null ? $"\nException: {exception}" : "";
|
||||
#endregion
|
||||
|
||||
var logMessage = $"[{timestamp}] {levelStr} {categoryStr}{message}{contextStr}{exceptionStr}";
|
||||
#region Level Checks
|
||||
|
||||
if (_useColors)
|
||||
{
|
||||
WriteColored(level, logMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer!.WriteLine(logMessage);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 检查是否启用Trace级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsTraceEnabled() => IsEnabled(LogLevel.Trace);
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用Debug级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsDebugEnabled() => IsEnabled(LogLevel.Debug);
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用Info级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsInfoEnabled() => IsEnabled(LogLevel.Info);
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用Warn级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsWarnEnabled() => IsEnabled(LogLevel.Warning);
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用Error级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsErrorEnabled() => IsEnabled(LogLevel.Error);
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用Fatal级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
public bool IsFatalEnabled() => IsEnabled(LogLevel.Fatal);
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定日志级别是否启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>如果启用则返回true,否则返回false</returns>
|
||||
public bool IsEnabled(LogLevel level) => level >= _minLevel;
|
||||
/// <returns>如果启用返回true,否则返回false</returns>
|
||||
private bool IsEnabled(LogLevel level) => level >= _minLevel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Trace
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前最小日志级别
|
||||
/// 记录Trace级别日志消息
|
||||
/// </summary>
|
||||
public LogLevel MinLevel => _minLevel;
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Trace(string msg) => LogIfEnabled(LogLevel.Trace, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 使用颜色写入日志消息
|
||||
/// 记录Trace级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Trace(string format, object arg) => LogIfEnabled(LogLevel.Trace, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Trace级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Trace(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Trace, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Trace级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Trace(string format, params object[] arguments) => LogIfEnabled(LogLevel.Trace, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Trace级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Trace(string msg, Exception t) => LogException(LogLevel.Trace, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Debug
|
||||
|
||||
/// <summary>
|
||||
/// 记录Debug级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Debug(string msg) => LogIfEnabled(LogLevel.Debug, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Debug级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Debug(string format, object arg) => LogIfEnabled(LogLevel.Debug, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Debug级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Debug(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Debug, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Debug级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Debug(string format, params object[] arguments) => LogIfEnabled(LogLevel.Debug, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Debug级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Debug(string msg, Exception t) => LogException(LogLevel.Debug, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Info
|
||||
|
||||
/// <summary>
|
||||
/// 记录Info级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Info(string msg) => LogIfEnabled(LogLevel.Info, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Info级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Info(string format, object arg) => LogIfEnabled(LogLevel.Info, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Info级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Info(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Info, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Info级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Info(string format, params object[] arguments) => LogIfEnabled(LogLevel.Info, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Info级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Info(string msg, Exception t) => LogException(LogLevel.Info, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Warn
|
||||
|
||||
/// <summary>
|
||||
/// 记录Warn级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Warn(string msg) => LogIfEnabled(LogLevel.Warning, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Warn级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Warn(string format, object arg) => LogIfEnabled(LogLevel.Warning, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Warn级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Warn(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Warning, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Warn级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Warn(string format, params object[] arguments) => LogIfEnabled(LogLevel.Warning, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Warn级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Warn(string msg, Exception t) => LogException(LogLevel.Warning, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Error
|
||||
|
||||
/// <summary>
|
||||
/// 记录Error级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Error(string msg) => LogIfEnabled(LogLevel.Error, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Error级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Error(string format, object arg) => LogIfEnabled(LogLevel.Error, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Error级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Error(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Error, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Error级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Error(string format, params object[] arguments) => LogIfEnabled(LogLevel.Error, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Error级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Error(string msg, Exception t) => LogException(LogLevel.Error, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fatal
|
||||
|
||||
/// <summary>
|
||||
/// 记录Fatal级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
public void Fatal(string msg) => LogIfEnabled(LogLevel.Fatal, msg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Fatal级别日志消息(带格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Fatal(string format, object arg) => LogIfEnabled(LogLevel.Fatal, format, arg);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Fatal级别日志消息(带两个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Fatal(string format, object arg1, object arg2) => LogIfEnabled(LogLevel.Fatal, format, arg1, arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Fatal级别日志消息(带多个格式化参数)
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Fatal(string format, params object[] arguments) => LogIfEnabled(LogLevel.Fatal, format, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 记录Fatal级别异常日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="t">异常对象</param>
|
||||
public void Fatal(string msg, Exception t) => LogException(LogLevel.Fatal, msg, t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Core
|
||||
|
||||
/// <summary>
|
||||
/// 如果指定级别已启用,则记录日志消息
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">要写入的消息</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
private void LogIfEnabled(LogLevel level, string message)
|
||||
{
|
||||
if (!IsEnabled(level)) return;
|
||||
LogInternal(level, message, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 如果指定级别已启用,则记录格式化日志消息
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="args">格式化参数数组</param>
|
||||
private void LogIfEnabled(LogLevel level, string format, params object[] args)
|
||||
{
|
||||
if (!IsEnabled(level)) return;
|
||||
LogInternal(level, string.Format(format, args), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 如果指定级别已启用,则记录异常日志
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
/// <param name="exception">异常对象</param>
|
||||
private void LogException(LogLevel level, string message, Exception exception)
|
||||
{
|
||||
if (!IsEnabled(level)) return;
|
||||
LogInternal(level, message, exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部日志记录方法
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
/// <param name="exception">异常对象(可选)</param>
|
||||
private void LogInternal(LogLevel level, string message, Exception? exception)
|
||||
{
|
||||
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
var levelStr = level.ToString().ToUpper().PadRight(7);
|
||||
var log = $"[{timestamp}] {levelStr} [{_name}] {message}";
|
||||
|
||||
// 添加异常信息到日志
|
||||
if (exception != null)
|
||||
{
|
||||
log += Environment.NewLine + exception;
|
||||
}
|
||||
|
||||
if (_useColors)
|
||||
{
|
||||
WriteColored(level, log);
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteLine(log);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以指定颜色写入日志消息
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
private void WriteColored(LogLevel level, string message)
|
||||
{
|
||||
var originalColor = Console.ForegroundColor;
|
||||
var original = Console.ForegroundColor;
|
||||
try
|
||||
{
|
||||
Console.ForegroundColor = GetColor(level);
|
||||
_writer!.WriteLine(message);
|
||||
_writer.WriteLine(message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ForegroundColor = originalColor;
|
||||
Console.ForegroundColor = original;
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,86 +408,17 @@ public sealed class ConsoleLogger : ILog
|
||||
/// 根据日志级别获取对应的颜色
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <returns>对应的控制台颜色</returns>
|
||||
private static ConsoleColor GetColor(LogLevel level)
|
||||
/// <returns>控制台颜色</returns>
|
||||
private static ConsoleColor GetColor(LogLevel level) => level switch
|
||||
{
|
||||
return level switch
|
||||
{
|
||||
LogLevel.Trace => ConsoleColor.Gray,
|
||||
LogLevel.Debug => ConsoleColor.Cyan,
|
||||
LogLevel.Info => ConsoleColor.White,
|
||||
LogLevel.Warning => ConsoleColor.Yellow,
|
||||
LogLevel.Error => ConsoleColor.Red,
|
||||
LogLevel.Fatal => ConsoleColor.Magenta,
|
||||
_ => ConsoleColor.White
|
||||
};
|
||||
}
|
||||
LogLevel.Trace => ConsoleColor.DarkGray,
|
||||
LogLevel.Debug => ConsoleColor.Cyan,
|
||||
LogLevel.Info => ConsoleColor.White,
|
||||
LogLevel.Warning => ConsoleColor.Yellow,
|
||||
LogLevel.Error => ConsoleColor.Red,
|
||||
LogLevel.Fatal => ConsoleColor.Magenta,
|
||||
_ => ConsoleColor.White
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 格式化上下文对象为字符串
|
||||
/// </summary>
|
||||
/// <param name="context">上下文对象</param>
|
||||
/// <returns>格式化后的字符串</returns>
|
||||
private static string FormatContext(object context)
|
||||
{
|
||||
return context switch
|
||||
{
|
||||
string str => str,
|
||||
int num => num.ToString(),
|
||||
null => "null",
|
||||
_ => $"{context.GetType().Name}: {context}"
|
||||
};
|
||||
}
|
||||
|
||||
// 快捷方法实现
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Info(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Info, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">异常信息(可选)</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Error(string msg, Exception? ex = null, object? ctx = null)
|
||||
=> Log(LogLevel.Error, msg, ex, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Debug(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Debug, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Trace(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Trace, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Warn(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Warning, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">异常信息(可选)</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Fatal(string msg,Exception? ex = null, object? ctx = null)
|
||||
=> Log(LogLevel.Fatal, msg, ex, ctx);
|
||||
#endregion
|
||||
}
|
||||
|
||||
17
GFramework.Core/logging/ConsoleLoggerFactory.cs
Normal file
17
GFramework.Core/logging/ConsoleLoggerFactory.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 控制台日志提供程序,用于创建控制台日志记录器实例
|
||||
/// </summary>
|
||||
public class ConsoleLoggerFactory : ILoggerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取指定名称的控制台日志记录器实例
|
||||
/// </summary>
|
||||
/// <param name="name">日志记录器的名称</param>
|
||||
/// <returns>控制台日志记录器实例</returns>
|
||||
public ILogger GetLogger(string name)
|
||||
{
|
||||
return new ConsoleLogger(name);
|
||||
}
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 定义日志记录接口,提供日志记录和级别检查功能
|
||||
/// </summary>
|
||||
public interface ILog
|
||||
{
|
||||
/// <summary>
|
||||
/// 记录指定级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息内容</param>
|
||||
/// <param name="exception">可选的异常对象,默认为null</param>
|
||||
/// <param name="context">可选的上下文对象,默认为null</param>
|
||||
void Log(
|
||||
LogLevel level,
|
||||
string message,
|
||||
Exception? exception = null,
|
||||
object? context = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定日志级别是否启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>如果指定级别已启用则返回true,否则返回false</returns>
|
||||
bool IsEnabled(LogLevel level);
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Info(string msg, object? ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常对象(可选)</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Error(string msg, Exception? ex = null, object? ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Debug(string msg, object? ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Trace(string msg, object? ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Warn(string msg, object? ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常对象(可选)</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
void Fatal(string msg, Exception? ex = null,object? ctx = null);
|
||||
}
|
||||
330
GFramework.Core/logging/ILogger.cs
Normal file
330
GFramework.Core/logging/ILogger.cs
Normal file
@ -0,0 +1,330 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 定义日志记录接口,提供日志记录和级别检查功能
|
||||
/// </summary>
|
||||
public interface ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// 根日志记录器的名称常量
|
||||
/// </summary>
|
||||
public const string RootLoggerName = "ROOT";
|
||||
|
||||
/// <summary>
|
||||
/// 获取日志记录器的名称
|
||||
/// </summary>
|
||||
/// <returns>日志记录器的名称</returns>
|
||||
string Name();
|
||||
|
||||
#region Level Enabled Check
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Trace级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Trace级别日志则返回true,否则返回false</returns>
|
||||
bool IsTraceEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Debug级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Debug级别日志则返回true,否则返回false</returns>
|
||||
bool IsDebugEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Info级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Info级别日志则返回true,否则返回false</returns>
|
||||
bool IsInfoEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Warn级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Warn级别日志则返回true,否则返回false</returns>
|
||||
bool IsWarnEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Error级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Error级别日志则返回true,否则返回false</returns>
|
||||
bool IsErrorEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了Fatal级别日志
|
||||
/// </summary>
|
||||
/// <returns>如果启用了Fatal级别日志则返回true,否则返回false</returns>
|
||||
bool IsFatalEnabled();
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定的日志级别是否已启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>如果指定的日志级别已启用则返回true,否则返回false</returns>
|
||||
bool IsEnabledForLevel(LogLevel level)
|
||||
{
|
||||
return level switch
|
||||
{
|
||||
LogLevel.Trace => IsTraceEnabled(),
|
||||
LogLevel.Debug => IsDebugEnabled(),
|
||||
LogLevel.Info => IsInfoEnabled(),
|
||||
LogLevel.Warning => IsWarnEnabled(),
|
||||
LogLevel.Error => IsErrorEnabled(),
|
||||
LogLevel.Fatal => IsFatalEnabled(),
|
||||
_ => throw new ArgumentException("Level [" + level + "] not recognized.")
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Trace Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 TRACE 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Trace(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 TRACE 级别的消息
|
||||
/// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Trace(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 TRACE 级别的消息
|
||||
/// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Trace(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 TRACE 级别的消息
|
||||
/// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Trace(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 TRACE 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Trace(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Debug Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 DEBUG 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Debug(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 DEBUG 级别的消息
|
||||
/// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Debug(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 DEBUG 级别的消息
|
||||
/// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Debug(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 DEBUG 级别的消息
|
||||
/// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Debug(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 DEBUG 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Debug(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Info Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 INFO 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Info(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 INFO 级别的消息
|
||||
/// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Info(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 INFO 级别的消息
|
||||
/// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Info(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 INFO 级别的消息
|
||||
/// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Info(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 INFO 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Info(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Warn Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 WARN 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Warn(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 WARN 级别的消息
|
||||
/// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Warn(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 WARN 级别的消息
|
||||
/// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Warn(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 WARN 级别的消息
|
||||
/// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Warn(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 WARN 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Warn(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Error Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 ERROR 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Error(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 ERROR 级别的消息
|
||||
/// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Error(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 ERROR 级别的消息
|
||||
/// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Error(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 ERROR 级别的消息
|
||||
/// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Error(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 ERROR 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Error(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fatal Logging Methods
|
||||
|
||||
/// <summary>
|
||||
/// 记录 FATAL 级别的消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息字符串</param>
|
||||
void Fatal(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 FATAL 级别的消息
|
||||
/// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg">参数</param>
|
||||
void Fatal(string format, object arg);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数记录 FATAL 级别的消息
|
||||
/// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arg1">第一个参数</param>
|
||||
/// <param name="arg2">第二个参数</param>
|
||||
void Fatal(string format, object arg1, object arg2);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定格式和参数数组记录 FATAL 级别的消息
|
||||
/// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的字符串连接
|
||||
/// </summary>
|
||||
/// <param name="format">格式字符串</param>
|
||||
/// <param name="arguments">参数数组</param>
|
||||
void Fatal(string format, params object[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// 使用伴随消息在 FATAL 级别记录异常
|
||||
/// </summary>
|
||||
/// <param name="msg">伴随异常的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
void Fatal(string msg, Exception t);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -6,16 +6,9 @@
|
||||
public interface ILoggerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建指定类别的日志记录器实例
|
||||
/// 根据指定的名称获取日志记录器实例
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别,用于区分不同的日志源</param>
|
||||
/// <returns>返回指定类别的日志记录器实例</returns>
|
||||
ILog Create(string category);
|
||||
|
||||
/// <summary>
|
||||
/// 创建全局日志记录器实例
|
||||
/// </summary>
|
||||
/// <returns>返回全局日志记录器实例</returns>
|
||||
ILog CreateGlobalLogger();
|
||||
}
|
||||
|
||||
/// <param name="name">日志记录器的名称</param>
|
||||
/// <returns>指定名称的日志记录器实例</returns>
|
||||
ILogger GetLogger(string name);
|
||||
}
|
||||
@ -1,149 +0,0 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 日志记录的静态类,提供全局日志记录功能
|
||||
/// </summary>
|
||||
public static class Log
|
||||
{
|
||||
private static ILoggerFactory? _factory;
|
||||
private static LogConfig? _config;
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前的日志记录器实例
|
||||
/// </summary>
|
||||
public static ILog Instance { get; private set; } = new ConsoleLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前的日志配置
|
||||
/// </summary>
|
||||
public static LogConfig Config => _config ??= new LogConfig();
|
||||
|
||||
/// <summary>
|
||||
/// 设置日志记录器实例
|
||||
/// </summary>
|
||||
/// <param name="logger">要设置的日志记录器,如果为 null 则使用默认的ConsoleLogger</param>
|
||||
public static void SetLogger(ILog? logger)
|
||||
{
|
||||
Instance = logger ?? new ConsoleLogger();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用日志工厂创建日志记录器
|
||||
/// </summary>
|
||||
/// <param name="factory">日志工厂实例</param>
|
||||
public static void SetLoggerFactory(ILoggerFactory factory)
|
||||
{
|
||||
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
|
||||
Instance = _factory.CreateGlobalLogger();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用日志配置初始化日志系统
|
||||
/// </summary>
|
||||
/// <param name="config">日志配置</param>
|
||||
public static void Initialize(LogConfig config)
|
||||
{
|
||||
_config = config ?? throw new ArgumentNullException(nameof(config));
|
||||
_factory = new LoggerFactory(config);
|
||||
Instance = _factory.CreateGlobalLogger();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 快速配置日志系统
|
||||
/// </summary>
|
||||
/// <param name="minLevel">最小日志级别(默认为Info)</param>
|
||||
/// <param name="enableConsole">是否启用控制台输出(默认为true)</param>
|
||||
/// <param name="useColors">是否使用彩色输出(默认为true)</param>
|
||||
/// <param name="enableFile">是否启用文件输出(默认为false)</param>
|
||||
/// <param name="logFilePath">日志文件路径(可选)</param>
|
||||
public static void Configure(
|
||||
LogLevel minLevel = LogLevel.Info,
|
||||
bool enableConsole = true,
|
||||
bool useColors = true,
|
||||
bool enableFile = false,
|
||||
string? logFilePath = null)
|
||||
{
|
||||
var config = new LogConfig
|
||||
{
|
||||
DefaultMinLevel = minLevel,
|
||||
EnableConsole = enableConsole,
|
||||
UseColors = useColors,
|
||||
EnableFile = enableFile,
|
||||
LogFilePath = logFilePath
|
||||
};
|
||||
|
||||
Initialize(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建指定类别的日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
public static ILog CreateLogger(string category)
|
||||
{
|
||||
if (_factory != null)
|
||||
{
|
||||
return _factory.Create(category);
|
||||
}
|
||||
|
||||
// 如果没有设置工厂,使用默认配置
|
||||
return new ConsoleLogger(category, Config.GetCategoryLevel(category));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定日志级别是否启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>如果指定级别已启用则返回 true,否则返回 false</returns>
|
||||
public static bool IsEnabled(LogLevel level) => Instance.IsEnabled(level);
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Info(string msg, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Info, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常对象(可选)</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Error(string msg, Exception? ex = null, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Error, msg, ex, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Debug(string msg, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Debug, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Trace(string msg, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Trace, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Warn(string msg, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Warning, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public static void Fatal(string msg, object? ctx = null)
|
||||
=> Instance.Log(LogLevel.Fatal, msg, null, ctx);
|
||||
}
|
||||
@ -1,102 +0,0 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 日志配置类,用于配置日志记录器的行为
|
||||
/// </summary>
|
||||
public sealed class LogConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置默认的最小日志级别(默认为Info)
|
||||
/// </summary>
|
||||
public LogLevel DefaultMinLevel { get; set; } = LogLevel.Info;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否启用控制台输出(默认为true)
|
||||
/// </summary>
|
||||
public bool EnableConsole { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否使用彩色输出(默认为true)
|
||||
/// </summary>
|
||||
public bool UseColors { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置是否启用文件输出(默认为false)
|
||||
/// </summary>
|
||||
public bool EnableFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置日志文件路径(当EnableFile为true时使用)
|
||||
/// </summary>
|
||||
public string? LogFilePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 取特定类别的日志级别配置
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, LogLevel> _categoryLevels = new();
|
||||
|
||||
/// <summary>
|
||||
/// 设置特定类别的日志级别
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <param name="level">日志级别</param>
|
||||
public void SetCategoryLevel(string category, LogLevel level)
|
||||
{
|
||||
_categoryLevels[category] = level;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类别的日志级别,如果没有特定配置则返回默认级别
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志级别</returns>
|
||||
public LogLevel GetCategoryLevel(string? category)
|
||||
{
|
||||
if (category != null && _categoryLevels.TryGetValue(category, out var level))
|
||||
{
|
||||
return level;
|
||||
}
|
||||
|
||||
return DefaultMinLevel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建控制台日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
public ILog CreateConsoleLogger(string? category = null)
|
||||
{
|
||||
var level = GetCategoryLevel(category);
|
||||
return new ConsoleLogger(category, level, null, UseColors);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建文件日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
public ILog CreateFileLogger(string? category = null)
|
||||
{
|
||||
if (!EnableFile || string.IsNullOrEmpty(LogFilePath))
|
||||
return new NullLogger();
|
||||
|
||||
try
|
||||
{
|
||||
var level = GetCategoryLevel(category);
|
||||
var directory = Path.GetDirectoryName(LogFilePath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var writer = new StreamWriter(LogFilePath, append: true);
|
||||
return new ConsoleLogger(category, level, writer, useColors: false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 如果创建文件失败,返回空日志记录器
|
||||
return new NullLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,184 +0,0 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 日志工厂实现,使用LogConfig配置创建日志记录器
|
||||
/// </summary>
|
||||
public class LoggerFactory : ILoggerFactory
|
||||
{
|
||||
private readonly LogConfig _config;
|
||||
private readonly Dictionary<string, ILog> _loggers = new();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="config">日志配置</param>
|
||||
public LoggerFactory(LogConfig config)
|
||||
{
|
||||
_config = config ?? throw new ArgumentNullException(nameof(config));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建指定类别的日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
public ILog Create(string category)
|
||||
{
|
||||
if (_loggers.TryGetValue(category, out var existingLogger))
|
||||
{
|
||||
return existingLogger;
|
||||
}
|
||||
|
||||
var logger = CreateLogger(category);
|
||||
_loggers[category] = logger;
|
||||
return logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建全局日志记录器实例
|
||||
/// </summary>
|
||||
/// <returns>全局日志记录器</returns>
|
||||
public ILog CreateGlobalLogger()
|
||||
{
|
||||
return Create("Global");
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
private ILog CreateLogger(string category)
|
||||
{
|
||||
var level = _config.GetCategoryLevel(category);
|
||||
var consoleLogger = new ConsoleLogger(category, level, null, _config.UseColors);
|
||||
|
||||
if (_config.EnableFile && !string.IsNullOrEmpty(_config.LogFilePath))
|
||||
{
|
||||
// 创建一个组合日志记录器,同时输出到控制台和文件
|
||||
return new CompositeLogger(level, consoleLogger, CreateFileLogger(category));
|
||||
}
|
||||
|
||||
return consoleLogger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建文件日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>文件日志记录器实例</returns>
|
||||
private ILog CreateFileLogger(string category)
|
||||
{
|
||||
try
|
||||
{
|
||||
var level = _config.GetCategoryLevel(category);
|
||||
var directory = Path.GetDirectoryName(_config.LogFilePath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var writer = new StreamWriter(_config.LogFilePath!, append: true);
|
||||
return new ConsoleLogger(category, level, writer, useColors: false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new NullLogger();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 组合日志记录器,将日志同时输出到多个目标
|
||||
/// </summary>
|
||||
/// <param name="minLevel">最小日志级别,低于此级别的日志将被忽略</param>
|
||||
/// <param name="consoleLogger">控制台日志记录器</param>
|
||||
/// <param name="fileLogger">文件日志记录器</param>
|
||||
private sealed class CompositeLogger(
|
||||
LogLevel minLevel,
|
||||
ILog consoleLogger,
|
||||
ILog fileLogger)
|
||||
: ILog
|
||||
{
|
||||
/// <summary>
|
||||
/// 记录日志消息到所有配置的日志目标
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
/// <param name="exception">相关异常(可选)</param>
|
||||
/// <param name="context">上下文信息(可选)</param>
|
||||
public void Log(
|
||||
LogLevel level,
|
||||
string message,
|
||||
Exception? exception = null,
|
||||
object? context = null)
|
||||
{
|
||||
if (!IsEnabled(level))
|
||||
return;
|
||||
|
||||
consoleLogger.Log(level, message, exception, context);
|
||||
fileLogger.Log(level, message, exception, context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定的日志级别是否已启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>如果级别已启用则返回true,否则返回false</returns>
|
||||
public bool IsEnabled(LogLevel level)
|
||||
{
|
||||
return level >= minLevel;
|
||||
}
|
||||
|
||||
// 快捷方法
|
||||
/// <summary>
|
||||
/// 记录跟踪级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Trace(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Trace, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Debug(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Debug, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Info(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Info, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Warn(string msg, object? ctx = null)
|
||||
=> Log(LogLevel.Warning, msg, null, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常(可选)</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Error(string msg, Exception? ex = null, object? ctx = null)
|
||||
=> Log(LogLevel.Error, msg, ex, ctx);
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别的日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常(可选)</param>
|
||||
/// <param name="ctx">上下文信息(可选)</param>
|
||||
public void Fatal(string msg, Exception? ex = null, object? ctx = null)
|
||||
=> Log(LogLevel.Fatal, msg, ex, ctx);
|
||||
}
|
||||
|
||||
}
|
||||
371
GFramework.Core/logging/NoopLogger.cs
Normal file
371
GFramework.Core/logging/NoopLogger.cs
Normal file
@ -0,0 +1,371 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 空操作日志记录器实现,不执行任何实际的日志记录操作
|
||||
/// </summary>
|
||||
internal sealed class NoopLogger : ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取日志记录器的名称
|
||||
/// </summary>
|
||||
/// <returns>返回"NOOP"字符串</returns>
|
||||
public string Name()
|
||||
{
|
||||
return "NOOP";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了跟踪级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsTraceEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了调试级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsDebugEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了信息级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsInfoEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了警告级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsWarnEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了错误级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsErrorEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否启用了致命错误级别日志
|
||||
/// </summary>
|
||||
/// <returns>始终返回false</returns>
|
||||
public bool IsFatalEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Trace(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Trace(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Trace(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Trace(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Trace(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Debug(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Debug(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Debug(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Debug(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Debug(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Info(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Info(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Info(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Info(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Info(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Warn(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Warn(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Warn(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Warn(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Warn(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Error(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Error(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Error(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Error(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Error(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志消息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
public void Fatal(string msg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志消息,支持格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg">格式化参数</param>
|
||||
public void Fatal(string format, object arg)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志消息,支持两个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arg1">第一个格式化参数</param>
|
||||
/// <param name="arg2">第二个格式化参数</param>
|
||||
public void Fatal(string format, object arg1, object arg2)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志消息,支持多个格式化参数
|
||||
/// </summary>
|
||||
/// <param name="format">格式化字符串</param>
|
||||
/// <param name="arguments">格式化参数数组</param>
|
||||
public void Fatal(string format, params object[] arguments)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志消息和异常信息
|
||||
/// </summary>
|
||||
/// <param name="msg">要记录的消息</param>
|
||||
/// <param name="t">要记录的异常</param>
|
||||
public void Fatal(string msg, Exception t)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
17
GFramework.Core/logging/NoopLoggerFactory.cs
Normal file
17
GFramework.Core/logging/NoopLoggerFactory.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 无操作日志工厂实现,用于提供空的日志记录功能
|
||||
/// </summary>
|
||||
public class NoopLoggerFactory: ILoggerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取指定名称的无操作日志记录器
|
||||
/// </summary>
|
||||
/// <param name="name">日志记录器的名称</param>
|
||||
/// <returns>返回一个NoopLogger实例,该实例不执行任何实际的日志记录操作</returns>
|
||||
public ILogger GetLogger(string name)
|
||||
{
|
||||
return new NoopLogger();
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
/// 空日志记录器实现,用于禁用日志记录功能
|
||||
/// </summary>
|
||||
internal sealed class NullLogger : ILog
|
||||
{
|
||||
/// <summary>
|
||||
/// 记录日志消息(空实现,不执行任何操作)
|
||||
/// </summary>
|
||||
/// <param name="level">日志级别</param>
|
||||
/// <param name="message">日志消息</param>
|
||||
/// <param name="exception">相关异常对象(可选)</param>
|
||||
/// <param name="context">日志上下文信息(可选)</param>
|
||||
public void Log(LogLevel level, string message, Exception? exception, object? context) { }
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定日志级别是否启用
|
||||
/// </summary>
|
||||
/// <param name="level">要检查的日志级别</param>
|
||||
/// <returns>始终返回 false,表示所有日志级别都被禁用</returns>
|
||||
public bool IsEnabled(LogLevel level) => false;
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Info(string msg, object? ctx = null) { }
|
||||
|
||||
/// <summary>
|
||||
/// 记录错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常对象(可选)</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Error(string msg, Exception? ex = null, object? ctx = null) { }
|
||||
|
||||
/// <summary>
|
||||
/// 记录调试级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Debug(string msg, object? ctx = null) { }
|
||||
|
||||
/// <summary>
|
||||
/// 记录跟踪级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Trace(string msg, object? ctx = null) { }
|
||||
|
||||
/// <summary>
|
||||
/// 记录警告级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Warn(string msg, object? ctx = null) { }
|
||||
|
||||
/// <summary>
|
||||
/// 记录致命错误级别日志
|
||||
/// </summary>
|
||||
/// <param name="msg">日志消息</param>
|
||||
/// <param name="ex">相关异常对象(可选)</param>
|
||||
/// <param name="ctx">日志上下文信息(可选)</param>
|
||||
public void Fatal(string msg,Exception? ex = null, object? ctx = null) { }
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.model;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个接口,表示可以获取模型的架构组件。
|
||||
/// 该接口继承自IBelongToArchitecture,表明实现此接口的类型属于特定架构。
|
||||
/// </summary>
|
||||
public interface ICanGetModel : IBelongToArchitecture;
|
||||
@ -7,7 +7,7 @@ namespace GFramework.Core.model;
|
||||
/// <summary>
|
||||
/// 模型接口,定义了模型的基本行为和功能
|
||||
/// </summary>
|
||||
public interface IModel : ICanSetArchitecture, ICanGetUtility, ICanSendEvent
|
||||
public interface IModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化模型
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.query;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个可以发送查询的接口契约
|
||||
/// 该接口继承自IBelongToArchitecture,表示实现此接口的类型属于某个架构组件
|
||||
/// </summary>
|
||||
public interface ICanSendQuery : IBelongToArchitecture;
|
||||
@ -8,8 +8,7 @@ namespace GFramework.Core.query;
|
||||
/// 查询接口,定义了执行查询操作的契约
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||
public interface IQuery<out TResult> : ICanSetArchitecture, ICanGetModel, ICanGetSystem,
|
||||
ICanSendQuery
|
||||
public interface IQuery<out TResult>
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行查询操作并返回结果
|
||||
|
||||
29
GFramework.Core/rule/ContextAwareBase.cs
Normal file
29
GFramework.Core/rule/ContextAwareBase.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using GFramework.Core.architecture;
|
||||
|
||||
namespace GFramework.Core.rule;
|
||||
|
||||
/// <summary>
|
||||
/// 上下文感知基类,实现了IContextAware接口,为需要感知架构上下文的类提供基础实现
|
||||
/// </summary>
|
||||
public abstract class ContextAwareBase : IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前实例的架构上下文
|
||||
/// </summary>
|
||||
protected IArchitectureContext Context { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 设置架构上下文的实现方法,由框架调用
|
||||
/// </summary>
|
||||
/// <param name="context">要设置的架构上下文实例</param>
|
||||
void IContextAware.SetContext(IArchitectureContext context)
|
||||
{
|
||||
Context = context;
|
||||
OnContextReady();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当上下文准备就绪时调用的虚方法,子类可以重写此方法来执行上下文相关的初始化逻辑
|
||||
/// </summary>
|
||||
protected virtual void OnContextReady() { }
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
using GFramework.Core.architecture;
|
||||
|
||||
namespace GFramework.Core.rule;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个接口,用于标识某个对象属于特定的架构体系。
|
||||
/// 实现此接口的对象可以通过GetArchitecture方法获取其所属的架构实例。
|
||||
/// </summary>
|
||||
public interface IBelongToArchitecture
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前对象所属的架构实例。
|
||||
/// </summary>
|
||||
/// <returns>返回实现IArchitecture接口的架构实例</returns>
|
||||
IArchitecture GetArchitecture();
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
using GFramework.Core.architecture;
|
||||
|
||||
namespace GFramework.Core.rule;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个接口,用于设置架构实例
|
||||
/// </summary>
|
||||
public interface ICanSetArchitecture
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要设置的架构实例</param>
|
||||
void SetArchitecture(IArchitecture architecture);
|
||||
}
|
||||
15
GFramework.Core/rule/IContextAware.cs
Normal file
15
GFramework.Core/rule/IContextAware.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using GFramework.Core.architecture;
|
||||
|
||||
namespace GFramework.Core.rule;
|
||||
|
||||
/// <summary>
|
||||
/// 上下文感知接口,用于为实现该接口的类提供设置架构上下文的能力
|
||||
/// </summary>
|
||||
public interface IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置架构上下文
|
||||
/// </summary>
|
||||
/// <param name="context">架构上下文对象,用于提供架构级别的服务和功能访问</param>
|
||||
void SetContext(IArchitectureContext context);
|
||||
}
|
||||
@ -8,39 +8,21 @@ namespace GFramework.Core.system;
|
||||
/// 抽象系统基类,实现系统接口的基本功能
|
||||
/// 提供架构关联、初始化和销毁机制
|
||||
/// </summary>
|
||||
public abstract class AbstractSystem : ISystem
|
||||
public abstract class AbstractSystem : ContextAwareBase, ISystem
|
||||
{
|
||||
private IArchitecture _mArchitecture;
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前系统所属的架构实例
|
||||
/// </summary>
|
||||
/// <returns>返回系统关联的架构对象</returns>
|
||||
IArchitecture IBelongToArchitecture.GetArchitecture()
|
||||
{
|
||||
return _mArchitecture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置系统所属的架构实例
|
||||
/// </summary>
|
||||
/// <param name="architecture">要关联的架构对象</param>
|
||||
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
|
||||
{
|
||||
_mArchitecture = architecture;
|
||||
}
|
||||
private ILogger _logger = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 系统初始化方法,调用抽象初始化方法
|
||||
/// </summary>
|
||||
void ISystem.Init()
|
||||
{
|
||||
var logger = Log.CreateLogger("System");
|
||||
logger.Debug($"Initializing system: {GetType().Name}");
|
||||
|
||||
_logger = Context.Logger;
|
||||
_logger.Debug($"Initializing system: {GetType().Name}");
|
||||
|
||||
OnInit();
|
||||
|
||||
logger.Info($"System initialized: {GetType().Name}");
|
||||
|
||||
_logger.Info($"System initialized: {GetType().Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -48,21 +30,22 @@ public abstract class AbstractSystem : ISystem
|
||||
/// </summary>
|
||||
void ISystem.Destroy()
|
||||
{
|
||||
var logger = Log.CreateLogger("System");
|
||||
logger.Debug($"Destroying system: {GetType().Name}");
|
||||
|
||||
_logger.Debug($"Destroying system: {GetType().Name}");
|
||||
|
||||
OnDestroy();
|
||||
|
||||
logger.Info($"System destroyed: {GetType().Name}");
|
||||
|
||||
_logger.Info($"System destroyed: {GetType().Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抽象初始化方法,由子类实现具体的初始化逻辑
|
||||
/// </summary>
|
||||
protected abstract void OnInit();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 抽象销毁方法,由子类实现具体的资源清理逻辑
|
||||
/// </summary>
|
||||
protected virtual void OnDestroy() { }
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.system;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个接口,表示可以获取系统的对象。
|
||||
/// 该接口继承自IBelongToArchitecture接口,用于标识那些属于系统架构并能够获取系统实例的类型。
|
||||
/// </summary>
|
||||
public interface ICanGetSystem : IBelongToArchitecture;
|
||||
@ -1,7 +1,4 @@
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.model;
|
||||
using GFramework.Core.rule;
|
||||
using GFramework.Core.utility;
|
||||
|
||||
namespace GFramework.Core.system;
|
||||
|
||||
@ -9,8 +6,7 @@ namespace GFramework.Core.system;
|
||||
/// 系统接口,定义了系统的基本行为和功能
|
||||
/// 该接口继承了多个框架相关的接口,提供了系统初始化和销毁能力
|
||||
/// </summary>
|
||||
public interface ISystem : ICanSetArchitecture, ICanGetModel, ICanGetUtility,
|
||||
ICanRegisterEvent, ICanSendEvent, ICanGetSystem
|
||||
public interface ISystem : IContextAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化系统
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
using GFramework.Core.rule;
|
||||
|
||||
namespace GFramework.Core.utility;
|
||||
|
||||
/// <summary>
|
||||
/// 定义一个接口,表示可以获取工具类的对象
|
||||
/// 该接口继承自IBelongToArchitecture,表明实现此接口的类型属于某个架构组件
|
||||
/// </summary>
|
||||
public interface ICanGetUtility : IBelongToArchitecture;
|
||||
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="entities\" />
|
||||
<Folder Include="logging\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GFramework.Core\GFramework.Core.csproj" />
|
||||
|
||||
17
GFramework.Godot/logging/GodotLogProvider.cs
Normal file
17
GFramework.Godot/logging/GodotLogProvider.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using GFramework.Core.logging;
|
||||
|
||||
namespace GFramework.Godot.logging;
|
||||
|
||||
/// <summary>
|
||||
/// Godot日志提供程序,实现ILogProvider接口,用于创建GodotLogger实例
|
||||
/// </summary>
|
||||
public sealed class GodotLogProvider : ILogProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建指定类别的日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别名称</param>
|
||||
/// <returns>返回GodotLogger实例</returns>
|
||||
public ILogger CreateLogger(string category)
|
||||
=> new GodotLogger(category);
|
||||
}
|
||||
@ -6,8 +6,11 @@ namespace GFramework.Godot.logging;
|
||||
/// <summary>
|
||||
/// Godot平台的日志记录器实现
|
||||
/// </summary>
|
||||
public sealed class GodotLogger : ILog
|
||||
public sealed class GodotLogger : ILogger
|
||||
{
|
||||
private readonly string _category;
|
||||
|
||||
public GodotLogger(string category) => _category = category;
|
||||
/// <summary>
|
||||
/// 记录日志消息到Godot控制台
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user