Receive notification when errors occurs

B

Bruno G.

Hello!

Is it possible for IIS to send some sort of notification when an ASP
error occurs on a page?

I can see them in the web logs, but I was wondering if there was a way
to receive an email or some other alert...

I thought about using CDO Send Mail, but this object is often what
cause the error in the first place.

Any suggestion?

Thanks in advance.

Bruno G.
 
R

Ray Costanzo [MVP]

Yes, by implemeting a custom "500" page for your site or application in IIS.
Then you can have an ASP page handle the 500 errors with code such as this:



<%
Option Explicit

Response.Clear
Response.Status = 500

Dim sUrlRequested, sRedirect, sPath
Dim aVars, aPostData, aCookies, aQuerystring






Function GetErrorInfo
Dim aErrorInfo(1,13)
Dim oError
Set oError = Server.GetLastError

aErrorInfo(0,0) = "ASP Code" : aErrorInfo(1,0) = oError.ASPCode
aErrorInfo(0,1) = "ASP Description" : aErrorInfo(1,1) =
oError.ASPDescription
aErrorInfo(0,2) = "Description" : aErrorInfo(1,2) = oError.Description
aErrorInfo(0,3) = "Category" : aErrorInfo(1,3) = oError.Category
aErrorInfo(0,4) = "Number" : aErrorInfo(1,4) = oError.Number
aErrorInfo(0,5) = "Source" : aErrorInfo(1,5) = oError.Source
aErrorInfo(0,6) = "File" : aErrorInfo(1,6) = oError.File
aErrorInfo(0,7) = "Line" : aErrorInfo(1,7) = oError.Line
aErrorInfo(0,8) = "Querystring" : aErrorInfo(1,8) = Request.Querystring
On Error Resume Next
aErrorInfo(0,9) = "Form Data" : aErrorInfo(1,9) = Request.Form
If Err.Number <> 0 Then aErrorInfo(0,9) = "(binary data)"
On Error Goto 0
aErrorInfo(0,10) = "All HTTP" : aErrorInfo(1,10) = "<textarea
style=""width: 410px; height=100px; font: 8pt verdana;"">" &
Request.ServerVariables("ALL_HTTP") & "</textarea>"
aErrorInfo(0,11) = "Remote Address" : aErrorInfo(1,11) =
Request.ServerVariables("REMOTE_ADDR")
aErrorInfo(0,12) = "Description" : aErrorInfo(1,12) =
Request.ServerVariables("REMOTE_HOST")
aErrorInfo(0,13) = "Request Date" : aErrorInfo(1,13) = FormatDateTime(Now,
1) & " " & FormatDateTime(Now, 3)

' "Safen" form data and all HTTP

If Len(aErrorInfo(1,9)) > 50 Then aErrorInfo(1,9) = "&nbsp;<textarea
style=""width: 410px; height=100px; font: 8pt verdana;"">" & aErrorInfo(1,9)
& "</textarea>"
If Len(aErrorInfo(1,10)) > 50 Then aErrorInfo(1,10) = "&nbsp;<textarea
style=""width: 410px; height=100px; font: 8pt verdana;"">" &
aErrorInfo(1,10) & "</textarea>"


GetErrorInfo = aErrorInfo
End Function




sPath = Replace(sUrlRequested, "http://" &
Request.ServerVariables("SERVER_NAME"), "")


Call SendAlert(sUrlRequested, sRedirect)
Call General500()
Call DumpInfo()



%>


<% Sub General500() %>

<strong>Oops! An error has occurred, and the page you requested
cannot be loaded.</strong><br />
The page you requested is unavailable at the moment. An alert has been
sent to the
webmaster and this should be corrected soon. Please check back later.

<% End Sub %>

<%
Sub DumpInfo()
Dim i, aErrors, sBGColor
aErrors = GetErrorInfo
%>
<table width="100%" bgcolor="#000000" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFFFF">
<td align="center"><strong>Error Information</strong></td>
</tr>

<tr>
<td>
<table width="100%" align="center" cellpadding="3" cellspacing="1">
<% For i = 0 To UBound(aErrors, 2)
If i Mod 2 = 0 Then
sBGColor = "#FFFFFF"
Else
sBGColor = "#EEEEEE"
End If
%>
<tr bgcolor="<%=sBGColor%>">
<td width="120" valign="top" align="right"><%=aErrors(0,i)%>:</td>
<td><font size="1"><%=aErrors(1,i)%></font></td>
</tr>
<% Next %>
</table>
</td>
</tr>
</table>
<br />
<% End Sub %>


<%
Function GetServerVariables()
Dim aVars(), sVar, i
i = 0
For Each sVar in Request.ServerVariables
Redim Preserve aVars(1,i)
aVars(0,i) = sVar
aVars(1,i) = Request.ServerVariables(sVar)
Next
GetServerVariables = aVars
End Function


Function GetQuerystring()
Dim aVars, sName, i
GetQuerystring = ""
i = 0
If Len(Request.Querystring) > 0 Then
For Each sName In Request.Querystring
Redim Preserve aVars(1,i)
aVars(0,i) = sName
aVars(1,i) = Request.Querystring(sName)
Next
GetQuerystring = aVars
End If
End Function


%>
<% Response.End %>







<%


Sub SendAlert(URL, Redirect)

Dim sUrlRequested, sReferer, sPath
Dim oCDO, sMessage, aErrorInfo, i

sReferer = Request.ServerVariables("HTTP_REFERER")


sMessage = "Dear Webmaster," & vbCrlf & vbCrLf
sMessage = sMessage & "The following page request was made on the
[sitename], but the page generated an error. Please check the code for
coder error." & vbCrLf & vbCrLf
sMessage = sMessage & "URL Requested: " & URL & vbCrLf
sMessage = sMessage & "Referring URL: " & sReferer & vbCrLf
sMessage = sMessage & "Requester IP : " &
Request.ServerVariables("REMOTE_ADDR") & vbCrLf
sMessage = sMessage & "Requested ID : " &
Request.ServerVariables("AUTH_USER") & vbCrLf & vbCrLf
sMessage = sMessage & String(50,"-") & vbCrLf
sMessage = sMessage & "Debug Info:" & vbCrLf & vbCrLf
aErrorInfo = GetErrorInfo
For i = 0 To UBound(aErrorInfo, 2)
sMessage = sMessage & aErrorInfo(0,i) & ":" & vbCrLf & aErrorInfo(1,i) &
vbCrLf & vbCrLf
Next

sPath = Replace(URL, "http://" & Request.ServerVariables("SERVER_NAME"),
"")







Set oCDO = Server.CreateObject("CDO.Message")
oCDO.From = "webserver@mydomain"
oCDO.To = "me@mydomain"
oCDO.Subject = "500 Error on Website - " &
Request.ServerVariables("REMOTE_ADDR")
oCDO.TextBody = sMessage
oCDO.Send
Set oCDO = Nothing

End Sub


%>






Ray at work
 
B

Bruno G.

Thanks for the code... but what if the 500 error was caused by the Send
method of the CDO.Message object in another ASP page?
Won't this email fail as well?

Bruno
 
J

Jeff Dillon

So what do you want to happen? Obviously email is out.

Write to a text file, event log, or database.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top