Classic ASP and Server.GetLastError in IIS7

My classic ASP error logging scripts were dead in the water when I moved them to a Windows Server 2008 with IIS 7.0.

Some code like this is useful to record errors in a database:

dim objErrorInfo, errorStringStr
set objErrorInfo = Server.GetLastError
errorStringStr = objErrorInfo.File & ", line: " & objErrorInfo.Line & ", error: " & objErrorInfo.Number & " " & objErrorInfo.Description & ", " & objErrorInfo.ASPDescription & objErrorInfo.Category
errorStringStr = replace( errorStringStr, "'", "''" )

Instead of using the default 500 item in the list, create a new handler for status code 500.100. Point this at your script, and you should be all set to log errors.

Another way is to enter the Error Pages module of the website profile, and click “Edit Feature Settings” on the right hand sidebar.

This screen will appear:

iis7getLastError

Configure yours in a similar fashion, and Server.GetLastError will start working in your script.


Comments

20 responses to “Classic ASP and Server.GetLastError in IIS7”

  1. It worked. I wonder why edit feature settings works differently than doubleclicking the script.

  2. This is a work-around.
    The real solution is to go to the Error Pages in IIS7, click Add… and add a handler for error code 500.100 and point that to /error.asp.

  3. Adding handler for error code 500.100 and point that to /error.asp. … Thats works great for me .

    Thanks

  4. “The real solution is to go to the Error Pages in IIS7, click Add… and add a handler for error code 500.100 and point that to /error.asp”

    Can this be done from web.config file?

  5. <?xml version="1.0" encoding="UTF-8"?><configuration> <system.webServer> <httpErrors> <remove statusCode="500" subStatusCode="100" /> <error statusCode="500" subStatusCode="100" prefixLanguageFilePath="" path="/error-500.asp" responseMode="ExecuteURL" /> </httpErrors> </system.webServer></configuration>

  6. Thanks for the tip on 500.100. I will give it a shot.

  7. Thanks Simon, i have been nagging my host for this for days now to no avail.

    You are a star man, keep shining!

  8. The 500.100 made it! Thanks to Rowdy.
    Thanks to the original poster, too, allowing me to find this thing.

    I’m happy :-)

  9. Jeff V

    Another thumbs-up for this solution.

    My Hosting Provider had subStatusCode=”-1″ in web.config. I changed to subStatusCode=”100″ and it worked!

    THANK YOU!!!

  10. I updated my ASP application to IIS 7.0.6000, I edit a hanler for errors 500. The IIS jumps to this programm and writes all information in a text file. But everything like Line is null and not helpfull. What can I do? Please help!
    Thanks from Austria

  11. Yes Yes Yes

    “The real solution is to go to the Error Pages in IIS7, click Add and add a handler for error code 500.100 and point that to /error.asp”

    That worked !!
    Great.

  12. 500.100 worked !! Thanks a lot.

  13. “The real solution is to go to the Error Pages in IIS7, click Add and add a handler for error code 500.100 and point that to /error.asp”

    That worked !!
    Great.
    Thanks a lot!

  14. Hi,
    Few days before I added handler for error code 500.100 and point that to my asp error page /SMGRError.asp but and for some errors Server.GetLastError() is still not working.
    Please help me to fix this issue.Thanks, Ravi

  15. SandraL

    This did the trick for me. Thanks so much!

  16. “add a handler for error code 500.100”

    Worked for me, many thanks

  17. Thanks for this post, it saved my day! Following is the specific web.config setup that worked for me, in IIS7.x

    Note that existingResponse MUST be “Auto”. It won’t work if you use “Replace”.

  18. It took me about 6 months off and on to find this. It wasn’t until I added the handler for 500.100 that I finally got it to work.

    So thanks very much for that!

  19. Harrison

    Thanks again to Simon for the info about redirecting via the web.config file. Worked great and didn’t have to hassle with the hosting service to change anything. For anyone who happens to be searching this topic, I changed this line:

    to this:

    In ErrorReport.asp I included this:

    set objErr=Server.GetLastError()

    along with some code like:

    response.write(“File=” & objErr.File)
    response.write(“”)
    response.write(“Line=” & objErr.Line)

  20. Thanks to Simon for his 12 year old tip. subStatusCode=”100? on a 500 error than “-1” fixed my problem of redirecting and maintaining accurate error data captured in Server.GetLastError() for logging/email/notification purposes, whilst maintaining browser privacy for the client.