Error Logging Modules and Handlers for ASP.NET (elmah)

by michel 18. May 2009 20:37

A while ago a friend a my promoted ELMAH via Live messenger, since then I am a FAN! 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.

Exceptions caught by ELMAH can be stored in memory, loose xml, VistaDb, Access, SQLLite, Oracle, MS Sql, or directly send by email. Storage- & mail configuration is done via the web.config.

The source of ELMAH is available and under the Apache License 2.0.
More info @: http://code.google.com/p/elmah/

In this post I’ll do an new implementation of the ELMAH Errorlog class to facilitate the MS SQL error logging with MOSS elevated privileges to solve the following problem.

The RunWithElevatedPrivilegesmethod will executes the specified code with Full Control rights even if the user does not otherwise have Full Control. In the MOSS environment it will execute the code under control of the the Application Pool Identity User. Implementing this method is really easy:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
   // put your code in here :)
});

The first step is to setup a class in Visual Studio (my example SqlErrorLogWEP (Sql ErrorLog With Elevated Privileges) and implement the abstract Errorlog class.

namespace Elmah
{
    using System;

    class SqlErrorLogWEP : ErrorLog
    {
        public override string Log(Error error)
        {
            throw new NotImplementedException();
        }

        public override ErrorLogEntry GetError(string id)
        {
            throw new NotImplementedException();
        }

        public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
        {
            throw new NotImplementedException();
        }
    }
}

Because our SqlErrorLogWEP looks really for 99,9% like the classic SqlErrorLog class. In fact our class will act as a wrapper to implement the MOSS elevated privileges functionality for the SqlErrorLog class. To do this our constructor will initialize the (classic) SqlError log class and use the private sqlErrror object to perform the database logging. The above methods which still need to be implemented will use the sqlError object to do the work.

Our new class will look like this:

namespace Elmah
{
    using System;
    using System.Text;
    using System.Collections;
    using Microsoft.SharePoint;
    

    class SqlErrorLogWEP : ErrorLog
    {
        SqlErrorLog sqlErrorLog;

        public SqlErrorLogWEP(IDictionary config)
        {
            sqlErrorLog = new SqlErrorLog(config);
        }

        public SqlErrorLogWEP(string connectionString)
        {
            sqlErrorLog = new SqlErrorLog(connectionString);
        }

        public override string Log(Error error)
        {
            string retVal = String.Empty;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                retVal = sqlErrorLog.Log(error);
            });

            return retVal;
        }

        public override ErrorLogEntry GetError(string id)
        {
            ErrorLogEntry retVal = default(ErrorLogEntry);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                retVal = sqlErrorLog.GetError(id);
            });

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
               // put your code in here :)
            });
            return retVal;
        }

        public override int GetErrors(int pageIndex, int pageSize, System.Collections.IList errorEntryList)
        {
            int retVal = -1;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // put your code overhere 
            });

            return retVal;
        }
    }
}

Pro’s  / Con’s
Pro’s: Implementation in 2 minutes, completely ELMAH proof, extendable.
Con’s: Instead of only deploying the ELMAH assembly you will have an extra reference to the Microosoft.SharePoint assembly.

Tags: , , ,

.Net | C# | Microsoft | SharePoint | Tooling

Comments

2/27/2010 6:10:01 PM #

Fatcow

Should I get a Dedicated Hosting?  I am using anhosting but they keep turning off my websites due to high server load. Im getting about 3,000 UV a day. What brand should I get?

Fatcow United States

3/16/2010 11:15:32 PM #

Bani Online

I love this blog, I will came back

Bani Online Taiwan

Comments are closed

About the author

Michel TolMy name is Michel Tol. I'm a developer specialized in .NET technologies. Mainly focussing on SharePoint Technologies and web development. I am Certified Technology Specialist for MOSS 2007 -  Configuration, .Net Framework 2.0 - Web applications and Biztalk Server 2006 - Custom Applications.

View Michel Tol's profile on LinkedIn

  

E-mail me Send mail

Page List