Errors Errors Errors
We would always prefer to avoid all errors but if one happens we certainly want to know about it. Adding an error handler to the Global.asax file is slick - easy and works beautifully..... here is what you need:
Dim smtp As New System.Net.Mail.SmtpClient("mail.yourmailserver.com")
EndTry
End Sub
strMessage.Append("<style type=""text/css"">")
strMessage.Append("<!--")
strMessage.Append(".basix {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("}")
strMessage.Append(".header1 {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("font-weight: bold;")
strMessage.Append("color: #000099;")
strMessage.Append("}")
strMessage.Append(".tlbbkground1 {")
strMessage.Append("background-color: #000099;")
strMessage.Append("}")
strMessage.Append("-->")
strMessage.Append("</style>")
strMessage.Append("<table width=""85%"" border=""0"" align=""center"" cellpadding=""5"" cellspacing=""1"" class=""tlbbkground1"">")
strMessage.Append("<tr bgcolor=""#eeeeee"">")
strMessage.Append("<td colspan=""2"" class=""header1"">Page Error</td>") strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>User</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("REMOTE_USER") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>IP Address</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("REMOTE_ADDR") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>User Agent</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("HTTP_USER_AGENT") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Page</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.Url.AbsoluteUri & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Time</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & System.DateTime.Now & " EST</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Details</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Server.GetLastError().ToString & "</td>")
strMessage.Append("</tr>")
strMessage.Append("</table>")
Sub
Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim strTo As String = System.Configuration.ConfigurationManager.AppSettings("EmailTo") Dim msg As System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient("mail.yourmailserver.com")
msg = New System.Net.Mail.MailMessage(strFrom, strTo, "Page Error My Application Name", BuildMessage())
msg.IsBodyHtml = True
smtp.Credentials = New System.Net.NetworkCredential("Username", "password", "mail.myserver.com")
Catch
EndTry
End Sub
The buildMessage Function in the global.asax file would look something like this:
Function
BuildMessage() As StringDim
strMessage As New StringBuilderstrMessage.Append("<style type=""text/css"">")
strMessage.Append("<!--")
strMessage.Append(".basix {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("}")
strMessage.Append(".header1 {")
strMessage.Append("font-family: Verdana, Arial, Helvetica, sans-serif;")
strMessage.Append("font-size: 12px;")
strMessage.Append("font-weight: bold;")
strMessage.Append("color: #000099;")
strMessage.Append("}")
strMessage.Append(".tlbbkground1 {")
strMessage.Append("background-color: #000099;")
strMessage.Append("}")
strMessage.Append("-->")
strMessage.Append("</style>")
strMessage.Append("<table width=""85%"" border=""0"" align=""center"" cellpadding=""5"" cellspacing=""1"" class=""tlbbkground1"">")
strMessage.Append("<tr bgcolor=""#eeeeee"">")
strMessage.Append("<td colspan=""2"" class=""header1"">Page Error</td>") strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>User</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("REMOTE_USER") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>IP Address</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("REMOTE_ADDR") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>User Agent</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.ServerVariables("HTTP_USER_AGENT") & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Page</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Request.Url.AbsoluteUri & "</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Time</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & System.DateTime.Now & " EST</td>")
strMessage.Append("</tr>")
strMessage.Append("<tr>")
strMessage.Append("<td width=""100"" align=""right"" bgcolor=""#eeeeee"" class=""header1"" nowrap>Details</td>")
strMessage.Append("<td bgcolor=""#FFFFFF"" class=""basix"">" & Server.GetLastError().ToString & "</td>")
strMessage.Append("</tr>")
strMessage.Append("</table>")
Return
strMessage.ToStringEnd
Function
Comments