tags:

views:

111

answers:

3

I want to try to time an ASP.NET MVC ActionMethod.

I was thinking a nice way to do this would be using a custom header.

Can I use an ActionFilterAttribute to do this? Has anyone done this, or is there any build in ASP.NET functionality to do this?

I'm just being kicked out of Starbucks and hoping someone will have an answer before I get home. Usually someone does :-)

A: 

i'd agree with Cherian that tracing would be the best choice since it should be the most accurate.

i think if i can guess what your idea is with the attribute option, i'd have to assume you would use reflection to sorta dynamically start and stop timers on the methods as they execute. this would be kool i guess.. but might add overhead giving false readings

cottsak
+1  A: 

You can try Eqatec profiler. It's free :)

mathieu
+2  A: 

I'd agree asp.net tracing would be most suitable for the job. But working with ActionFilter should be fun.

This code was untested.

public class TimingAttribute : ActionFilterAttribute
{
    Stopwatch stopwatch;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        stopwatch = new Stopwatch();
        stopwatch.Start();
        base.OnActionExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        try
        {
            base.OnResultExecuted(filterContext);
        }
        finally
        {
            stopwatch.Stop();
            var ms = stopwatch.ElapsedMilliseconds;
            // your logging here. controller/action/request info could be extracted from filterContext
        }
    }
}
Canton
Simon_Weaver
by the 'fun' part i mean
Simon_Weaver
Hope this wont land up in production.
Cherian