mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 04:06:48 +08:00
refactor(logging): 优化日志生成器中的类别表达式处理
- 将类别处理逻辑重构为条件判断,支持用户显式指定字符串或默认使用 nameof - 添加字符串空值检查,避免空字符串或空白字符的无效输入 - 使用 nameof 表达式作为默认类别值,提高代码可维护性 - 统一类别表达式的字符串格式化处理
This commit is contained in:
parent
ab5ea42350
commit
7d2fbc32da
@ -93,11 +93,21 @@ namespace GFramework.Generator.generator.logging
|
|||||||
var attr = classSymbol.GetAttributes()
|
var attr = classSymbol.GetAttributes()
|
||||||
.First(a => a.AttributeClass!.ToDisplayString() == AttributeMetadataName);
|
.First(a => a.AttributeClass!.ToDisplayString() == AttributeMetadataName);
|
||||||
|
|
||||||
var category =
|
string categoryExpr;
|
||||||
attr.ConstructorArguments.Length > 0 &&
|
|
||||||
attr.ConstructorArguments[0].Value is string s
|
if (attr.ConstructorArguments.Length > 0 &&
|
||||||
? s
|
attr.ConstructorArguments[0].Value is string s &&
|
||||||
: className;
|
!string.IsNullOrWhiteSpace(s))
|
||||||
|
{
|
||||||
|
// 用户显式指定字符串
|
||||||
|
categoryExpr = $"\"{s}\"";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 默认使用 nameof(Class)
|
||||||
|
categoryExpr = $"nameof({className})";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var fieldName = GetNamedArg(attr, "FieldName", "_log");
|
var fieldName = GetNamedArg(attr, "FieldName", "_log");
|
||||||
var access = GetNamedArg(attr, "AccessModifier", "private");
|
var access = GetNamedArg(attr, "AccessModifier", "private");
|
||||||
@ -119,7 +129,7 @@ namespace GFramework.Generator.generator.logging
|
|||||||
sb.AppendLine(" {");
|
sb.AppendLine(" {");
|
||||||
sb.AppendLine(
|
sb.AppendLine(
|
||||||
$" {access} {staticKeyword}readonly ILog {fieldName} =" +
|
$" {access} {staticKeyword}readonly ILog {fieldName} =" +
|
||||||
$" Log.CreateLogger(\"{category}\");");
|
$" Log.CreateLogger(\"{categoryExpr}\");");
|
||||||
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
||||||
|
|
||||||
if (ns is not null)
|
if (ns is not null)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user