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