mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(logging): 优化日志系统实现
- 为ConsoleLogger中的_writer添加空值检查操作符 - 移除LogConfig中的未使用using语句 - 为LoggerFactory添加方法注释文档 - 调整ConsoleLogger默认构造函数参数 - 优化日志系统的代码结构和可读性 - [no tag]
This commit is contained in:
parent
64543be0b5
commit
5087db9f21
@ -55,7 +55,7 @@ public sealed class ConsoleLogger : ILog
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteLine(logMessage);
|
||||
_writer!.WriteLine(logMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ public sealed class ConsoleLogger : ILog
|
||||
try
|
||||
{
|
||||
Console.ForegroundColor = GetColor(level);
|
||||
_writer.WriteLine(message);
|
||||
_writer!.WriteLine(message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@ -11,7 +11,7 @@ public static class Log
|
||||
/// <summary>
|
||||
/// 获取当前的日志记录器实例
|
||||
/// </summary>
|
||||
public static ILog Instance { get; private set; } = new ConsoleLogger(null, LogLevel.Info);
|
||||
public static ILog Instance { get; private set; } = new ConsoleLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前的日志配置
|
||||
@ -24,7 +24,7 @@ public static class Log
|
||||
/// <param name="logger">要设置的日志记录器,如果为 null 则使用默认的ConsoleLogger</param>
|
||||
public static void SetLogger(ILog? logger)
|
||||
{
|
||||
Instance = logger ?? new ConsoleLogger(null, LogLevel.Info);
|
||||
Instance = logger ?? new ConsoleLogger();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -1,7 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace GFramework.Core.logging;
|
||||
|
||||
/// <summary>
|
||||
@ -27,7 +23,7 @@ public sealed class LogConfig
|
||||
/// <summary>
|
||||
/// 获取或设置是否启用文件输出(默认为false)
|
||||
/// </summary>
|
||||
public bool EnableFile { get; set; } = false;
|
||||
public bool EnableFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置日志文件路径(当EnableFile为true时使用)
|
||||
|
||||
@ -42,7 +42,11 @@ public class LoggerFactory : ILoggerFactory
|
||||
{
|
||||
return Create("Global");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>日志记录器实例</returns>
|
||||
private ILog CreateLogger(string category)
|
||||
{
|
||||
var level = _config.GetCategoryLevel(category);
|
||||
@ -57,6 +61,11 @@ public class LoggerFactory : ILoggerFactory
|
||||
return consoleLogger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建文件日志记录器
|
||||
/// </summary>
|
||||
/// <param name="category">日志类别</param>
|
||||
/// <returns>文件日志记录器实例</returns>
|
||||
private ILog CreateFileLogger(string category)
|
||||
{
|
||||
try
|
||||
@ -77,6 +86,7 @@ public class LoggerFactory : ILoggerFactory
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 组合日志记录器,将日志同时输出到多个目标
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user