简体   繁体   中英

capture user experience upon exception

When users are entering information on my site and experience an exception, what is the best way to capture that exception. I would like to know what were they filling out on the form when the exception occurred. How have you handled it? what tool have you used? I have looked into ELMAH and log.net and currently trying to use log.net to capture this information. Any suggestions on what is the best way to capture form fields?

You could register an application error handler in Global.Asax , then access the HttpContext.Current to retrieve the request & form values.

protected void Application_Error(Object sender, EventArgs e)
{
    HttpContext currentContext= HttpContext.Current;

    // set the exception to the Context 
    Exception exception = currentContext.Server.GetLastError();

...

You can then log/save this information as you see fit.

In your Application_Error in Global.asax.cs, you can access the following and log them. This is what we do (with our specifics removed).

Obviously in place of each var whatever =... , you would do LogInSomeWay(whatever) . This code just demonstrates assigning each important piece of the current experience to a variable.

Hope this helps.

Code

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    HttpContext context = HttpContext.Current;

    bool requestAvailable = false;
    try
    {
        if (context.Request != null)
        {
            requestAvailable = true;
        }
    }
    catch{}

    if (context != null) var serverName = context.Server.MachineName;

    if (context != null && requestAvailable)
    {
        var referrer = context.Request.UrlReferrer != null ? context.Request.UrlReferrer.ToString() : "";

        // If you use forms authentication and define your own principal, do some logging with it here
        OurPrincipal user = OurContext.Current.LoggedOnPrincipal;
        if (user != null && user.Identity != null && user.Identity.IsAuthenticated && user.Person != null)
        {
            var logonName = user.Person.LogonName;
            var loginPersonName = user.Person.FirstName + " " + user.Person.LastName;
        }

        var userIP = context.Request.UserHostAddress;
        var userDns = context.Request.UserHostName;
        var userAgent = context.Request.UserAgent;
        var exceptionText = ex.ToString();

        foreach (string key in context.Request.ServerVariables.AllKeys)
        {
            var currentServerVarKey = key;
            var currentServerVarValue = context.Request.ServerVariables[key];
        }

        if (context.Request.ServerVariables["REQUEST_METHOD"] == "POST"
            && context.Request.Form != null && context.Request.Form.AllKeys != null)
        {
            foreach (string key in context.Request.Form.AllKeys)
            {
                var currentFormKey = key;
                var currentFormValue = context.Request.Form[key];
            }
        }
    }
}

In some projects we've been using ELMAH, but I somehow don't like it. (My personal experience though) The abiliy to read filtered error logs in ELMAH is cool, but that's about it.

I've had my best experience writing my own error handler. One way to go would be to subscribe to the Application_Error event.
Another would be to write a custom HealthMonitoring provider. I've written about that topic here: http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails
Maybe it is possible to get a reference to the form that threw the exception and iterate over every Control/TextBox and get their values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM