ELMAH and MiniProfiler In ASP.NET MVC 4

Tuesday, September 11, 2012

If you are new to ASP.NET MVC you might not know about ELMAH and MiniProfiler. These are two distinct OSS projects that you can easily install with NuGet, and both provide valuable debugging and diagnostic services. Scott Hanselman has blogged about both projects in the past (see NuGet Package of the Week #7, and #9), but this post will highlight some of the updates since Scott's post and still provide a general overview of theĀ  features for each project.

ELMAH

ELMAH gives you "application-wide error logging that is completely pluggable". In other words, you can easily log and view all the errors your application experiences without making any changes to your code. All you need to get started is to "install-package elmah" from the Package Manager Console in Visual Studio and ELMAH will be fully configured and available. Navigate to /elmah.axd in your application, and you'll see all the errors (this will only work for local requests, by default).

ELMAH Error Log

ELMAH will keep all the errors in memory, so an application restart will clear the log. If you want the error information to stick around longer, you can configure ELMAH to store information in a more permanent location, like inside of a database. The elmah.sqlservercompact package will add all the configuration you need to store the error log in a SQL Compact database file inside the App_Data directory of the app. There are also packages to log errors to SQL Server (elmah.sqlserver), Mongo (elmah.mongodb) and more. Most of these packages require you to go into the web.config file to configure simple connection string settings, and possibly setup a schema (see the files dropped into App_Readme for more details).

ELMAH and HandleErrorAttribute

For ASP.NET MVC projects you might want to start by installing the elmah.mvc package. The elmah.mvc package will install an ElmahController so you can view the error log by visiting the /elmah URL. More importantly, elmah.mvc will install an action filter to log errors even when the MVC HandleErrorAttribute (the one that most applications use as a global action filter) marks an exception as "handled". The HandleErrorAttribute marks exceptions as handled and renders a friendly error view when custom errors are enabled in web.config, which will usually be true when an app runs in production. These handled exceptions are invisible to ELMAH, but not when elmah.mvc is in place.

MiniProfiler

MiniProfiler is a "simple but effective" profiler you can use to help find performance problems and bottlenecks. The project has come a long way over the last year. To get started with MiniProfiler, you can install the MiniProfiler.MVC3 package (which also works in ASP.NET MVC 4). After installation, add a single line of code to your _Layout view(s) before the closing </body> tag:

@StackExchange.Profiling.MiniProfiler.RenderIncludes() 

Update:

You'll also need to add a handler so MiniProfiler can retrieve the scripts it needs:

<system.webServer>
...
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>


Once everything is ready, you'll start to see the MiniProfiler results appear in the top left of the browser window (for local request only, by default). Click on the time measurement to see more details:

MiniProfiler results

MiniProfiler can tell you how long it takes for actions to execute and views to render, and provides an API if you need to instrument specific pieces of code. The MiniProfiler install will also add a MiniProfiler.cs file in your App_Start directory. This file is where most of the MiniProfiler configuration takes place. From here you can enable authorization and database profiling.

Two great projects to check out, if you haven't seen them before.


Comments
gravatar Kristof Tuesday, September 11, 2012
Is the runAllManagedModulesForAllRequests part specific for MVC? I'm using MiniProfiler with ASP.NET WebForms (.NET 4.5) without the runAllManagedModulesForAllRequests option and it is working fine.

As far as I know, you should avoid turning on runAllManagedModulesForAllRequests if possible. See www.hanselman.com/...
gravatar Graham Clark Tuesday, September 11, 2012
According to the MiniProfiler site, you don't have to have runAllManagedModulesForAllRequests turned on, instead you can add a handler to the system.webServer section of the config. See the troubleshooting section at the bottom (http://miniprofiler.com/).
gravatar scott Tuesday, September 11, 2012
@Graham: thanks, updated.
gravatar Keith Bloom Wednesday, September 12, 2012
They are indeed two great tools. I also came across an dashboard for ELAMH. It is built using SignalR and cunningly called ElmahR http://elmahr.apphb.com/
gravatar Georgi Hadzhigeorgiev Wednesday, September 12, 2012
Great article!

May I ask next one to be on Glimpse.
gravatar link Saturday, September 15, 2012
As for me I was searching for this script for a long time! You saved me)
gravatar cheap polo shirts Thursday, September 20, 2012
There are extremely few firms of standing, which have been in the manufacture and final sale involving advertising polo t-shirts, and many many other
teams of publicity not to mention personalised clothes.
gravatar polo ralph lauren shirts Thursday, September 20, 2012
in September two trading days,cheap polo shirts, the market is hot, no energy, no interest, no characteristic is obvious,Ralph Lauren Outlet, the heavyweights although after a boost, but still difficult to continue, but underestimate the value of financial sector strong trend, the market still exists to change the style characteristics.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!