ELMAH - Using custom error pages to collecting user feedback
Asked Answered
L

1

8

I'm looking at using ELMAH for the first time but have a requirement that needs to be met that I'm not sure how to go about achieving...

Basically, I am going to configure ELMAH to work under asp.net MVC and get it to log errors to the database when they occur. On top of this I be using customErrors to direct the user to a friendly message page when an error occurs. Fairly standard stuff...

The requirement is that on this custom error page I have a form which enables to user to provide extra information if they wish. Now the problem arises due to the fact that at this point the error is already logged and I need to associate the loged error with the users feedback.

Normally, if I was using my own custom implementation, after I log the error I would pass through the ID of the error to the custom error page so that an association can be made. But because of the way that ELMAH works, I don't think the same is quite possible.

Hence I was wondering how people thought that one might go about doing this....

Cheers

UPDATE:

My solution to the problem is as follows:

public class UserCurrentConextUsingWebContext : IUserCurrentConext
{
    private const string _StoredExceptionName = "System.StoredException.";
    private const string _StoredExceptionIdName = "System.StoredExceptionId.";

    public virtual string UniqueAddress
    {
        get { return HttpContext.Current.Request.UserHostAddress; }
    }

    public Exception StoredException
    {
        get { return HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] as Exception; }
        set { HttpContext.Current.Application[_StoredExceptionName + this.UniqueAddress] = value; }
    }

    public string StoredExceptionId
    {
        get { return HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] as string; }
        set { HttpContext.Current.Application[_StoredExceptionIdName + this.UniqueAddress] = value; }
    }
}

Then when the error occurs, I have something like this in my Global.asax:

public void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
    var item = new UserCurrentConextUsingWebContext();
    item.StoredException = args.Entry.Error.Exception;
    item.StoredExceptionId = args.Entry.Id;
} 

Then where ever you are later you can pull out the details by

    var item = new UserCurrentConextUsingWebContext();
    var error = item.StoredException;
    var errorId = item.StoredExceptionId;
    item.StoredException = null;
    item.StoredExceptionId = null;

Note this isn't 100% perfect as its possible for the same IP to have multiple requests to have errors at the same time. But the likely hood of that happening is remote. And this solution is independent of the session, which in our case is important, also some errors can cause sessions to be terminated, etc. Hence why this approach has worked nicely for us.

Lettered answered 26/1, 2010 at 22:25 Comment(4)
Did you get this to work? I am having the same problem. I followed Atif's advice, but when I reference HttpContext.Items for the Elmah Id on my custom error page, I get a null reference exception.Ruthi
@Ronnie Overby: See above edit.Lettered
Thanks. I thought about doing something like that, but using a cookie/ipaddress combination as an identifier. Ultimately I just did this: #2885987Ruthi
@Ronnie Overby: np... looks like a reasonable approach... the only thing was that we wanted to pass the exception (which is a complex object) along to the page where the error occured. As the system is a private system, if all logging fails, we tell the user it failed and print the error info into the source of the page. Hence we can get access to it if needed.Lettered
P
9

The ErrorLogModule in ELMAH (version 1.1 as of this writing) provides a Logged event that you can handle in Global.asax and which you can use to communicate details, say via HttpContext.Items collection, to your custom error page. If you registered the ErrorLogModule under the name ErrorLog in web.config then your event handler in Global.asax will look like this:

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)  
{ 
    var id = args.Entry.Id
    // ...  
}
Peddada answered 5/2, 2010 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.