Updating the ELMAH database for handled exceptions

I had installed ELMAH via NuGet into my MVC application. I wanted to log errors to the database, so my next step was to download the scripts to create the SQL Server objects at https://code.google.com/p/elmah/downloads/detail?name=ELMAH-1.2-db-SQLServer.sql. After running the scripts in my database of choice, I had in my possession an awesome new database table and a few stored procedures.

Then I dug into the web.config to add to tell ELMAH to log unhandled exceptions to this new table. That last property, “applicationName”, is the value that is stored in the “Application” column when ELMAH logs an unhandled exception.

  <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection" applicationName="ScottsApp" />
  </elmah>

For handled exceptions, ELMAH comes with the handy ErrorSignal class to handle that exception and, in my case, store it in the database:

try {
     // Code that I'm sure would never, ever cause an error. But just in case…
} catch (Exception ex) {
     ErrorSignal.FromCurrentContext().Raise(ex);
}

But I also wanted to use the ELMAH table, ELMAH_Error, to act as a logging store for certain events such as invalid logins. So I created a method using the Entity Framework library:

        public void LogError(string errorType, string message, string user, string stackTrace) {
            DbContext o = new DbContext(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            string app = ConfigurationManager.AppSettings["applicationName"];
            List<object> os = o.Database.SqlQuery<object>("exec ELMAH_LogError @ErrorId, @Application, @Host, @Type, @Source, @Message, @User, @AllXml, @StatusCode, @TimeUtc", new SqlParameter("@ErrorId", Guid.NewGuid())
                , new SqlParameter ("@Application", app )
                , new SqlParameter ("@Host", System.Environment.MachineName)
                , new SqlParameter ("@Type", errorType)
                , new SqlParameter ("@Source", app)
                , new SqlParameter ("@Message", message)
                , new SqlParameter ("@User", user)
                , new SqlParameter ("@AllXml", stackTrace)
                , new SqlParameter ("@StatusCode", "0")
                , new SqlParameter ("@TimeUtc", DateTime.Now)
                ).ToList();
        }

I used a DbContext object, and then called the Database.SqlQuery method to call the ELMAH-created stored procedure, ELMAH_LogError. I had a problem where code similar to that above didn’t execute. No error, just nothing happened. The problem in my case is that I was missing the ToList() call. I actually needed a call to execute the query I had built with “SqlQuery”, and an action like ToList() did just that.

Notice that the SqlQuery format of:

 exec spName @paramName, @paramName, ...

If you don’t properly separate the parameters with commas, you’ll see this error:
Procedure or function ‘ELMAH_LogError’ expects parameter ‘@ErrorId’, which was not supplied.

Don’t let that happen to you.

Advertisements
  1. Leave a comment

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

%d bloggers like this: