7

是否可以在全局级别捕获 Classic ASP 中的所有 500 个错误?也许在 IIS 中。我目前正在使用 II6。我喜欢捕获错误消息,然后将其存储在数据库中。我知道它在 ASPX 页面中是可能的,但不知道你在经典 asp 中的具体表现。

谢谢

4

4 回答 4

16

是的,创建一个将错误详细信息记录到数据库的 asp 页面,并将其设置为 IIS 中的 500 处理程序页面,如下所示。

使用 Server.GetLastError 对象在处理程序脚本中获取错误的详细信息。

在 500 处理程序中记录到文本文件而不是数据库以提高弹性可能是个好主意。

在 IIS 中设置自定义 500 处理程序

于 2012-02-13T16:33:42.140 回答
7

补充 Jon 的回答,使用此脚本将错误写入日志文件:

<%
'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

%>
于 2013-10-07T02:44:41.137 回答
5

经典 ASP 中的错误处理是一件非常痛苦的事情。您可以使用 捕获您认为将发生on error resume next的错误,然后在以下代码行中检查错误代码。

或者,您可以扫描服务器日志以查找 500 错误。或在 IIS 设置中设置“500 错误”页面。

On Error Resume Next
... do something...
If Err.Number <> 0 Then
... handle error
end if
于 2012-02-13T16:35:47.127 回答
-1

要添加到@Jon Eastwood 的答案 - 如果您使用的是 IIS 7.5,那么您将根据下图查找“.NET 错误页面”,而不是“自定义错误”:

在此处输入图像描述

这适用于 Windows Server 2008 和其他较新的 Windows SKU。

于 2015-07-29T05:35:03.417 回答