/// <summary> /// Executes the Unchase.FluentPerformanceMeter-wrapped middleware. /// </summary> /// <param name="context">The <see cref="HttpContext"/> for the current request.</param> /// <returns>A task that represents the execution of the MiniProfiler-wrapped middleware.</returns> /// <exception cref="ArgumentNullException">Throws when <paramref name="context"/> is <c>null</c>.</exception> public async Task Invoke(HttpContext context) { _ = context ?? throw new ArgumentNullException(nameof(context)); if (PerformanceMeterCommonMethods.ShouldWatching(context.Request, Options)) { var controllerActionDescriptor = (context.Features[typeof(IEndpointFeature)] as IEndpointFeature)?.Endpoint?.Metadata?.GetMetadata <ControllerActionDescriptor>(); if (controllerActionDescriptor != null) { if (PerformanceMeterCommonMethods.CheckExcludedMethods(controllerActionDescriptor, Options)) { await _next(context); // don't watching, only relay } if (PerformanceMeterCommonMethods.CheckAnnotatedAttribute(controllerActionDescriptor, Options, typeof(WatchingPerformanceAttribute))) { await _next(context); // don't watching, only relay } if (PerformanceMeterCommonMethods.CheckIgnoreMethodPerformanceAttribute(controllerActionDescriptor, Options)) { await _next(context); // don't watching, only relay } if (controllerActionDescriptor.ControllerTypeInfo.UnderlyingSystemType != typeof(TClass)) { await _next(context); // don't watching, only relay } var performanceMeterBuilder = PerformanceMeter <TClass> .WatchingMethod(controllerActionDescriptor.ActionName) .WithSettingData; // add custom data from custom attributes performanceMeterBuilder = performanceMeterBuilder.AddCustomDataFromCustomAttributes(context, controllerActionDescriptor, Options); using (performanceMeterBuilder.Start()) { // execute the pipe await _next(context); } } else { // don't watching, only relay await _next(context); } } else { // don't watching, only relay await _next(context); } }
public void OnBeforeAction(HttpContext httpContext, ActionDescriptor actionDescriptor) { if (PerformanceMeterCommonMethods.ShouldWatching(httpContext.Request, Options)) { if (actionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { if (PerformanceMeterCommonMethods.CheckExcludedMethods(controllerActionDescriptor, Options)) { return; } if (PerformanceMeterCommonMethods.CheckAnnotatedAttribute(controllerActionDescriptor, Options, typeof(WatchingWithDiagnosticSourceAttribute))) { return; } if (PerformanceMeterCommonMethods.CheckIgnoreMethodPerformanceAttribute(controllerActionDescriptor, Options)) { return; } var performanceMeterBuilder = PerformanceMeter <TClass> .WatchingMethod(controllerActionDescriptor.ActionName) .WithSettingData; // add custom data from custom attributes performanceMeterBuilder = performanceMeterBuilder.AddCustomDataFromCustomAttributes(httpContext, controllerActionDescriptor, Options); httpContext.Items["PerformanceMeter"] = performanceMeterBuilder.Start(); } } }