mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 11:14:30 +08:00
refactor(logging): 重构日志工厂中的组合日志记录器实现
- 将 CompositeLogger 类改为密封类并使用主构造函数 - 移除不必要的 category 参数传递 - 为 CompositeLogger 类添加详细的 XML 文档注释 - 调整代码格式和缩进保持一致性 - 优化 CompositeLogger 构造函数参数结构
This commit is contained in:
parent
9ed57324c7
commit
9104202af5
@ -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
|
||||
/// <summary>
|
||||
/// 组合日志记录器,将日志同时输出到多个目标
|
||||
/// </summary>
|
||||
private class CompositeLogger : ILog
|
||||
/// <param name="minLevel">最小日志级别,低于此级别的日志将被忽略</param>
|
||||
/// <param name="consoleLogger">控制台日志记录器</param>
|
||||
/// <param name="fileLogger">文件日志记录器</param>
|
||||
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)
|
||||
/// <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);
|
||||
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);
|
||||
/// <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);
|
||||
|
||||
public void Fatal(string msg,Exception? ex = null, 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)
|
||||
=> 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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user