/// <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); } }