ELMAH Error Logging in MVC 4

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

For logging to a SQL Server database download the SQL Server Script.

Execute the script in SQL Server.

It does the following:
1. creates a table called ELMAH_error, define a primary key called PK_ELMAH_Error and an index called IX_ELMAH_Error_App_Time_Seq
2. creates 3 stored procedures: ELMAH_GetErrorXml, ELMAH_GetErrorsXml, ELMAH_LogError

To add ELMAH to your Project you need to install the Nuget Package ELMAH, using the package manager console:

PM> Install-Package elmah

In Web.config

1. In <connectionStrings> look for the one your application uses like this:

<connectionStrings>
<add name="MVCMovieTrainingContext" connectionString="Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=MVCMovieTraining;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Take note of the name in this case “MVCMovieTrainingContext”.

Note: Replace SERVERNAME with the name of your SQL Server.

2. Look for <elmah> section and add this line (between the <elmah> tags):

<elmah>
<errorLog type ="Elmah.SqlErrorLog, Elmah" connectionStringName ="MVCMovieTrainingContext"> </errorLog>
<security allowRemoteAccess="true" />
</elmah>

Note: If you enable “allowRemoteAccess”, follow the instructions in the readme file created when you install elmah. There are security issues that you should address.

3. Execute and try to generate an error in your application like going to an inexistent page.
4. Test to see if it is working going to:

http://yourapplication:999/elmah.axd

Replace “yourapplication” and port number 999 with the correct ones for your application.

errorlogelmah

Advertisements
This entry was posted in CSharp, MVC, Visual Studio 2012 and tagged , , , , , , , , . Bookmark the permalink.

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