/// <summary> /// Method called before the action method starts processing /// </summary> /// <param name="filterContext">An ActionExecutingContext object</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { // First thing is to check if performance is enabled globally. If not, return if (ConfigInfo.Value.PerformanceEnabled == false) { return; } // Second thing, check if performance tracking has been turned off for this action // If the DoNotTrackAttribute is present, then return ActionDescriptor actionDescriptor = filterContext.ActionDescriptor; if (actionDescriptor.GetCustomAttributes(typeof(DoNotTrackPerformanceAttribute), true).Length > 0 || actionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(DoNotTrackPerformanceAttribute), true).Length > 0) { return; } // ActionInfo encapsulates all the info about the action being invoked ActionInfo info = this.CreateActionInfo(filterContext); // PerformanceTracker is the object that tracks performance and is attached to the request PerformanceTracker tracker = new PerformanceTracker(info); // Store this on the request String contextKey = this.GetUniqueContextKey(filterContext.ActionDescriptor.UniqueId); HttpContext.Current.Items.Add(contextKey, tracker); // Process the action start - this is what starts the timer and increments any // required counters before the action executes tracker.ProcessActionStart(); }
/// <summary> /// Method that runs before the Web API action is invoked /// </summary> /// <param name="actionContext">An HttpActionContext with info about the action that is executing</param> public override void OnActionExecuting(HttpActionContext actionContext) { // First thing is to check if performance is enabled globally if (ConfigInfo.Value.PerformanceEnabled == false) { return; } // Second thing is to check if performance tracking has been turned off for this action // If the DoNotTrackAttribute is present, then we set a flag not to track performance and return HttpActionDescriptor actionDescriptor = actionContext.ActionDescriptor; if (actionDescriptor.GetCustomAttributes <DoNotTrackPerformanceAttribute>().Count > 0 || actionDescriptor.ControllerDescriptor.GetCustomAttributes <DoNotTrackPerformanceAttribute>().Count > 0) { return; } // ActionInfo encapsulates all the info about the action being invoked ActionInfo info = this.CreateActionInfo(actionContext); // PerformanceTracker is the object that tracks performance and is attached to the request PerformanceTracker tracker = new PerformanceTracker(info); // Store this on the request actionContext.Request.Properties.Add(this.GetType().FullName, tracker); // Process the action start - this is what starts the timer and increments any // required counters before the action executes tracker.ProcessActionStart(); }
/// <summary> /// Method called after the action method has completed executing /// </summary> /// <remarks> /// This method first checks to make sure we are indeed tracking performance. If so, it stops /// the stopwatch and then calls the OnActionComplete() method of all of the performance metric /// objects attached to this action filter /// </remarks> /// <param name="filterContext">An ActionExecutedConext object</param> public override void OnActionExecuted(ActionExecutedContext filterContext) { // This is the unique key the PerformanceTracker object would be stored under String contextKey = this.GetUniqueContextKey(filterContext.ActionDescriptor.UniqueId); // Check if there is an object on the request. If not, must not be tracking performance // for this action, so just go ahead and return if (HttpContext.Current.Items.Contains(contextKey) == false) { return; } // If we are here, we are tracking performance. Extract the object from the request and call // ProcessActionComplete. This will stop the stopwatch and update the performance metrics PerformanceTracker tracker = HttpContext.Current.Items[contextKey] as PerformanceTracker; if (tracker != null) { bool exceptionThrown = (filterContext.Exception != null); tracker.ProcessActionComplete(exceptionThrown); } }
/// <summary> /// Method that executes after the WebAPI action has completed /// </summary> /// <param name="actionExecutedContext">An HttpActionExecutedContext object with info about the action that just executed</param> public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { String key = this.GetType().FullName; // Check the request properties for the PerformanceTracker object. If it does not exist // then performance isn't being tracked on this action, so just return if (actionExecutedContext.Request.Properties.ContainsKey(key) == false) { return; } // Get the PerformanceTrcker object PerformanceTracker tracker = actionExecutedContext.Request.Properties[key] as PerformanceTracker; // Make sure the object isn't null (failed cast). ProcessActionComplete stops the stopwach // and updates the performance counters if (tracker != null) { bool exceptionThrown = (actionExecutedContext.Exception != null); tracker.ProcessActionComplete(exceptionThrown); } }