Add Session Variable or Custom Field to Elmah Error Log Table

I want to add my own session variable to the elmah error log table and display it. I already changed the source code and added new fields to Error.cs and other fields, but I don’t know, but when I set the value of HttpContext.Current.Session [MyVar] tostring () for my field in the constructor field, the exception logging stops and logs no exceptions.

Do I just need to get the value of the session variable for another? I read the message in which he added the fields for the email, but does not say exactly where I should get the session value. I also read that session and cookies are written by default Elmah, but I do not know how to access them. Thank you all for your help.

+3
source share
2 answers

Without seeing my source code, I can only make assumptions, but I think Elmah stops registering because you get an exception. This can happen because the property Sessionis null. A session is available only after the event PostAcquireRequestState, so you cannot count on the fact that this property is always available.

I tried this myself and wrote a method like this:

private static Dictionary<string, object> CopySession(HttpSessionState session)
{
    if (session == null || session.Count == 0)
        return null;

    Dictionary<string, object> copy = new Dictionary<string, object>(session.Count);
    foreach (var key in session.Keys)
    {
        string name = key.ToString();
        copy.Add(name, session[name]);
    }

    return copy;
}

I call this method in the constructor of the Error class as follows:

// _session is a new field that I added like this:
// private Dictionary<string, object> _session;
_session = CopySession(context.Session);

(immediately after copying the server variables and other context contents. You may need to change the implementation IClonableto copy the field _session).

, . SqlErrorLog, ErrorLog: , , .

+2

, , , , .

0

Source: https://habr.com/ru/post/1742155/


All Articles