Classic ASP : Capture Errors

后端 未结 4 1892
深忆病人
深忆病人 2020-12-28 23:12

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 th

相关标签:
4条回答
  • 2020-12-28 23:48

    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
    
    0 讨论(0)
  • 2020-12-29 00:01

    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
    
    %>
    
    0 讨论(0)
  • 2020-12-29 00:02

    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.

    Set Custom 500 Handler in IIS

    0 讨论(0)
  • 2020-12-29 00:14

    To add to @Jon Eastwood's answer - if you are using IIS 7.5, then instead of "Custom errors" you will look for ".NET Error Pages" per the image below:

    This applies to Windows Server 2008 and other newer Windows SKUs.

    0 讨论(0)
提交回复
热议问题