Get Last Sever Error without using Global.asax page
I have set up custom error on my server and i'm redirecting to a page as shown below
<customErrors mode="On">
<error statusCode="500" redirect="/servererror/default.aspx" />
</customErrors>
When it gets to the page servererror/default.aspx
I need it to send an e-mail to me with the exception.message
Here's what i'm trying but it won't work
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
Any help would be much appreciated
Thanks
Jamie
Look here: ASP.NET custom error page - Server.GetLastError() is null
you need to add the redirectmode:
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="/servererror/default.aspx" />
</customErrors>
精彩评论