不使用Global.asax页面获取上次服务器错误

时间:2010-09-27 09:58:00

标签: asp.net server-error

我在服务器上设置了自定义错误,我正在重定向到如下所示的页面

<customErrors mode="On"> 
   <error statusCode="500" redirect="/servererror/default.aspx" /> 
</customErrors>

当它到达页面servererror/default.aspx时,我需要它使用exception.message

向我发送电子邮件

这是我正在尝试的但它不起作用

Sub Page_load(ByVal sender As Object, ByVal e As EventArgs)

     Dim LastError As Exception
     Dim ErrMessage As String

     LastError = Server.GetLastError()


        ErrMessage = LastError.Message


Dim Errormail = New MailMessage

     'Send email to me
     Errormail.To           = "email@email.co.uk"

     Errormail.From         = "servererror@email.co.uk"
     Errormail.Subject      = "Server Error Alert"
     Errormail.BodyFormat   = MailFormat.Text
     Errormail.Priority     = MailPriority.Normal
     Errormail.Body         = ErrMessage

     SmtpMail.SmtpServer    = "localhost"

     SmtpMail.Send(Errormail)

     Server.ClearError()
End Sub

非常感谢任何帮助

由于

杰米

1 个答案:

答案 0 :(得分:1)

看这里: ASP.NET custom error page - Server.GetLastError() is null

您需要添加redirectmode:

<customErrors mode="On" redirectMode="ResponseRewrite"> 
   <error statusCode="500" redirect="/servererror/default.aspx" /> 
</customErrors>