示例#1
0
        /// <summary>
        /// 实例化IInterceptor唯一方法
        /// </summary>
        /// <param name="invocation">包含被拦截方法的信息</param>
        public void Intercept(IInvocation invocation)
        {
            var profiler = new StopwatchProfiler();

            profiler.Start();
            //记录被拦截方法信息的日志信息
            var dataIntercept = "" +
                                $"【当前执行方法】:{ invocation.Method.Name} \r\n" +
                                $"【携带的参数有】: {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())} \r\n";

            try
            {
                MiniProfiler.Current.Step($"执行Service方法:{invocation.Method.Name}() -> ");
                //在被拦截的方法执行完毕后 继续执行当前方法,注意是被拦截的是异步的
                invocation.Proceed();

                // 异步获取异常,先执行
                if (IsAsyncMethod(invocation.Method))
                {
                    //Wait task execution and modify return value
                    if (invocation.Method.ReturnType == typeof(Task))
                    {
                        invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(
                            (Task)invocation.ReturnValue,
                            async() => await TestActionAsync(invocation),
                            ex =>
                        {
                            LogEx(ex, ref dataIntercept);
                        });
                    }
                    else //Task<TResult>
                    {
                        invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
                            invocation.Method.ReturnType.GenericTypeArguments[0],
                            invocation.ReturnValue,
                            async() => await TestActionAsync(invocation),
                            ex =>
                        {
                            LogEx(ex, ref dataIntercept);
                        });
                    }
                }
                else
                {// 同步1
                }
            }
            catch (Exception ex)// 同步2
            {
                LogEx(ex, ref dataIntercept);
            }
            profiler.Stop();
            dataIntercept += ($"【执行消耗时间】:{profiler.ElapsedMilliseconds}ms, \r\n【执行完成结果】:{invocation.ReturnValue}");

            Parallel.For(0, 1, e =>
            {
                LogLock.OutSql2Log("AOPLog", new string[] { dataIntercept });
            });
            //SignalR 暂时不开启
            // _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
        }
示例#2
0
    public void StopWatchProfileTest(int delay)
    {
        var profiler = new StopwatchProfiler();

        profiler.Start();
        Thread.Sleep(delay * 2);
        profiler.Stop();
        Assert.True(profiler.Elapsed.TotalMilliseconds >= delay);
    }
        /// <summary>
        /// 注入中间件到HttpContext中
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
        {
            var profiler = new StopwatchProfiler();

            profiler.Start();
            await _request(context);

            profiler.Stop();
            _logger.LogInformation("TraceId:{TraceId}, RequestMethod:{RequestMethod}, RequestPath:{RequestPath}, ElapsedMilliseconds:{ElapsedMilliseconds}, Response StatusCode: {StatusCode}",
                                   context.TraceIdentifier, context.Request.Method, context.Request.Path, profiler.ElapsedMilliseconds, context.Response.StatusCode);
        }
示例#4
0
    public static StopwatchProfiler GetAndStartProfiler(object name)
    {
        if (DoNotProfile)
        {
            return(null);
        }
        StopwatchProfiler profiler = GetProfiler(name.ToString());

        profiler.Start();
        return(profiler);
    }
    public static StopwatchProfiler GetProfiler(string name)
    {
        if (DoNotProfile) return null;

        StopwatchProfiler profiler;
        profilers.TryGetValue(name, out profiler);

        if (profiler == null)
        {
            profiler = new StopwatchProfiler();
            profilers.Add(name, profiler);
        }

        return profiler;
    }
示例#6
0
        public void StopWatchProfileTest(int delay)
        {
            var profiler = new StopwatchProfiler();

            profiler.Start();
            Thread.Sleep(delay);
            profiler.Stop();
            Assert.True(profiler.ElapsedMilliseconds >= delay);
            profiler.Restart();
            Thread.Sleep(delay / 2);
            profiler.Stop();
            Assert.True(profiler.ElapsedMilliseconds < delay);
            profiler.Reset();
            Assert.Equal(0, profiler.ElapsedMilliseconds);
        }
示例#7
0
        public static IApplicationBuilder UsePerformanceLog(this IApplicationBuilder applicationBuilder)
        {
            applicationBuilder.Use(async(context, next) =>
            {
                var profiler = new StopwatchProfiler();
                profiler.Start();
                await next();
                profiler.Stop();

                var logger = context.RequestServices.GetService <ILoggerFactory>()
                             .CreateLogger("PerformanceLog");
                logger.LogInformation("TraceId:{TraceId}, RequestMethod:{RequestMethod}, RequestPath:{RequestPath}, ElapsedMilliseconds:{ElapsedMilliseconds}, Response StatusCode: {StatusCode}",
                                      context.TraceIdentifier, context.Request.Method, context.Request.Path, profiler.ElapsedMilliseconds, context.Response.StatusCode);
            });
            return(applicationBuilder);
        }
示例#8
0
    public void Start()
    {
        StopwatchProfiler lastProfiler = ProfilerStack.Peek();

        if (lastProfiler != this)
        {
            ProfilerStack.Push(this);
        }

        nestingLevel++;
        NumberOfCalls++;

        if (nestingLevel == 1)
        {
            stopWatch.Start();
        }
    }
示例#9
0
    public static StopwatchProfiler GetProfiler(string name)
    {
        if (DoNotProfile)
        {
            return(null);
        }

        StopwatchProfiler profiler;

        profilers.TryGetValue(name, out profiler);

        if (profiler == null)
        {
            profiler = new StopwatchProfiler();
            profilers.Add(name, profiler);
        }

        return(profiler);
    }
示例#10
0
    public void Stop()
    {
        if (nestingLevel == 1)
        {
            stopWatch.Stop();

            StopwatchProfiler previousProfiler = ProfilerStack.Peek();
            if (previousProfiler == this)
            {
                ProfilerStack.Pop();
            }

            previousProfiler = ProfilerStack.Peek();
            if (previousProfiler != null)
            {
                previousProfiler.childrenElapsedMilliseconds += (ElapsedMilliseconds - lastElapsedMilliseconds);
            }
            lastElapsedMilliseconds = ElapsedMilliseconds;
        }
        nestingLevel--;
    }