From 9104202af5a828b30c7638d3c02a18fcfe179bc1 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Tue, 23 Dec 2025 13:32:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(logging):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=B7=A5=E5=8E=82=E4=B8=AD=E7=9A=84=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=99=A8=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 CompositeLogger 类改为密封类并使用主构造函数 - 移除不必要的 category 参数传递 - 为 CompositeLogger 类添加详细的 XML 文档注释 - 调整代码格式和缩进保持一致性 - 优化 CompositeLogger 构造函数参数结构 --- GFramework.Core/logging/LoggerFactory.cs | 110 ++++++++++++++++------- 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/GFramework.Core/logging/LoggerFactory.cs b/GFramework.Core/logging/LoggerFactory.cs index 685e759..2521060 100644 --- a/GFramework.Core/logging/LoggerFactory.cs +++ b/GFramework.Core/logging/LoggerFactory.cs @@ -47,13 +47,13 @@ public class LoggerFactory : ILoggerFactory { var level = _config.GetCategoryLevel(category); var consoleLogger = new ConsoleLogger(category, level, null, _config.UseColors); - + if (_config.EnableFile && !string.IsNullOrEmpty(_config.LogFilePath)) { // 创建一个组合日志记录器,同时输出到控制台和文件 - return new CompositeLogger(category, level, consoleLogger, CreateFileLogger(category)); + return new CompositeLogger(level, consoleLogger, CreateFileLogger(category)); } - + return consoleLogger; } @@ -80,49 +80,95 @@ public class LoggerFactory : ILoggerFactory /// /// 组合日志记录器,将日志同时输出到多个目标 /// - private class CompositeLogger : ILog + /// 最小日志级别,低于此级别的日志将被忽略 + /// 控制台日志记录器 + /// 文件日志记录器 + private sealed class CompositeLogger( + LogLevel minLevel, + ILog consoleLogger, + ILog fileLogger) + : ILog { - private readonly string _category; - private readonly LogLevel _minLevel; - private readonly ILog _consoleLogger; - private readonly ILog _fileLogger; - - public CompositeLogger(string category, LogLevel minLevel, ILog consoleLogger, ILog fileLogger) - { - _category = category; - _minLevel = minLevel; - _consoleLogger = consoleLogger; - _fileLogger = fileLogger; - } - - public void Log(LogLevel level, string message, Exception? exception = null, object? context = null) + /// + /// 记录日志消息到所有配置的日志目标 + /// + /// 日志级别 + /// 日志消息 + /// 相关异常(可选) + /// 上下文信息(可选) + 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); + consoleLogger.Log(level, message, exception, context); + fileLogger.Log(level, message, exception, context); } - public bool IsEnabled(LogLevel level) => level >= _minLevel; - - // 快捷方法实现 - public void Info(string msg, object? ctx = null) - => Log(LogLevel.Info, msg, null, ctx); - - public void Error(string msg, Exception? ex = null, object? ctx = null) - => Log(LogLevel.Error, msg, ex, ctx); - - public void Debug(string msg, object? ctx = null) - => Log(LogLevel.Debug, msg, null, ctx); + /// + /// 检查指定的日志级别是否已启用 + /// + /// 要检查的日志级别 + /// 如果级别已启用则返回true,否则返回false + public bool IsEnabled(LogLevel level) + { + return level >= minLevel; + } + // 快捷方法 + /// + /// 记录跟踪级别的日志消息 + /// + /// 日志消息 + /// 上下文信息(可选) public void Trace(string msg, object? ctx = null) => Log(LogLevel.Trace, msg, null, ctx); + /// + /// 记录调试级别的日志消息 + /// + /// 日志消息 + /// 上下文信息(可选) + public void Debug(string msg, object? ctx = null) + => Log(LogLevel.Debug, msg, null, ctx); + + /// + /// 记录信息级别的日志消息 + /// + /// 日志消息 + /// 上下文信息(可选) + public void Info(string msg, object? ctx = null) + => Log(LogLevel.Info, msg, null, ctx); + + /// + /// 记录警告级别的日志消息 + /// + /// 日志消息 + /// 上下文信息(可选) public void Warn(string msg, object? ctx = null) => Log(LogLevel.Warning, msg, null, ctx); - public void Fatal(string msg,Exception? ex = null, object? ctx = null) + /// + /// 记录错误级别的日志消息 + /// + /// 日志消息 + /// 相关异常(可选) + /// 上下文信息(可选) + public void Error(string msg, Exception? ex = null, object? ctx = null) + => Log(LogLevel.Error, msg, ex, ctx); + + /// + /// 记录致命错误级别的日志消息 + /// + /// 日志消息 + /// 相关异常(可选) + /// 上下文信息(可选) + public void Fatal(string msg, Exception? ex = null, object? ctx = null) => Log(LogLevel.Fatal, msg, ex, ctx); } + } \ No newline at end of file