Skip to content

Instantly share code, notes, and snippets.

@mortenbock
Created September 21, 2013 16:04
Show Gist options
  • Save mortenbock/6651847 to your computer and use it in GitHub Desktop.
Save mortenbock/6651847 to your computer and use it in GitHub Desktop.
MiniProfiler.UmbracoMvc proof of concept. Blogpost here: http://www.mortenbock.dk/post/Profiling-MVC-views-in-Umbraco-6
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web.Mvc;
namespace UmbMvcMiniProfiler
{
public class Bootstrapper : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyProfilingEngine(new RenderViewEngine()));
ViewEngines.Engines.Add(new MyProfilingEngine(new PluginViewEngine()));
}
}
}
using System.Web.Mvc;
using StackExchange.Profiling;
namespace UmbMvcMiniProfiler
{
public class MyProfilingEngine : IViewEngine
{
private readonly IViewEngine _inner;
private readonly string _name;
public MyProfilingEngine(IViewEngine inner)
{
_inner = inner;
_name = inner.GetType().Name;
}
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
using (MiniProfiler.Current.Step(string.Format("{0}.FindPartialView, {1},{2}", _name, partialViewName, useCache)))
{
return WrapResult(_inner.FindPartialView(controllerContext, partialViewName, useCache));
}
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
using (MiniProfiler.Current.Step(string.Format("{0}.FindView, {1},{2},{3}", _name, viewName, masterName, useCache)))
{
return WrapResult(_inner.FindView(controllerContext, viewName, masterName, useCache));
}
}
private static ViewEngineResult WrapResult(ViewEngineResult innerResult)
{
ViewEngineResult profiledResult = innerResult.View != null
? new ViewEngineResult(new MyProfilingView(innerResult.View), innerResult.ViewEngine)
: new ViewEngineResult(innerResult.SearchedLocations);
return profiledResult;
}
public void ReleaseView(ControllerContext controllerContext, IView view)
{
using (MiniProfiler.Current.Step(string.Format("{0}.ReleaseView, {1}", _name, view.GetType().Name)))
{
_inner.ReleaseView(controllerContext, view);
}
}
}
}
using System.IO;
using System.Web.Mvc;
using StackExchange.Profiling;
namespace UmbMvcMiniProfiler
{
public class MyProfilingView : IView
{
private readonly IView _innerView;
private readonly string _path;
public MyProfilingView(IView innerView)
{
_innerView = innerView;
_path = innerView is BuildManagerCompiledView
? ((BuildManagerCompiledView) innerView).ViewPath
: "NoPath";
}
public void Render(ViewContext viewContext, TextWriter writer)
{
using (MiniProfiler.Current.Step(string.Format("Render: {0}", _path)))
{
_innerView.Render(viewContext, writer);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment