From 5087db9f21d0062a51b7e9b5739b7098918d1570 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Tue, 23 Dec 2025 13:38:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(logging):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=B3=BB=E7=BB=9F=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为ConsoleLogger中的_writer添加空值检查操作符 - 移除LogConfig中的未使用using语句 - 为LoggerFactory添加方法注释文档 - 调整ConsoleLogger默认构造函数参数 - 优化日志系统的代码结构和可读性 - [no tag] --- GFramework.Core/logging/ConsoleLogger.cs | 4 ++-- GFramework.Core/logging/Log.cs | 4 ++-- GFramework.Core/logging/LogConfig.cs | 6 +----- GFramework.Core/logging/LoggerFactory.cs | 12 +++++++++++- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/GFramework.Core/logging/ConsoleLogger.cs b/GFramework.Core/logging/ConsoleLogger.cs index fdd74b8..6eb0283 100644 --- a/GFramework.Core/logging/ConsoleLogger.cs +++ b/GFramework.Core/logging/ConsoleLogger.cs @@ -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 { diff --git a/GFramework.Core/logging/Log.cs b/GFramework.Core/logging/Log.cs index 48b93fe..4f0c9cd 100644 --- a/GFramework.Core/logging/Log.cs +++ b/GFramework.Core/logging/Log.cs @@ -11,7 +11,7 @@ public static class Log /// /// 获取当前的日志记录器实例 /// - public static ILog Instance { get; private set; } = new ConsoleLogger(null, LogLevel.Info); + public static ILog Instance { get; private set; } = new ConsoleLogger(); /// /// 获取当前的日志配置 @@ -24,7 +24,7 @@ public static class Log /// 要设置的日志记录器,如果为 null 则使用默认的ConsoleLogger public static void SetLogger(ILog? logger) { - Instance = logger ?? new ConsoleLogger(null, LogLevel.Info); + Instance = logger ?? new ConsoleLogger(); } /// diff --git a/GFramework.Core/logging/LogConfig.cs b/GFramework.Core/logging/LogConfig.cs index a96c70e..e4f28cb 100644 --- a/GFramework.Core/logging/LogConfig.cs +++ b/GFramework.Core/logging/LogConfig.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.IO; - namespace GFramework.Core.logging; /// @@ -27,7 +23,7 @@ public sealed class LogConfig /// /// 获取或设置是否启用文件输出(默认为false) /// - public bool EnableFile { get; set; } = false; + public bool EnableFile { get; set; } /// /// 获取或设置日志文件路径(当EnableFile为true时使用) diff --git a/GFramework.Core/logging/LoggerFactory.cs b/GFramework.Core/logging/LoggerFactory.cs index 2521060..afe12c5 100644 --- a/GFramework.Core/logging/LoggerFactory.cs +++ b/GFramework.Core/logging/LoggerFactory.cs @@ -42,7 +42,11 @@ public class LoggerFactory : ILoggerFactory { return Create("Global"); } - + /// + /// 创建日志记录器 + /// + /// 日志类别 + /// 日志记录器实例 private ILog CreateLogger(string category) { var level = _config.GetCategoryLevel(category); @@ -57,6 +61,11 @@ public class LoggerFactory : ILoggerFactory return consoleLogger; } + /// + /// 创建文件日志记录器 + /// + /// 日志类别 + /// 文件日志记录器实例 private ILog CreateFileLogger(string category) { try @@ -77,6 +86,7 @@ public class LoggerFactory : ILoggerFactory } } + /// /// 组合日志记录器,将日志同时输出到多个目标 ///