I want to log the request processing time for Asp.Net MVC application. I can get \'time-taken\' from IIS logs but this time includes the network time taken to
One simple option would be to use something like Miniprofiler. There's documentation that shows you how to tie it into MVC properly.
However, if this type of metric is truly meaningful to track long term, I'd suggest looking into something like New Relic, or any other APM (application performance monitoring) solution.
You can also check out Glimpse. It doesn't so much log as just show you all the pertinent statistics as you browse around. It's also more suited to development than running on a production server, though you can potentially run it in production if you choose.
You can create a filter to intercept action execution time.
public class ActionTiming :ActionFilterAttribute {
private static readonly ILog Logger = LogManager.GetLogger("TimingLogger");
private string controllerName = string.Empty;
private string actionName = string.Empty;
private Stopwatch intertime = Stopwatch.StartNew();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
intertime.Restart();
controllerName = filterContext.RouteData.Values["controller"].ToString();
actionName = filterContext.RouteData.Values["action"].ToString();
var message = string.Format("{0} controller: {1} action: {2}", "OnActionExecuting", controllerName, actionName);
Logger.Info(message);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var message = string.Format("{0} controller: {1} action: {2}, time (ms): {3}", "OnResultExecuted", controllerName, actionName, intertime.ElapsedMilliseconds);
Logger.Info(message);
}
}
Then, you can register this filter globally in Application_Start or use it as attribute.