Emailing errors to yourself

BluJag

Perch
Emailing errors to yourself

ASP.net in VB

If your site suffers a glitch it should

a) Redirect the user to a suitable error page which says Sorry, an error has occurred etc etc
b) Email the details of the error to your self for analysis

So, to redirect the user, put this code in your web.config file

<system.web>

<!-- CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable.
Add <error> tags for each of the errors you want to handle.

"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running
on the local Web server. This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->

<!-- enable custom errors for the application -->
<customErrors mode="RemoteOnly" defaultRedirect="ErrorPage.aspx"/>

etc etc

</system.web>

This "ErrorPage.aspx" will be in the root folder of your website. Any time an error occurs, including a 404 error, the user will be redirected to this error page.

Now, to email the error to yourself. In Global.asax.vb

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim mBody As String
mBody = "Error: "
mBody = mBody & Server.GetLastError().InnerException.ToString()
mBody = mBody & vbCrLf & vbCrLf

mBody = mBody & Request.Url.AbsolutePath()
mBody = mBody & vbCrLf & vbCrLf
mBody = mBody & Now
mBody = mBody & vbCrLf & vbCrLf
mBody = mBody & Server.MachineName
mBody = mBody & vbCrLf
mBody = mBody & Request.UserAgent
mBody = mBody & vbCrLf


Dim Mail As ASPEMAILLib.IMailSender ' using Persits ASPEmail - use whatever email handler you're used to and amend code as necessary
Mail = New ASPEMAILLib.MailSender

With Mail

.From = "ERRORS@MySite"
.AddAddress("[email protected]")
.Subject = "Website App Error"
.Body = "ERROR MSG MYSITE " & Chr(10) & Chr(10) & mBody

Try
.Host = "mail.mydomain.com"
.Send()
Catch ex_1 As Exception
Try
.Host = "mail.m****here.biz"
.Send()
Catch ex_2 As Exception
Try
.Host = "mail2.m****here.biz"
.Send()
Catch ex_3 As Exception
Try
.Host = "mail3.m****here.biz"
.Send()
Catch ex_4 As Exception
Try
.Host = "mail3.m****here.biz"
.Send()
Catch ex_5 As Exception
Try
.Host = "localhost"
.Send()
Catch ex_6 As Exception
Try
.Host = "127.0.0.1"
.Send()
Catch ex As Exception

End Try
End Try
End Try
End Try
End Try
End Try
End Try

End With

'This bit of code would add the error to a table in your database which logs the error
Try
Dim Errs As New Namespace.Helpers
Errs.AddError(mBody)
Catch ex99 As Exception
End Try


End Sub
 
Alternatively, if you e-mail the contents of Server.GetLastError().GetBaseException().GetHtmlErrorMessage() you can see the exact message and line numbers the user would normally see (of course, I'd still recommend a redirect to an error page, and only send the details to admin).

HttpCompileException ex = (HttpCompileException) ctx.Server.GetLastError().GetBaseException();
string txtError = ex.GetHtmlErrorMessage().ToString();
 
Back
Top