经典 ASP:捕获错误

发布于 2025-01-05 10:40:45 字数 137 浏览 0 评论 0原文

是否有可能在全局级别捕获经典 ASP 中的所有 500 个错误?也许是 IIS 中的东西。我现在用的是II6。我喜欢捕获错误消息,然后将其存储在数据库中。我知道它在 ASPX 页面中是可能的,但不确切知道你在经典的 asp 中是如何做的。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九八野马 2025-01-12 10:40:45

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

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

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

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

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

黑白记忆 2025-01-12 10:40:45

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

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

%>

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

%>
我不咬妳我踢妳 2025-01-12 10:40:45

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

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

On Error Resume Next
... do something...
If Err.Number <> 0 Then
... handle error
end if

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
笑咖 2025-01-12 10:40:45

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

在此处输入图像描述

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

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:

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文