Skip to content

Instantly share code, notes, and snippets.

@ido-ran
Last active December 17, 2015 21:39
Show Gist options
  • Save ido-ran/5676077 to your computer and use it in GitHub Desktop.
Save ido-ran/5676077 to your computer and use it in GitHub Desktop.
Profiler for Razor View Engine using MiniProfiler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StackExchange.Profiling;
namespace MiniProfiler.Razor {
public class ProfiledRazorViewEngine : RazorViewEngine {
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) {
using (MiniProfiler.Current.Step("CreatePartialView " + partialPath)) {
IView view = base.CreatePartialView(controllerContext, partialPath);
return new ProfiledView(view, partialPath);
}
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) {
using (MiniProfiler.Current.Step("CreateView " + viewPath)) {
IView view = base.CreateView(controllerContext, viewPath, masterPath);
return new ProfiledView(view, viewPath);
}
}
private class ProfiledView : IView {
private readonly IView view;
private readonly string debugName;
public ProfiledView(IView view, string debugName) {
this.view = view;
this.debugName = debugName;
}
public void Render(ViewContext viewContext, System.IO.TextWriter writer) {
using (MiniProfiler.Current.Step("Rendering " + debugName)) {
view.Render(viewContext, writer);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment