Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ELMAH to include session values?

NOTE: I know the various reasons to avoid using the session, but this is a project I've inherited, so please skip that part of any replies :)

Since it's a solved problem, I'm hoping someone can point to an ELMAH patch/branch/fork that includes logging session data rather than reinventing the wheel.

One weird thing is an older post from Atif that says they're already logged:

http://markmail.org/message/ncmdgwm5rmzewbwu

commenter henningst mentioned adding in the session variables here:

http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

Another approach (I'd rather avoid) is copying the values into cookies

http://www.sharpdeveloper.net/content/archive/2008/11/10/how-to-get-session-or-other-custom-values-into-elmah.aspx

I know one alternative is to switch to something besides ELMAH (like Exceptioneer - see http://exceptioneer.com/Public/ExceptioneerAndELMAH.aspx) but since this is my only problem with ELMAH at the moment, I'd rather just have a patched ELMAH than switch to something else.

like image 803
James Manning Avatar asked Dec 29 '09 09:12

James Manning


2 Answers

Atif replied on twitter to say there's no known patch:

http://twitter.com/raboof/statuses/7229453423

So I created a patch that does so:

http://twitter.com/manningj/statuses/7231616905

http://blog.sublogic.com/2009/12/patch-to-enable-session-variable-logging-with-elmah/

like image 143
James Manning Avatar answered Oct 13 '22 04:10

James Manning


Rather than patching Elmah, I did this with Exception data. In Global.asax I inserted the extra data into the exception on Application_Error(). "HistoryStack" is my own class for recording user history, including button and tab clicks:

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();
    var stack = HistoryStack.Dump(); // essentially grabs data from the session
    ex.Data.Add("historyStack", stack);
}

Then, in ErrorMail_Mailing() I grabbed the data back and appended it in the email:

void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
{
    var stack = e.Error.Exception.Data["historyStack"] as Stack<string>;
    if (stack == null && e.Error.Exception.InnerException != null)
    {
        // could probably skip the first try and go straight to this assignment:
        stack = e.Error.Exception.InnerException.Data["historyStack"] as Stack<string>;
    }

    if (stack != null && stack.Count > 0)
    {
        e.Mail.Body = e.Mail.Body + "<h1>Browsing History</h1>" + System.Environment.NewLine;
        while (stack.Count > 0)
        {
            e.Mail.Body = e.Mail.Body + stack.Pop() + "<br />" + System.Environment.NewLine;
        }
    }
}

Now this data is appended to the bottom of the email. No patches or extensions necessary.

like image 21
thinkOfaNumber Avatar answered Oct 13 '22 03:10

thinkOfaNumber