Skip to content

Commit 23f79aa

Browse files
committed
fix: 优化日志AOP功能
1 parent 3578c3d commit 23f79aa

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

framework/src/Bing.Logs/Bing/Logs/Aspects/ErrorLogAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
2424
}
2525
catch (Exception ex)
2626
{
27-
log.Class(context.ServiceMethod.DeclaringType.FullName).Method(methodName).Exception(ex);
27+
log.Tag(methodName)
28+
.Class(context.ServiceMethod.DeclaringType.FullName)
29+
.Method(methodName)
30+
.Exception(ex);
2831
foreach (var parameter in context.GetParameters())
2932
parameter.AppendTo(log);
3033
log.Error();

framework/src/Bing.Logs/Bing/Logs/Aspects/LogAttributeBase.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
2222
return;
2323
ExecuteBefore(log, context, methodName);
2424
await next(context);
25-
ExecuteAfter(log, context, methodName);
25+
await ExecuteAfter(log, context, methodName);
2626
}
2727

2828
/// <summary>
@@ -45,7 +45,9 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
4545
/// <param name="methodName">方法名</param>
4646
private void ExecuteBefore(ILog log, AspectContext context, string methodName)
4747
{
48-
log.Caption($"{context.ServiceMethod.Name}方法执行前")
48+
log
49+
.Tag(context.ServiceMethod.Name)
50+
.Caption($"{context.ServiceMethod.Name}方法执行前")
4951
.Class(context.ServiceMethod.DeclaringType.FullName)
5052
.Method(methodName);
5153
foreach (var parameter in context.GetParameters())
@@ -65,12 +67,17 @@ private void ExecuteBefore(ILog log, AspectContext context, string methodName)
6567
/// <param name="log">日志操作</param>
6668
/// <param name="context">Aspect上下文</param>
6769
/// <param name="methodName">方法名</param>
68-
private void ExecuteAfter(ILog log, AspectContext context, string methodName)
70+
private async Task ExecuteAfter(ILog log, AspectContext context, string methodName)
6971
{
70-
var parameter = context.GetReturnParameter();
72+
if (context.ServiceMethod.ReturnType == typeof(Task) ||
73+
context.ServiceMethod.ReturnType == typeof(void) ||
74+
context.ServiceMethod.ReturnType == typeof(ValueTask))
75+
return;
76+
var returnValue = context.IsAsync() ? await context.UnwrapAsyncReturnValue() : context.ReturnValue;
77+
var returnType = returnValue.GetType().FullName;
7178
log.Caption($"{context.ServiceMethod.Name}方法执行后")
7279
.Method(methodName)
73-
.Content($"返回类型: {parameter.ParameterInfo.ParameterType.FullName},返回值: {parameter.Value.SafeString()}");
80+
.Content($"返回类型: {returnType}, 返回值: {returnValue.SafeString()}");
7481
WriteLog(log);
7582
}
7683
}

0 commit comments

Comments
 (0)