mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 17:11:44 +08:00
- 清理 GFramework.Core 与 GFramework.Cqrs 中的大量低风险 Meziantou 警告 - 修复 GFramework.Godot 运行时中的 ConfigureAwait、StringComparison 与参数校验告警 - 调整 Core SourceGenerators 中的字符串比较、文件命名与局部长方法问题 - 拆分部分配置与缓存辅助类型文件以消除 file/type mismatch 告警 - 更新 warning reduction 跟踪与执行记录,保留下一批结构性告警的恢复点
130 lines
4.7 KiB
C#
130 lines
4.7 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using GFramework.Core.Abstractions.Logging;
|
|
using GFramework.Core.Logging.Appenders;
|
|
using GFramework.Core.Logging.Filters;
|
|
using GFramework.Core.Logging.Formatters;
|
|
|
|
namespace GFramework.Core.Logging;
|
|
|
|
/// <summary>
|
|
/// 日志配置加载器
|
|
/// </summary>
|
|
public static class LoggingConfigurationLoader
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
ReadCommentHandling = JsonCommentHandling.Skip,
|
|
AllowTrailingCommas = true,
|
|
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, allowIntegerValues: true) }
|
|
};
|
|
|
|
/// <summary>
|
|
/// 从 JSON 文件加载配置
|
|
/// </summary>
|
|
/// <param name="filePath">配置文件路径</param>
|
|
/// <returns>日志配置对象</returns>
|
|
public static LoggingConfiguration LoadFromJson(string filePath)
|
|
{
|
|
if (!File.Exists(filePath))
|
|
throw new FileNotFoundException($"Configuration file not found: {filePath}");
|
|
|
|
var json = File.ReadAllText(filePath);
|
|
var config = JsonSerializer.Deserialize<LoggingConfiguration>(json, JsonOptions);
|
|
|
|
return config ?? throw new InvalidOperationException("Failed to deserialize configuration.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从 JSON 字符串加载配置
|
|
/// </summary>
|
|
/// <param name="json">JSON 字符串</param>
|
|
/// <returns>日志配置对象</returns>
|
|
public static LoggingConfiguration LoadFromJsonString(string json)
|
|
{
|
|
var config = JsonSerializer.Deserialize<LoggingConfiguration>(json, JsonOptions);
|
|
return config ?? throw new InvalidOperationException("Failed to deserialize configuration.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据配置创建 Logger 工厂
|
|
/// </summary>
|
|
/// <param name="config">日志配置</param>
|
|
/// <returns>Logger 工厂</returns>
|
|
public static ILoggerFactory CreateFactory(LoggingConfiguration config)
|
|
{
|
|
return new ConfigurableLoggerFactory(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据配置创建 Appender
|
|
/// </summary>
|
|
internal static ILogAppender CreateAppender(AppenderConfiguration config)
|
|
{
|
|
var formatter = CreateFormatter(config.Formatter);
|
|
var filter = config.Filter != null ? CreateFilter(config.Filter) : null;
|
|
|
|
return config.Type.ToLowerInvariant() switch
|
|
{
|
|
"console" => new ConsoleAppender(formatter, useColors: config.UseColors, filter: filter),
|
|
|
|
"file" => new FileAppender(
|
|
config.FilePath ?? throw new InvalidOperationException("FilePath is required for File appender."),
|
|
formatter,
|
|
filter),
|
|
|
|
"rollingfile" => new RollingFileAppender(
|
|
config.FilePath ??
|
|
throw new InvalidOperationException("FilePath is required for RollingFile appender."),
|
|
config.MaxFileSize,
|
|
config.MaxFileCount,
|
|
formatter,
|
|
filter),
|
|
|
|
"async" => new AsyncLogAppender(
|
|
CreateAppender(config.InnerAppender ??
|
|
throw new InvalidOperationException("InnerAppender is required for Async appender.")),
|
|
config.BufferSize),
|
|
|
|
_ => throw new NotSupportedException($"Appender type '{config.Type}' is not supported.")
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据配置创建格式化器
|
|
/// </summary>
|
|
internal static ILogFormatter CreateFormatter(string formatterType)
|
|
{
|
|
return formatterType.ToLowerInvariant() switch
|
|
{
|
|
"default" => new DefaultLogFormatter(),
|
|
"json" => new JsonLogFormatter(),
|
|
_ => throw new NotSupportedException($"Formatter type '{formatterType}' is not supported.")
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据配置创建过滤器
|
|
/// </summary>
|
|
internal static ILogFilter CreateFilter(FilterConfiguration config)
|
|
{
|
|
return config.Type.ToLowerInvariant() switch
|
|
{
|
|
"loglevel" => new LogLevelFilter(
|
|
config.MinLevel ?? throw new InvalidOperationException("MinLevel is required for LogLevel filter.")),
|
|
|
|
"namespace" => new NamespaceFilter(
|
|
config.Namespaces?.ToArray() ??
|
|
throw new InvalidOperationException("Namespaces is required for Namespace filter.")),
|
|
|
|
"composite" => new CompositeFilter(
|
|
config.Filters?.Select(CreateFilter).ToArray() ??
|
|
throw new InvalidOperationException("Filters is required for Composite filter.")),
|
|
|
|
_ => throw new NotSupportedException($"Filter type '{config.Type}' is not supported.")
|
|
};
|
|
}
|
|
}
|