Capture and save ASP Run Error function code _ Application Tips

Source: Internet
Author: User
Tags servervariables string to file
Procedure name: catch (str)
How to use:
Copy Code code as follows:

On Error Resume Next
' Your code, such as a database connection
Call catch ("message displayed to the user")

Features: Clear IIS error messages, custom error prompts to return to the user, and the error message to save to TXT file (of course, you can also slightly modify to customize the page, etc.)

Code:
Copy Code code as follows:

<%
Option Explicit
' Example a---------------------------
' must be used with On Error Resume Next, but it is best to comment out the page before it is officially released so that you do not see error details during debugging
On Error Resume Next
' I have no definition, there will be an error, use catch to clear the error and save to Notepad
I
Call catch ("page unreachable")
'-------------------------------
' Case Two---------------------------
Function Conn ()
' must be used with On Error Resume Next
On Error Resume Next
'........... Your connection database code.
Call catch ("Database open Error")
End Function
'-------------------------------
Sub catch (str)
If Err.Number <> 0 Then
Dim Tmp,path
' Error log absolute path, such as '/error_log.txt '
Path = "/table/error_log.txt"
TMP = tmp & "error page:" & Geturl & vbCrLf
TMP = tmp & "Error time:" & Now () & vbCrLf
TMP = tmp & "Visiting IP:" & IP & vbCrLf
TMP = TMP & "Hint:" & Str & vbCrLf
TMP = tmp & "error code:" & Err.Number & vbCrLf
TMP = TMP & "error message:" & Err.Description & vbCrLf
TMP = TMP & "Application:" & Err.Source & vbCrLf & vbCrLf & vbCrLf
TMP = tmp & File_read (PATH)
Call File_save (tmp,path,1)
Err.Clear ()
Die (str)
End If
End Sub
' The following are the functions used by the catch--------------------
Sub Echo (str)
Response.Write (str)
End Sub
Sub Die (str)
Echo (str): Response.End ()
End Sub
function IP ()
ip = Request.ServerVariables ("REMOTE_ADDR")
End Function
' Get current URL
function Geturl ()
Dim tmp
If LCase (Request.ServerVariables ("https") = "Off" Then
TMP = "http://"
Else
TMP = "https://"
End If
TMP = tmp & Request.ServerVariables ("SERVER_NAME")
If Request.ServerVariables ("Server_port") <> Then
TMP = tmp & ":" & Request.ServerVariables ("Server_port")
End If
TMP = tmp & Request.ServerVariables ("url")
If Trim (request.querystring) <> "" Then
TMP = tmp & "?" & Trim (Request.QueryString)
End If
Geturl = tmp
End Function
' function: Read the contents of a file to a string
function File_read (path)
Dim tmp:tmp = "false"
If not file_exists (path) then File_read = Tmp:exit function
Dim Stream:set stream = Server. CreateObject ("ADODB.") Stream ")
With Stream
. Type = 2 ' text type
. mode = 3 ' read/write mode
. CharSet = "gb2312"
. Open
. LoadFromFile (server. MapPath (PATH))
TMP =. ReadText ()
End With
Stream.close:set stream = Nothing
File_read = tmp
End Function
' function: Save string to File
function File_save (Str,path,model)
If Model<>0 and model<>1 then model=1
If Model=0 and file_exists (path) then File_save=true:exit function
Dim Stream:set stream = Server. CreateObject ("ADODB.") Stream ")
With Stream
. Type = 2 ' text type
. CharSet = "gb2312"
. Open
. WRITETEXT Str
. SaveToFile (server. MapPath (Path)), model+1
End With
Stream.close:set stream = Nothing
File_save = file_exists (path)
End Function
' Function: Detects if a file/folder exists
function file_exists (path)
Dim Tmp:tmp = False
Dim Fso:set FSO = Server. CreateObject ("Scripting.FileSystemObject")
If Fso.fileexists (server. MapPath (PATH)) Then TMP = True
If Fso.folderexists (server. MapPath (PATH)) Then TMP = True
Set fso = Nothing
file_exists = tmp
End Function
%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.