ELMAH for ASP.NET MVC 4 using SQL SERVER 2008 R2

asp.net-mvc, elmah, sql-server, sql-server-2008-r2

Solution

Once you have the database setup, all you need to do is add the following to the `<elmah>` section your web.config to setup the Elmah to log to the SQL Database:

 <elmah>
      <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="<DBConnString>"
            applicationName="<YourApp>" 
  </elmah>

Replace `<DBConnString>` and `<YourApp>` with appropriate values for your configuration.

Once you have done this you will not need to use your custom ElmahHandleErrorAttribute class.

I am not sure which NuGet package you installed, but I would recommend using the Elmah.MVC package as it integrates Elmah into MVC exceptionally well by setting up all of the ErrorHandlers and ErrorFilters for you.

Problem

I ran ELMAH sql scripts in test DB(It created ELmah_Error table and 3 stored procedures) and configured ELMAH in MVC application using Nuget. I modified web.config as specified and I'm able to log exceptions into ``` http://mysite/elmah.axd ``` But, instead i want to log the exceptions into Sql Server. I added below class to achieve that ``` public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute { public override void OnException(System.Web.Mvc.ExceptionContext context) { LogException(e); } private static void LogException(Exception e) { // Call to Database and insert the exception info } } ``` Final step was to: ``` public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ElmahHandleErrorAttribute ()); } ``` Is it the correct way to use ELMAH to log all exceptions or AM I missing something?

Original source