【问题标题】:How do I generate error report emails in Classic ASP?如何在 Classic ASP 中生成错误报告电子邮件?
【发布时间】:2018-06-26 20:24:54
【问题描述】:

我想在我的经典 ASP 应用程序中捕获 500 个错误并接收有关错误的电子邮件。

【问题讨论】:

    标签: vbscript asp-classic iis-10


    【解决方案1】:

    我遇到的最大问题是当一个 asp 页面崩溃时要执行一个页面。要么 IIS 让 ASP 处理错误(这是无用的,因为 customErrors 标记被完全忽略),要么它会接管并清除 Server.GetLastError

    最后我发现如果你将defaultPath直接设置为httpErrors并将defaultResponseMode设置为ExecuteURL,它不仅会执行实际的500.asp页面,还会让页面这样Server.GetLastError 就可以保持填充状态。

    web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>    
        <system.webServer>
            <httpErrors defaultPath="/500.asp" defaultResponseMode="ExecuteURL" />
        </system.webServer>
    </configuration>
    

    500.asp

    <%option explicit%>
    <!DOCTYPE html>
    <%  
        'Get Error
        Dim objASPError
        Set objASPError = Server.GetLastError
    
        'Reset Response
        If Response.Buffer Then
            Response.Clear
            Response.Status = "500 Internal Server Error"
            Response.ContentType = "text/html"
            Response.Expires = 0
        End If
    
        'CSS
        dim style : style = "<style type='text/css'>"
        style = style & "table { width: 800px; } "  
        style = style & "#debugInfo th{ text-align:left; background-color:palegoldenrod; }"
        style = style & "#debugInfo td{ background-color:lightgoldenrodyellow; }"
        style = style & "#sessionInfo th{ text-align:left; background-color:cyan;}"
        style = style & "#sessionInfo td{ background-color:lightcyan; }"
        style = style & "#appInfo th{ text-align:left; background-color:tomato; }"
        style = style & "#appInfo td{ background-color:pink; }"
        style = style & "</style>"
    
        ' Error Message
        Dim errMsg : errMsg = ""    
        errMsg = errMsg &"<p>"
        errMsg = errMsg & objASPError.Category & "(0x" & hex(objASPError.Number) & ")<br />"
        errMsg = errMsg & objASPError.Description & "<br />"
        errMsg = errMsg & objASPError.File & ", line " & objASPError.Line & "<br />"
        errMsg = errMsg & objASPError.Source & "<br />"
        errMsg = errMsg & "</p>"
    
        'Debug Info
        Dim debugInfo : debugInfo = ""
        debugInfo = debugInfo & "<table id='debugInfo'>"    
        debugInfo = debugInfo & "<tr> <th colspan='2' style='text-align:center;background-color:gold;padding:2px;'>Debug Information</th> </tr>"    
        debugInfo = debugInfo & "<tr> <th>User</th>     <td>"& Request.ServerVariables("AUTH_USER") &"</td> </tr>"
        debugInfo = debugInfo & "<tr> <th>Time</th>     <td>"& Now() &"</td> </tr>"     
        debugInfo = debugInfo & "<tr> <th>Page</th>     <td>"& Request.ServerVariables("SCRIPT_NAME") &"</td> </tr>"
        debugInfo = debugInfo & "<tr> <th>User IP</th>  <td>"& Request.ServerVariables("REMOTE_HOST") & " (" & Request.ServerVariables("REMOTE_ADDR") &")</td> </tr>"
        debugInfo = debugInfo & "<tr> <th>Browser</th>  <td>"& Request.ServerVariables("HTTP_USER_AGENT") &"</td> </tr>"
        debugInfo = debugInfo & "<tr> <th>Server</th>   <td>"& Request.ServerVariables("SERVER_NAME") & " (" & Request.ServerVariables("LOCAL_ADDR") &")</td> </tr>"    
        debugInfo = debugInfo & "<tr> <th>POST</th>     <td>"& Request.Form &"</td> </tr>"
        debugInfo = debugInfo & "</table>"  
    
        'Session Variables
        Dim sessionVar, sessionVarItem, sessionTable
        sessionTable = "<table id='sessionInfo'>"   
        sessionTable = sessionTable & "<tr><th colspan='2' style='text-align:center;background-color:dodgerblue;padding:2px;'>Session Variables ("& Session.Contents.Count  &")</th></tr>"  
        For Each sessionVar in Session.Contents
            If IsArray(Session(sessionVar)) Then
                For appVarItem = LBound(Session(sessionVar)) to UBound(Session(sessionVar))
                    sessionTable = sessionTable & "<tr>"
                    sessionTable = sessionTable & "<th>" & sessionVar & " " & sessionVarItem & "</th>"
                    sessionTable = sessionTable & "<td>" & Session(sessionVar)(sessionVarItem) & "</td>"
                    sessionTable = sessionTable & "</tr>"
                Next
            Else
                sessionTable = sessionTable & "<tr>"
                sessionTable = sessionTable & "<th>" & sessionVar & "</th>"
                sessionTable = sessionTable & "<td>" & Session(sessionVar) & "</td>"
                sessionTable = sessionTable & "</tr>"
            End If
        Next
        sessionTable = sessionTable & "</table>"
    
        'Application Variables
        Dim appVar, appVarItem, appVarTable
        appVarTable = "<table id='appInfo'>"    
        appVarTable = appVarTable & "<tr><th colspan='2' style='text-align:center;background-color:lightcoral;padding:2px;'>Application Variables ("& Application.Contents.Count  &")</th></tr>"    
        For Each appVar in Application.Contents
            If IsArray(Application(appVar)) Then
                For appVarItem = LBound(Application(appVar)) to UBound(Application(appVar))
                    appVarTable = appVarTable & "<tr>"
                    appVarTable = appVarTable & "<th>" & appVar & " " & appVarItem & "</th>"
                    appVarTable = appVarTable & "<td>" & Application(item)(appVarItem) & "</td>"
                    appVarTable = appVarTable & "</tr>"
                Next
            Else
                appVarTable = appVarTable & "<tr>"
                appVarTable = appVarTable & "<th>" & appVar & "</th>"
                appVarTable = appVarTable & "<td>" & Application(appVar) & "</td>"
                appVarTable = appVarTable & "</tr>"
            End If
        Next
        appVarTable = appVarTable & "</table>"
    
    
        'Send Email
        dim tech : tech = CreateObject("WScript.Network").UserName = "ITGuy"
        If NOT tech Then
            Dim oMessage : Set oMessage = CreateObject("CDO.Message") 
            oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")      = 2
            oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")     = "smtp.server.com"
            oMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            oMessage.Configuration.Fields.Update
    
            oMessage.To = "ITGuy@server.com"
            oMessage.From = "WebApp@server.com"
            oMessage.Subject="WebApp Error - " & objASPError.Category
            oMessage.htmlBody = style & errMsg & debugInfo & sessionTable & appVarTable
            oMessage.Send
        End If
    %>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>System Error</title>
            <% If tech Then Response.Write style%>
        </head>
        <body>
            Sorry! The system encountered an error. An email has been sent to IT. If you have any concerns or questions please send an email to <a href="mailto:ITGuy@server.com">ITGuy@server.com</a> or call x555.
            <%If tech Then Response.Write errMsg & debugInfo & sessionTable & appVarTable %>
        </body>
    </html>
    

    【讨论】:

    • 谢谢@martha!我非常担心在我为此制作的 GitHub 上发布一些敏感的东西,以至于我完全忽略了在这里检查代码。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多