首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >异常时捕获用户体验

异常时捕获用户体验
EN

Stack Overflow用户
提问于 2012-04-06 06:13:08
回答 3查看 165关注 0票数 1

当用户在我的网站上输入信息并遇到异常时,捕获该异常的最佳方法是什么?我想知道当异常发生时,他们在表格上填写了什么。你是怎么处理的?你用过什么工具?我已经研究了ELMAH和log4net,目前正在尝试使用log4net来捕获这些信息。有什么建议是捕获表单域的最佳方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-06 06:45:59

在Global.asax.cs的Application_Error中,您可以访问并记录以下内容。这就是我们要做的(去掉了我们的细节)。

显然,您将执行LogInSomeWay(whatever),而不是每个var whatever = ...。这段代码只是演示了将当前经验的每个重要部分赋给一个变量。

希望这能有所帮助。

代码

代码语言:javascript
运行
复制
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];
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-04-06 06:23:28

您可以在Global.Asax中注册一个应用程序错误处理程序,然后访问HttpContext.Current以检索请求和表单值。

代码语言:javascript
运行
复制
protected void Application_Error(Object sender, EventArgs e)
{
    HttpContext currentContext= HttpContext.Current;

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

..。

然后,您可以根据需要记录/保存此信息。

票数 1
EN

Stack Overflow用户

发布于 2012-04-06 15:10:29

在一些项目中,我们一直在使用ELMAH,但我不太喜欢它。(我个人的经验是)在ELMAH中能够读取过滤的错误日志是很酷的,但仅此而已。

我对编写自己的错误处理程序有过最好的体验。一种方法是订阅Application_Error事件。

另一种方法是编写一个自定义的HealthMonitoring提供程序。我在这里写过这个话题:http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails

也许可以获得对抛出异常的窗体的引用,并遍历每个控件/文本框并获取它们的值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10036740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档