asp.net - 有关获取诊断数据转储的全局错误处理程序的建议

标签 asp.net error-handling

有大量资源显示如何使用global.ascx全局错误事件处理程序捕获Web应用程序中的非托管错误,但是我尚未找到包含在此方法中的大量代码示例,该示例将报告诸如错误消息,堆栈跟踪的输出,生成错误的页面,生成错误的用户名/角色...等等。

有没有人为此目的使用,使用过或遇到过不错的代码呢?

提前致谢。

最佳答案

这是我们使用的一些VB代码,它为我们提供了诊断大多数问题的详细信息。只有这样的电子邮件错误的缺点是,当发生重大故障时,您可能会在邮箱中收到数千条消息:(

Dim e As Exception

If System.Web.HttpContext.Current.Request.IsLocal Then Exit Sub

e = System.Web.HttpContext.Current.Server.GetLastError
If e IsNot Nothing Then

    If TypeOf e Is HttpException Then
        Dim he As HttpException = DirectCast(e, HttpException)
        If he.GetHttpCode=404 Then
            System.Web.HttpContext.Current.Server.ClearError()
            Response.Redirect("/")
            Exit Sub
        End If
    End If

    MsgBody += "Exception: " & e.Message & vbCrLf & vbCrLf
    MsgBody += e.ToString & vbCrLf & vbCrLf & vbCrLf
Else
    MsgBody += "Unknown Error" & vbCrLf & vbCrLf & vbCrLf
End If

If System.Web.HttpContext.Current.Request.Url Is Nothing Then
    MsgBody += "URL: ?" & vbCrLf & vbCrLf
Else
    MsgBody += "URL: " & System.Web.HttpContext.Current.Request.Url.ToString & vbCrLf
    If System.Web.HttpContext.Current.Request.RawUrl IsNot Nothing Then MsgBody += System.Web.HttpContext.Current.Request.RawUrl & vbCrLf
    MsgBody += vbCrLf
End If


If System.Web.HttpContext.Current.Request.UrlReferrer Is Nothing Then
    MsgBody += "Referer: <direct>" & vbCrLf & vbCrLf
Else
    MsgBody += "Referer: " & System.Web.HttpContext.Current.Request.UrlReferrer.ToString() & vbCrLf & vbCrLf
End If

If User IsNot Nothing Then MsgBody += "User: " & User.Identity.Name & vbCrLf

MsgBody += "User-Agent: " & System.Web.HttpContext.Current.Request.UserAgent & vbCrLf
MsgBody += "User-IP: " & System.Web.HttpContext.Current.Request.UserHostAddress & vbCrLf
MsgBody += "Server: " & System.Environment.MachineName & vbCrLf

MsgBody += vbCrLf & "RequestType: " & System.Web.HttpContext.Current.Request.RequestType() & vbCrLf & vbCrLf        

' dump request items
'   QueryString, Form, Cookies, ServerVariables

MsgBody += "Querystring Variables ================" & vbCrLf
If Request.QueryString.Count > 0 Then
    For Each key As String In Request.QueryString.Keys
        MsgBody += key & " = " & Request.QueryString(key) & vbCrLf
    Next
Else
    MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf

MsgBody += "Form Variables =======================" & vbCrLf
If Request.Form.Count > 0 Then
    For Each key As String In Request.Form.Keys
        MsgBody += key & " = " & Request.Form(key) & vbCrLf
    Next
Else
    MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf

MsgBody += "Cookies ==============================" & vbCrLf
If Request.Cookies.Count > 0 Then
    For Each key As String In Request.Cookies.Keys
        MsgBody += key & " = " & Request.Cookies(key).Value & vbCrLf
    Next
Else
    MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf

MsgBody += "ServerVariables ======================" & vbCrLf
If Request.ServerVariables.Count > 0 Then
    For Each key As String In Request.ServerVariables.Keys
        MsgBody += key & " = " & Request.ServerVariables(key) & vbCrLf
    Next
Else
    MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf           

' we send MsgBody via email using the System.Net.Mail.MailMessage & System.Net.Mail.SmtpClient classes

' we've handled the error       
System.Web.HttpContext.Current.Server.ClearError()

' depending on the error we redirect to our homepage /default.aspx unless that is the faulting page then redirect to static /error.html

关于asp.net - 有关获取诊断数据转储的全局错误处理程序的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250100/

相关文章:

javascript - promise 拒绝不能正确传播错误

tsql - TRY..CATCH Error_Line()…行

asp.net - Javascript 和 ASP.net 之间的区别

C# ASP X-CONTENT-TYPE-OPTIONS - Internet Explorer 不显示 PNG 文件

sql-server - 如何处理用户界面中的数据库约束违规?

asp.net-mvc - 在返回 PartialViewResult 的方法中返回 HttpStatusCodeResult

google-apps-script - 调用 Drive.Files.get(fileId,{alt : 'media' }) in Google apps script (Google Drive API/REST V2)

ASP.NET MVC Html.textbox - 如何只验证数字?

ASP.NET MVC 不良做法 : Controller Action Not Restricted to POST in Asp.Net MVc

c# - 从 Dynamics CRM 2011 插件调用 System.Diagnostics.Trace