mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 20:34:29 +08:00
refactor(logging): 优化日志生成器属性处理逻辑
- 将 LogAttribute 的 Category 属性改为可设置 - 添加了无参构造函数支持 - 移除了部分无效的诊断检查代码 - 为 LoggerGenerator 添加了可空引用类型支持 - 使用 switch 表达式简化参数解析逻辑 - 优化了命名参数获取方法的实现
This commit is contained in:
parent
73c474c37a
commit
fb61ae3198
@ -10,7 +10,7 @@ namespace GFramework.Generator.Attributes.generator.logging;
|
|||||||
public sealed class LogAttribute : Attribute
|
public sealed class LogAttribute : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>日志分类名(默认使用类名)</summary>
|
/// <summary>日志分类名(默认使用类名)</summary>
|
||||||
public string? Category { get; }
|
public string? Category { get; set; }
|
||||||
|
|
||||||
/// <summary>生成字段名</summary>
|
/// <summary>生成字段名</summary>
|
||||||
public string FieldName { get; set; } = "_log";
|
public string FieldName { get; set; } = "_log";
|
||||||
@ -20,12 +20,12 @@ public sealed class LogAttribute : Attribute
|
|||||||
|
|
||||||
/// <summary>访问修饰符</summary>
|
/// <summary>访问修饰符</summary>
|
||||||
public string AccessModifier { get; set; } = "private";
|
public string AccessModifier { get; set; } = "private";
|
||||||
|
public LogAttribute() { }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化 LogAttribute 类的新实例
|
/// 初始化 LogAttribute 类的新实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="category">日志分类名,默认使用类名</param>
|
/// <param name="category">日志分类名,默认使用类名</param>
|
||||||
public LogAttribute(string? category = null)
|
public LogAttribute(string? category)
|
||||||
{
|
{
|
||||||
Category = category;
|
Category = category;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using System;
|
#nullable enable
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
@ -42,7 +43,6 @@ namespace GFramework.Generator.generator.logging
|
|||||||
// 2. 生成代码
|
// 2. 生成代码
|
||||||
context.RegisterSourceOutput(targets, (spc, pair) =>
|
context.RegisterSourceOutput(targets, (spc, pair) =>
|
||||||
{
|
{
|
||||||
// === 关键修复:添加 try-catch 块 ===
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var classDecl = pair.ClassDecl;
|
var classDecl = pair.ClassDecl;
|
||||||
@ -55,7 +55,6 @@ namespace GFramework.Generator.generator.logging
|
|||||||
// 检查 partial
|
// 检查 partial
|
||||||
if (!classDecl.Modifiers.Any(SyntaxKind.PartialKeyword))
|
if (!classDecl.Modifiers.Any(SyntaxKind.PartialKeyword))
|
||||||
{
|
{
|
||||||
// 如果 Diagnostics 类有问题,这里可能会崩,先注释掉或确保该类存在
|
|
||||||
spc.ReportDiagnostic(Diagnostic.Create(
|
spc.ReportDiagnostic(Diagnostic.Create(
|
||||||
Diagnostics.MustBePartial,
|
Diagnostics.MustBePartial,
|
||||||
classDecl.Identifier.GetLocation(),
|
classDecl.Identifier.GetLocation(),
|
||||||
@ -79,7 +78,7 @@ namespace GFramework.Generator.generator.logging
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static AttributeData GetAttribute(INamedTypeSymbol classSymbol)
|
private static AttributeData? GetAttribute(INamedTypeSymbol classSymbol)
|
||||||
{
|
{
|
||||||
return classSymbol.GetAttributes().FirstOrDefault(a =>
|
return classSymbol.GetAttributes().FirstOrDefault(a =>
|
||||||
{
|
{
|
||||||
@ -101,34 +100,21 @@ namespace GFramework.Generator.generator.logging
|
|||||||
var className = classSymbol.Name;
|
var className = classSymbol.Name;
|
||||||
|
|
||||||
// === 解析 Category ===
|
// === 解析 Category ===
|
||||||
string category = className; // 默认使用类名
|
var category = className; // 默认使用类名
|
||||||
|
|
||||||
// 检查是否有构造函数参数
|
// 检查是否有构造函数参数
|
||||||
if (attr.ConstructorArguments.Length > 0)
|
if (attr.ConstructorArguments.Length > 0)
|
||||||
{
|
{
|
||||||
var argValue = attr.ConstructorArguments[0].Value;
|
var argValue = attr.ConstructorArguments[0].Value;
|
||||||
|
|
||||||
// 情况 1: 参数存在,但值为 null (例如 [Log] 且构造函数有默认值 null)
|
category = argValue switch
|
||||||
if (argValue is null)
|
|
||||||
{
|
{
|
||||||
category = className;
|
// 情况 1: 参数存在,但值为 null (例如 [Log] 且构造函数有默认值 null)
|
||||||
}
|
null => className,
|
||||||
// 情况 2: 参数存在,且是有效的字符串 (例如 [Log("MyCategory")])
|
// 情况 2: 参数存在,且是有效的字符串 (例如 [Log("MyCategory")])
|
||||||
else if (argValue is string s && !string.IsNullOrWhiteSpace(s))
|
string s when !string.IsNullOrWhiteSpace(s) => s,
|
||||||
{
|
_ => $"{className}_InvalidArg"
|
||||||
category = s;
|
};
|
||||||
}
|
|
||||||
// 情况 3: 参数存在,但类型不对 (防御性编程)
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 这里可以选择报错,或者回退到默认值。
|
|
||||||
// 为了让用户知道写错了,保留报错逻辑是合理的。
|
|
||||||
// 注意:这里需要你有 ReportDiagnostic 的逻辑,如果没有,为了不中断生成,建议回退到默认值。
|
|
||||||
|
|
||||||
// 如果你在 Generate 方法里无法轻松调用 ReportDiagnostic,
|
|
||||||
// 建议直接忽略错误使用默认值,或者生成一段 #error 代码。
|
|
||||||
category = $"{className}_InvalidArg";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === 解析 Named Arguments (更加安全的获取方式) ===
|
// === 解析 Named Arguments (更加安全的获取方式) ===
|
||||||
@ -169,13 +155,7 @@ namespace GFramework.Generator.generator.logging
|
|||||||
|
|
||||||
private static object? GetNamedArg(AttributeData attr, string name)
|
private static object? GetNamedArg(AttributeData attr, string name)
|
||||||
{
|
{
|
||||||
foreach (var kv in attr.NamedArguments)
|
return (from kv in attr.NamedArguments where kv.Key == name select kv.Value.Value).FirstOrDefault();
|
||||||
{
|
|
||||||
if (kv.Key == name)
|
|
||||||
return kv.Value.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user