Classic ASP : Capture Errors
Solution 1
Yes, create an asp page which will log the error details to the database, and set this to be the 500 handler page in IIS as below.
Use the Server.GetLastError object to get the details of the error in your handler script.
It might be a good idea to log to a text file rather than a DB in your 500 handler for resiliency.
Solution 2
Complementing Jon's answer, use this script to write errors to a log file:
<%
'Set this page up in IIS to receive HTTP 500 errors
''Type' needs to be 'URL' and the URL is e.g.: '/500Error.asp' if this file is named '500Error.asp' and is in the site root directory.
'This script assumes there is a "/Log" folder, and that IIS has write access to it.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim objFSO, err
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set err = Server.GetLastError()
outFile=Server.MapPath("/Log/ErrorLog.txt")
Set objFile = objFSO.OpenTextFile(outFile, ForAppending, True, TristateTrue)
objFile.WriteLine Now & " - ERROR - ASPCode:" & err.ASPCode & " ASPDescription: " & err.ASPDescription & " Category: " & err.Category & " Description: " & err.Description & " File: " & err.File & " Line: " & err.Line & " Source: " & err.Source & vbCrLf
objFile.Close
Set objFile = Nothing
Set err = Nothing
%>
Solution 3
Error handling in classic ASP is a complete pain. You can catch the error where you think it's going to occur using on error resume next
, then check for the error code in the following line of code.
Alternately you can scan the server logs for 500 errors. or set up a "500 error" page in your IIS settings.
On Error Resume Next
... do something...
If Err.Number <> 0 Then
... handle error
end if
MindGame
Updated on June 05, 2022Comments
-
MindGame about 2 years
Is it possible to capture all 500 errors in Classic ASP at a global level? Maybe something in IIS. I'm using II6 at the moment. I like to capture the error message and then store it in the database. I know its possible in ASPX pages, but don't know exactly how you do in classic asp.
Thank you
-
MindGame over 12 yearsThis is exactly what I was looking for. Thanks Jon. I will try it out right now.
-
MindGame over 12 yearsWhen I response.write Server.GetLastError() I don't get anything. Any ideas?
-
MindGame over 12 yearsOh I was doing something dumb. I was not looking at the properties. ASPError.ASPCode() ASPError.ASPDescription() ASPError.Category() ASPError.Column() ASPError.Description() ASPError.File() ASPError.Line() ASPError.Number() ASPError.Source()
-
Shadow The Kid Wizard over 12 yearsGreat answer indeed as @Dan also said - going to award nice fat bounty soon. :-)
-
Shawn J. Molloy almost 9 yearsI wish I could upvote this answer twice; such an elegantly sophisticated solution to a usually clunky and antiquated software development platform. It sure beats modifying tons of legacy code to include psuedo try to catch blocks...
-
Oliver almost 8 yearsFor IIS7 you have to configure a new error redirection in "IIS->Error Pages" in your website settings. The url must be relative to root site not relative to your site (ex: "/YourSite/ErrorPages/500.asp") although you are configuring it in your website settings.
-
Jeff Huang over 7 yearsI tried this injunction with @Saille's solution. However I wasn't able to produce the same result. Either at ASP -> Error Page or IIS -> Error Page
-
Stephen R over 4 yearsASP Classic <> ASP.NET
-
Stephen R over 4 yearsIf that doesn't work, set a custom error page for
500.100
errors. This is the actual error ASP Classic returns. (Also, if your site runs multiple languages you can have a different error page for ASP vs. whatever else. Handy if you want ASP to log errors! For example,500.100
handled byerror.asp
,500
handled byerror.php
.)