views:

62

answers:

1

I am using the Elmah Logged event in my Global.asax file to transfer users to a feedback form when an unhandled exception occurs.

Sometimes I log other handled exceptions. For example:

ErrorSignal.FromCurrentContext().Raise(new System.ApplicationException("Program code not found: " + Student.MostRecentApplication.ProgramCode));

// more code that should execute after logging this exception

The problem I am having is that the Logged event gets fired for both unhandled and these handled, raised exceptions. Is there a way to determine, in the Logged event handler, whether the exception was raised via ErrorSignal class or was simply unhandled? Are there other Elmah events that I can take advantage of?

+1  A: 

Tired of trying to find the "right" way to do this, so I ended up creating my own exception type:

public class HandledElmahException : Exception
{
    public HandledElmahException() : base() { }
    public HandledElmahException(string message) : base(message) { }
    public HandledElmahException(string message, Exception innerException) : base(message, innerException) { }
}

Then, in the ErrorLog.Logged event handler I just check to see if the exception is of type HandledElmahException.

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
    if (args.Entry.Error.Exception is HandledElmahException)
        return;

    // my code to transfer to custom error page to collect feedback...

}

So, if I don't want to take them to the ErrorPage, when an exception is logged, I use an instance of my special HandledElmahException class, which can be derived from.

ErrorSignal.FromCurrentContext().Raise(new HandledElmahException("Program code not found: " + Student.MostRecentApplication.ProgramCode));
Ronnie Overby