hutongqing
2024-08-28 d0f01f571918ae942349068776fa7fb70f3ab5cd
WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/LogAOP.cs
@@ -73,9 +73,6 @@
                // 异步获取异常,先执行
                if (IsAsyncMethod(invocation.Method))
                {
                    #region 方案一
                    //Wait task execution and modify return value
                    if (invocation.Method.ReturnType == typeof(Task))
                    {
                        invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(
@@ -86,45 +83,17 @@
                                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
                {
@@ -157,7 +126,6 @@
                LogEx(ex, apiLogAopInfo);
                throw;
            }
        }
        private async Task SuccessAction(IInvocation invocation, AOPLogInfo apiLogAopInfo, DateTime startTime, object o = null)
@@ -173,7 +141,6 @@
            {
                Parallel.For(0, 1, e =>
                {
                    //LogLock.OutSql2Log("AOPLog", new string[] { JsonConvert.SerializeObject(apiLogAopInfo) });
                    LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo) });
                });
            });