Skip to content

Instantly share code, notes, and snippets.

@michaeljbailey
Created May 11, 2015 13:22
Show Gist options
  • Save michaeljbailey/460ab0ec1f60376279df to your computer and use it in GitHub Desktop.
Save michaeljbailey/460ab0ec1f60376279df to your computer and use it in GitHub Desktop.
Timing for Razor rendering
namespace Timing
{
using System;
using System.Diagnostics;
using System.Web.Mvc;
public class TraceScope : IDisposable
{
private readonly string _name;
private readonly ViewContext _viewContext;
private readonly Stopwatch _stopwatch;
public TraceScope(string name, ViewContext viewContext)
{
_name = name;
_viewContext = viewContext;
_stopwatch = Stopwatch.StartNew();
}
public void Dispose()
{
_stopwatch.Stop();
_viewContext.Writer.Write("<!-- {0} rendered in {1} milliseconds -->", _name, _stopwatch.ElapsedMilliseconds);
}
}
public static class HtmlHelperExtensions
{
public static IDisposable BeginTrace(this HtmlHelper helper, string name)
{
return new TraceScope(name, helper.ViewContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment