How to get ELMAH to include session values?

后端 未结 3 813
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 01:24

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 probl

3条回答
  •  灰色年华
    2021-02-15 02:19

    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;
        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;
        }
    
        if (stack != null && stack.Count > 0)
        {
            e.Mail.Body = e.Mail.Body + "

    Browsing History

    " + System.Environment.NewLine; while (stack.Count > 0) { e.Mail.Body = e.Mail.Body + stack.Pop() + "
    " + System.Environment.NewLine; } } }

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

提交回复
热议问题