hutongqing
2024-11-20 70233af5426b0d1c343ebe87183303a34a9aaa58
WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/LogAOP.cs
@@ -16,7 +16,7 @@
namespace WIDESEAWCS_Core.AOP
{
    /// <summary>
   /// 拦截器BlogLogAOP 继承IInterceptor接口
   /// 拦截器LogAOP 继承IInterceptor接口
   /// </summary>
   public class LogAOP : IInterceptor
    {
@@ -26,7 +26,6 @@
        {
            _accessor = accessor;
        }
        /// <summary>
        /// 实例化IInterceptor唯一方法 
@@ -38,7 +37,7 @@
            string json;
            try
            {
                if(invocation.Arguments.Any())
                if (invocation.Arguments.Any())
                {
                    json = JsonConvert.SerializeObject(invocation.Arguments);
                }
@@ -73,9 +72,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 +82,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
                {
@@ -155,9 +123,7 @@
            catch (Exception ex) // 同步2
            {
                LogEx(ex, apiLogAopInfo);
                throw;
            }
        }
        private async Task SuccessAction(IInvocation invocation, AOPLogInfo apiLogAopInfo, DateTime startTime, object o = null)
@@ -168,12 +134,10 @@
            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) });
                });
            });
@@ -204,10 +168,7 @@
        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<>);
        }
    }
@@ -258,7 +219,7 @@
            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 });
        }
@@ -289,7 +250,7 @@
        /// <summary>
        /// 请求参数数据JSON
        /// </summary>
        [Description("请求参数数据JSON")]
        [Description("请求参数数据JSON")]
        public string RequestParamsData { get; set; } = string.Empty;
        /// <summary>
        /// 请求响应间隔时间