Search code examples
mvc-mini-profiler

getting mvc-mini-profiler to show some data to all users


I am using the excellent MVC Mini Profiler for an internal project, but would like to get it to show timing information, no matter who you are. ideally, i would like to be able to show the full profiling information if the user is an admin or developer of the site, and show just overall timing info if the user is just a standard user...

would MVC mini profiler be the way to go, or should i just add stopwatches to the site? We are using Solr for our backend, so i would like to be able to say "Solr got results in x miliseconds and we rendered the page in y miliseconds" which we can do (to an extent) at the moment but only for developers... can we get those numbers from the profiler, and then display them ourselves, or am i embarking up the wrong tree here?


Solution

  • MiniProfiler is probably fine or you could register a global filter similar to the code below. Obviously style appropriately for your scenario (see the response.Write(...) portion near the bottom).

    I can't take credit for the filter because I found something nearly identical on a blog (don't remember where, though).

    /// <summary>
    /// Filter to display the execution time of both the action and result
    /// </summary>
    public class RequestTimingFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Returns a Stopwatch instance for the specific context to gather
        /// diagnostics timing for
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static Stopwatch GetTimer(ControllerContext context, string name)
        {
            var key = string.Format("__timer__{0}", name);
            if (context.HttpContext.Items.Contains(key))
            {
                return (Stopwatch)context.HttpContext.Items[key];
            }
    
            var result = new Stopwatch();
            context.HttpContext.Items[key] = result;
            return result;
        }
    
        /// <summary>
        /// Called before an action method executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            GetTimer(filterContext, "action").Start();
        }
    
        /// <summary>
        /// Called after the action method executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            GetTimer(filterContext, "action").Stop();
        }
    
        /// <summary>
        /// Called before an action result executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            GetTimer(filterContext, "render").Start();
        }
    
        /// <summary>
        /// Called after an action result executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            var renderTimer = GetTimer(filterContext, "render");
            renderTimer.Stop();
    
            var actionTimer = GetTimer(filterContext, "action");
            var response = filterContext.HttpContext.Response;
    
            if (response.ContentType == "text/html")
            {
                response.Write(
                  string.Format(
                    "<div style='font-size: 70%; font-weight: bold; color: #888;'>Action '{0} :: {1}'<br /> Execute: {2}ms, Render: {3}ms.</div>",
                    filterContext.RouteData.Values["controller"],
                    filterContext.RouteData.Values["action"],
                    actionTimer.ElapsedMilliseconds,
                    renderTimer.ElapsedMilliseconds
                    )
                  );
            }
        }
    }