refactor(logging): 重构日志系统抽象和目标框架配置

- 将 RootLoggerName 常量从接口移至具体实现类 AbstractLogger
- 在 AbstractLogger 中添加 IsEnabledForLevel 方法的完整实现
- 更新 ILogger 接口移除 IsEnabledForLevel 的默认实现
- 统一所有项目的目标框架为 netstandard2.0
- 修正命名空间引用确保日志相关类型正确导入
- 添加详细的 XML 文档注释提升代码可读性
- 优化项目构建属性配置支持更高版本的语言特性
This commit is contained in:
GwWuYou 2025-12-28 11:45:24 +08:00
parent 4172952b11
commit 8496bf43bb
9 changed files with 61 additions and 41 deletions

View File

@ -1,12 +1,16 @@
<Project> <Project>
<!-- 配置项目构建属性包括目标框架、CI构建设置、源代码嵌入和语言版本 --> <!-- import parent: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build -->
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources> <EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--
we use a higher version than supported by the target framework to have nullable types and other nice features
(a lot of features get polyfilled by Meziantou.Polyfill)
however we need to be careful with the available features!
-->
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
</PropertyGroup> </PropertyGroup>
<!-- 引用代码分析和Polyfill功能的NuGet包用于增强代码质量和兼容性 -->
<ItemGroup> <ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264"> <PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -1,18 +1,10 @@
using System; namespace GFramework.Core.Abstractions.logging;
using GFramework.Core.logging;
namespace GFramework.Core.Abstractions.logging;
/// <summary> /// <summary>
/// 定义日志记录接口,提供日志记录和级别检查功能 /// 定义日志记录接口,提供日志记录和级别检查功能
/// </summary> /// </summary>
public interface ILogger public interface ILogger
{ {
/// <summary>
/// 根日志记录器的名称常量
/// </summary>
public const string RootLoggerName = "ROOT";
/// <summary> /// <summary>
/// 获取日志记录器的名称 /// 获取日志记录器的名称
/// </summary> /// </summary>
@ -62,19 +54,7 @@ public interface ILogger
/// </summary> /// </summary>
/// <param name="level">要检查的日志级别</param> /// <param name="level">要检查的日志级别</param>
/// <returns>如果指定的日志级别已启用则返回true否则返回false</returns> /// <returns>如果指定的日志级别已启用则返回true否则返回false</returns>
bool IsEnabledForLevel(LogLevel level) bool IsEnabledForLevel(LogLevel level);
{
return level switch
{
LogLevel.Trace => IsTraceEnabled(),
LogLevel.Debug => IsDebugEnabled(),
LogLevel.Info => IsInfoEnabled(),
LogLevel.Warning => IsWarnEnabled(),
LogLevel.Error => IsErrorEnabled(),
LogLevel.Fatal => IsFatalEnabled(),
_ => throw new ArgumentException($"Level [{level}] not recognized.", nameof(level))
};
}
#endregion #endregion

View File

@ -1,4 +1,4 @@
namespace GFramework.Core.logging; namespace GFramework.Core.Abstractions.logging;
/// <summary> /// <summary>
/// 定义日志级别的枚举,用于标识不同严重程度的日志消息 /// 定义日志级别的枚举,用于标识不同严重程度的日志消息

View File

@ -1,6 +1,4 @@
using GFramework.Core.Abstractions.logging; namespace GFramework.Core.logging;
namespace GFramework.Core.logging;
/// <summary> /// <summary>
/// 日志抽象基类,封装日志级别判断、格式化与异常处理逻辑。 /// 日志抽象基类,封装日志级别判断、格式化与异常处理逻辑。
@ -10,7 +8,12 @@ public abstract class AbstractLogger(
string? name = null, string? name = null,
LogLevel minLevel = LogLevel.Info) : ILogger LogLevel minLevel = LogLevel.Info) : ILogger
{ {
private readonly string _name = name ?? ILogger.RootLoggerName; /// <summary>
/// 根日志记录器的名称常量
/// </summary>
public const string RootLoggerName = "ROOT";
private readonly string _name = name ?? RootLoggerName;
#region Metadata #region Metadata
@ -99,6 +102,27 @@ public abstract class AbstractLogger(
return IsEnabled(LogLevel.Fatal); return IsEnabled(LogLevel.Fatal);
} }
/// <summary>
/// 检查指定日志级别是否启用
/// </summary>
/// <param name="level">要检查的日志级别</param>
/// <returns>如果指定级别启用返回true否则返回false</returns>
/// <exception cref="ArgumentException">当传入的日志级别不被识别时抛出</exception>
public bool IsEnabledForLevel(LogLevel level)
{
// 根据不同的日志级别调用对应的检查方法
return level switch
{
LogLevel.Trace => IsTraceEnabled(),
LogLevel.Debug => IsDebugEnabled(),
LogLevel.Info => IsInfoEnabled(),
LogLevel.Warning => IsWarnEnabled(),
LogLevel.Error => IsErrorEnabled(),
LogLevel.Fatal => IsFatalEnabled(),
_ => throw new ArgumentException($"Level [{level}] not recognized.", nameof(level))
};
}
#endregion #endregion
#region Trace #region Trace

View File

@ -1,12 +1,16 @@
<Project> <Project>
<!-- 配置项目构建属性包括目标框架、CI构建设置、源代码嵌入和语言版本 --> <!-- import parent: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build -->
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources> <EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--
we use a higher version than supported by the target framework to have nullable types and other nice features
(a lot of features get polyfilled by Meziantou.Polyfill)
however we need to be careful with the available features!
-->
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
</PropertyGroup> </PropertyGroup>
<!-- 引用代码分析和Polyfill功能的NuGet包用于增强代码质量和兼容性 -->
<ItemGroup> <ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264"> <PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<PackageId>GeWuYou.GFramework.Godot.SourceGenerators</PackageId> <PackageId>GeWuYou.GFramework.Godot.SourceGenerators</PackageId>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<!-- 这是 Analyzer不是运行时库 --> <!-- 这是 Analyzer不是运行时库 -->
<IsRoslynAnalyzer>true</IsRoslynAnalyzer> <IsRoslynAnalyzer>true</IsRoslynAnalyzer>

View File

@ -1,12 +1,16 @@
<Project> <Project>
<!-- 配置项目构建属性包括目标框架、CI构建设置、源代码嵌入和语言版本 --> <!-- import parent: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build -->
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources> <EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--
we use a higher version than supported by the target framework to have nullable types and other nice features
(a lot of features get polyfilled by Meziantou.Polyfill)
however we need to be careful with the available features!
-->
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
</PropertyGroup> </PropertyGroup>
<!-- 引用代码分析和Polyfill功能的NuGet包用于增强代码质量和兼容性 -->
<ItemGroup> <ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264"> <PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -1,12 +1,16 @@
<Project> <Project>
<!-- 配置项目构建属性包括目标框架、CI构建设置、源代码嵌入和语言版本 --> <!-- import parent: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build -->
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources> <EmbedUntrackedSources>true</EmbedUntrackedSources>
<!--
we use a higher version than supported by the target framework to have nullable types and other nice features
(a lot of features get polyfilled by Meziantou.Polyfill)
however we need to be careful with the available features!
-->
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
</PropertyGroup> </PropertyGroup>
<!-- 引用代码分析和Polyfill功能的NuGet包用于增强代码质量和兼容性 -->
<ItemGroup> <ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.264"> <PackageReference Include="Meziantou.Analyzer" Version="2.0.264">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<PackageId>GeWuYou.GFramework.SourceGenerators</PackageId> <PackageId>GeWuYou.GFramework.SourceGenerators</PackageId>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks> <TargetFramework>netstandard2.0</TargetFramework>
<!-- 这是 Analyzer不是运行时库 --> <!-- 这是 Analyzer不是运行时库 -->
<IsRoslynAnalyzer>true</IsRoslynAnalyzer> <IsRoslynAnalyzer>true</IsRoslynAnalyzer>