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; // 排除 TcpClient, SerialPort, Modbus 的 getter if (methodName.Contains("get_TcpClient") || methodName.Contains("get_SerialPort") || methodName.Contains("get_Modbus")) { invocation.Proceed(); // 不拦截 return; } LoggerHelper.InfoWithNotify($"调用 {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.InfoWithNotify($"调用 {className}.{methodName}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } catch (Exception ex) { sw.Stop(); LoggerHelper.ErrorWithNotify($"调用 {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.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: {result}"); } else if (resultType == typeof(bool[])) { bool[] result = (bool[])((dynamic)task).Result; LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(ushort[])) { ushort[] result = (ushort[])((dynamic)task).Result; LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(string[])) { string[] result = (string[])((dynamic)task).Result; LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{string.Join(",", result)}]"); } else if (resultType == typeof(byte[])) { byte[] result = (byte[])((dynamic)task).Result; LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: [{BitConverter.ToString(result)}]"); } else if (resultType == typeof(void)) { LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } else { LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms,结果: {((dynamic)task).Result}"); } } catch (Exception ex) { // 如果出现异常,记录异常并设置默认返回值 sw.Stop(); LoggerHelper.ErrorWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); // 根据返回类型设置默认值 if (resultType == typeof(bool)) defaultReturnValue = false; // 默认为 false else if (resultType == typeof(bool[])) defaultReturnValue = new bool[0]; // 默认为空数组 else if (resultType == typeof(ushort[])) defaultReturnValue = new ushort[0]; // 默认为空数组 else if (resultType == typeof(string[])) defaultReturnValue = new string[0]; // 默认为空数组 else if (resultType == typeof(byte[])) defaultReturnValue = new byte[0]; // 默认为空字节数组 else if (resultType == typeof(void)) defaultReturnValue = null; // 无返回值,设置为 null // 返回默认值 invocation.ReturnValue = Task.FromResult(defaultReturnValue); return; } } else { // 如果是普通的 Task,无需处理泛型返回值 await (Task)invocation.ReturnValue; LoggerHelper.SuccessWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 完成,耗时 {sw.ElapsedMilliseconds} ms"); } } catch (Exception ex) { sw.Stop(); LoggerHelper.ErrorWithNotify($"调用 {invocation.TargetType.Name}.{invocation.Method.Name}() 异常:{ex.Message}", ex.StackTrace); } } }