using Castle.DynamicProxy; using Logger; using System.Diagnostics; public class LoggingInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { string className = invocation.TargetType.Name; string methodName = invocation.Method.Name; if (methodName== "ConnectAsync"|| methodName == "Close") { invocation.Proceed(); return; } LoggerHelper.Info($"调用 {className}.{methodName}() 开始"); var sw = Stopwatch.StartNew(); try { invocation.Proceed(); // 如果是异步方法 if (invocation.Method.ReturnType == typeof(Task) || (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))) { // 异步方法处理 _= InterceptAsync(invocation, sw); } sw.Stop(); LoggerHelper.Info($"调用 {className}.{methodName}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } catch (Exception ex) { sw.Stop(); LoggerHelper.Error($"调用 {className}.{methodName}() 异常:{ex.Message}", ex.StackTrace); throw; } } private async Task InterceptAsync(IInvocation invocation, Stopwatch sw) { // 创建一个默认返回值,用于捕获异常时返回 object defaultReturnValue = null; try { // 获取返回类型 Type returnType = invocation.Method.ReturnType; // 如果返回类型是 Task,我们需要处理泛型类型 if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>)) { // 获取泛型参数类型(如 Task 中的 bool) Type resultType = returnType.GetGenericArguments()[0]; // 获取原始方法返回的任务 var task = (Task)invocation.ReturnValue; try { // 异步等待并获取结果 await task; // 执行完任务后,获取结果类型并记录日志 if (resultType == typeof(bool)) { bool result = (bool)((dynamic)task).Result; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: {result}"); } else if (resultType == typeof(bool[])) { bool[] result = (bool[])((dynamic)task).Result; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(ushort[])) { ushort[] result = (ushort[])((dynamic)task).Result; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(string[])) { string[] result = (string[])((dynamic)task).Result; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(byte[])) { byte[] result = (byte[])((dynamic)task).Result; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{BitConverter.ToString(result)}]"); } else if (resultType == typeof(void)) { LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } else { LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: {((dynamic)task).Result}"); } } catch (Exception ex) { sw.Stop(); LoggerHelper.Error($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); var exception = new Exception($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 时发生异常:{ex.Message}"); invocation.ReturnValue = Task.FromException(exception); return; } } else { // 如果是普通的 Task,无需处理泛型返回值 await (Task)invocation.ReturnValue; LoggerHelper.Success($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } } catch (Exception ex) { sw.Stop(); LoggerHelper.Error($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); } } }