| | |
| | | namespace WIDESEAWCS_Core.AOP |
| | | { |
| | | /// <summary> |
| | | /// 拦截器BlogLogAOP 继承IInterceptor接口 |
| | | /// 拦截器LogAOP 继承IInterceptor接口 |
| | | /// </summary> |
| | | public class LogAOP : IInterceptor |
| | | { |
| | |
| | | { |
| | | _accessor = accessor; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 实例化IInterceptor唯一方法 |
| | |
| | | string json; |
| | | try |
| | | { |
| | | if(invocation.Arguments.Any()) |
| | | if (invocation.Arguments.Any()) |
| | | { |
| | | json = JsonConvert.SerializeObject(invocation.Arguments); |
| | | } |
| | |
| | | // 异步获取异常,先执行 |
| | | if (IsAsyncMethod(invocation.Method)) |
| | | { |
| | | #region 方案一 |
| | | |
| | | //Wait task execution and modify return value |
| | | if (invocation.Method.ReturnType == typeof(Task)) |
| | | { |
| | | invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( |
| | |
| | | LogEx(ex, apiLogAopInfo); |
| | | }); |
| | | } |
| | | //Task<TResult> |
| | | else |
| | | { |
| | | invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( |
| | | invocation.Method.ReturnType.GenericTypeArguments[0], |
| | | invocation.ReturnValue, |
| | | //async () => await SuccessAction(invocation, dataIntercept),/*成功时执行*/ |
| | | async (o) => await SuccessAction(invocation, apiLogAopInfo, startTime, o), /*成功时执行*/ |
| | | ex => |
| | | { |
| | | LogEx(ex, apiLogAopInfo); |
| | | }); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | |
| | | // 如果方案一不行,试试这个方案 |
| | | //#region 方案二 |
| | | |
| | | //var type = invocation.Method.ReturnType; |
| | | //var resultProperty = type.GetProperty("Result"); |
| | | //DateTime endTime = DateTime.Now; |
| | | //string ResponseTime = (endTime - startTime).Milliseconds.ToString(); |
| | | //apiLogAopInfo.ResponseTime = endTime.ToString("yyyy-MM-dd hh:mm:ss fff"); |
| | | //apiLogAopInfo.ResponseIntervalTime = ResponseTime + "ms"; |
| | | //apiLogAopInfo.ResponseJsonData = JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue)); |
| | | |
| | | ////dataIntercept += ($"【响应时间】:{ResponseTime}ms\r\n"); |
| | | ////dataIntercept += ($"【执行完成时间】:{endTime.ToString("yyyy-MM-dd hh:mm:ss fff")}\r\n"); |
| | | ////dataIntercept += ($"【执行完成结果】:{JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue))}\r\n"); |
| | | |
| | | //Parallel.For(0, 1, e => |
| | | //{ |
| | | // //LogLock.OutLogAOP("AOPLog", new string[] { dataIntercept }); |
| | | // LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString() + " - ResponseJsonDataType:" + type, JsonConvert.SerializeObject(apiLogAopInfo) }); |
| | | //}); |
| | | |
| | | //#endregion |
| | | } |
| | | else |
| | | { |
| | |
| | | catch (Exception ex) // 同步2 |
| | | { |
| | | LogEx(ex, apiLogAopInfo); |
| | | throw; |
| | | } |
| | | |
| | | } |
| | | |
| | | private async Task SuccessAction(IInvocation invocation, AOPLogInfo apiLogAopInfo, DateTime startTime, object o = null) |
| | |
| | | apiLogAopInfo.ResponseIntervalTime = ResponseTime + "ms"; |
| | | apiLogAopInfo.ResponseJsonData = JsonConvert.SerializeObject(o); |
| | | |
| | | |
| | | await Task.Run(() => |
| | | { |
| | | Parallel.For(0, 1, e => |
| | | { |
| | | //LogLock.OutSql2Log("AOPLog", new string[] { JsonConvert.SerializeObject(apiLogAopInfo) }); |
| | | LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) }); |
| | | }); |
| | | }); |
| | |
| | | |
| | | public static bool IsAsyncMethod(MethodInfo method) |
| | | { |
| | | return |
| | | method.ReturnType == typeof(Task) || |
| | | method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>) |
| | | ; |
| | | return method.ReturnType == typeof(Task) || method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>); |
| | | } |
| | | } |
| | | |
| | |
| | | Func<object, Task> action, Action<Exception> finalAction) |
| | | { |
| | | return typeof(InternalAsyncHelper) |
| | | .GetMethod("AwaitTaskWithPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
| | | .GetMethod(nameof(AwaitTaskWithPostActionAndFinallyAndGetResult), BindingFlags.Public | BindingFlags.Static) |
| | | .MakeGenericMethod(taskReturnType) |
| | | .Invoke(null, new object[] { actualReturnValue, action, finalAction }); |
| | | } |
| | |
| | | /// <summary> |
| | | /// 请求参数数据JSON |
| | | /// </summary> |
| | | [Description("请求参数数据JSON")] |
| | | [Description("请求参数数据JSON")] |
| | | public string RequestParamsData { get; set; } = string.Empty; |
| | | /// <summary> |
| | | /// 请求响应间隔时间 |