public void StopWatchProfileTest(int delay) { var profiler = new StopwatchProfiler(); profiler.Start(); Thread.Sleep(delay * 2); profiler.Stop(); Assert.True(profiler.Elapsed.TotalMilliseconds >= delay); profiler.Restart(); Thread.Sleep(delay / 2); profiler.Stop(); Assert.True(profiler.Elapsed.TotalMilliseconds < delay); }
/// <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(); }
/// <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); }
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); }