Using MVC Mini Profiler as a Http Module

MVC Mini Profiler is a great tool to profile your ASP.NET MVC apps. It’s easy to setup: all you need is to render some includes in your page’s HTML and start/stop the profiler on BeginRequest/EndRequest event handlers, as desribed here.

However, I did not want to include that code and HTML includes in production pages, so I wrote a Http Module that you can switch on/off easily in your application’s web.config file. Here’s the code:

public class MiniProfilerHttpModule: IHttpModule
{
	public void Init(HttpApplication context)
	{
		context.BeginRequest += delegate
		{
			if (context.Request.IsLocal)
			{
				context.Response.Filter = new MiniProfilerFilterStream(context.Response.Filter);
				MiniProfiler.Start();
			}
		};

		context.EndRequest += delegate
		{
			MiniProfiler.Stop();
		};
	}

	public void Dispose()
	{
	}
}

That’s easy, the more interesting part is the MiniProfilerFilterStream class. It acts as a filter to all the response HTML that is returned to the browser. All it does is search for the end of head tag and insert the output of MvcMiniProfiler.MiniProfiler.RenderIncludes() method just before it. Here’s the source code of the class:

public class MiniProfilerFilterStream: Stream
{
	private readonly Stream stream;
	private bool profilerHtmlRendered;

	public MiniProfilerFilterStream(Stream stream)
	{
		this.stream = stream;
	}

	public override void Flush()
	{
		stream.Flush();
	}

	public override long Seek(long offset, SeekOrigin origin)
	{
		return stream.Seek(offset, origin);
	}

	public override void SetLength(long value)
	{
		stream.SetLength(value);
	}

	public override int Read(byte[] buffer, int offset, int count)
	{
		return stream.Read(buffer, offset, count);
	}

	public override void Write(byte[] buffer, int offset, int count)
	{
		if (!profilerHtmlRendered)
		{
			var responseText = Encoding.UTF8.GetString(buffer, offset, count);

			if (responseText.Contains("</head>"))
			{
				var miniProfilerHtml = MiniProfiler.RenderIncludes().ToHtmlString();
				int index = responseText.IndexOf("</head>");
				var newOutput = responseText.Insert(index, miniProfilerHtml);
				var newBytes = Encoding.UTF8.GetBytes(newOutput);

				stream.Write(newBytes, 0, newBytes.Length);
				profilerHtmlRendered = true;
				return;
			}
		}

		stream.Write(buffer, offset, count);
	}

	public override bool CanRead
	{
		get { return stream.CanRead; }
	}

	public override bool CanSeek
	{
		get { return stream.CanSeek; }
	}

	public override bool CanWrite
	{
		get { return stream.CanWrite; }
	}

	public override long Length
	{
		get { return stream.Length; }
	}

	public override long Position
	{
		get { return stream.Position; }
		set { stream.Position = value; }
	}
}

The magic happens in Write method, others just act as a proxy to the original stream.

Switching MVC Mini Profiler on in your application is now as easy as adding the Http Module to the web.config:

<system.web>
	<httpModules>
		<add name="MiniProfiler" type="Management.Web.Infrastructure.Monitoring.MiniProfilerHttpModule"/>
	</httpModules>
</system.web>

Advertisements

One thought on “Using MVC Mini Profiler as a Http Module

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s