捕捉并保存ASP運(yùn)行錯誤的函數(shù)代碼
更新時間:2012年03月03日 21:54:47 作者:
捕捉并保存ASP運(yùn)行錯誤的函數(shù)代碼,需要獲取asp代碼運(yùn)行錯誤的朋友可以參考下
過程名:catch(str)
使用方法:
on error resume next
'你的代碼,如數(shù)據(jù)庫連接
call catch("顯示給用戶的提示信息")
功能:清除IIS的錯誤提示信息,自定義錯誤提示返回給用戶,并將出錯信息保存到txt文件(當(dāng)然你也可以稍做修改轉(zhuǎn)向自定義頁面等)
代碼:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網(wǎng)頁沒有正式發(fā)布之前最好將其注釋掉,以免在調(diào)試時看不到出錯詳細(xì)信息
on error resume next
'i沒有定義,會出錯,使用catch清除錯誤并保存到記事本
i
call catch("頁面無法訪問")
'-------------------------------
'例二---------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連接數(shù)據(jù)庫代碼
call catch("數(shù)據(jù)庫打開錯誤")
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯誤日志絕對路徑,如"/error_log.txt"
path = "/table/error_log.txt"
tmp = tmp & "出錯頁面:" & geturl & vbcrlf
tmp = tmp & "錯誤時間:" & now() & vbcrlf
tmp = tmp & "來訪IP:" & ip & vbcrlf
tmp = tmp & "提示信息:" & str & vbcrlf
tmp = tmp & "錯誤代號:" & err.number & vbcrlf
tmp = tmp & "錯誤信息:" & err.description & vbcrlf
tmp = tmp & "應(yīng)用程序:" & err.source & vbcrlf & vbcrlf & vbcrlf
tmp = tmp & file_read(path)
call file_save(tmp,path,1)
err.clear()
die(str)
end if
end sub
'以下為catch所用到的函數(shù)--------------------
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
'獲取當(dāng)前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") <> 80 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
'函數(shù):讀取文件內(nèi)容到字符串
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 '文本類型
.mode = 3 '讀寫模式
.charset = "gb2312"
.open
.loadfromfile(server.MapPath(path))
tmp = .readtext()
end with
stream.close : set stream = nothing
file_read = tmp
end function
'函數(shù):保存字符串到文件
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 '文本類型
.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
'函數(shù):檢測文件/文件夾是否存在
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
%>
使用方法:
復(fù)制代碼 代碼如下:
on error resume next
'你的代碼,如數(shù)據(jù)庫連接
call catch("顯示給用戶的提示信息")
功能:清除IIS的錯誤提示信息,自定義錯誤提示返回給用戶,并將出錯信息保存到txt文件(當(dāng)然你也可以稍做修改轉(zhuǎn)向自定義頁面等)
代碼:
復(fù)制代碼 代碼如下:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網(wǎng)頁沒有正式發(fā)布之前最好將其注釋掉,以免在調(diào)試時看不到出錯詳細(xì)信息
on error resume next
'i沒有定義,會出錯,使用catch清除錯誤并保存到記事本
i
call catch("頁面無法訪問")
'-------------------------------
'例二---------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連接數(shù)據(jù)庫代碼
call catch("數(shù)據(jù)庫打開錯誤")
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯誤日志絕對路徑,如"/error_log.txt"
path = "/table/error_log.txt"
tmp = tmp & "出錯頁面:" & geturl & vbcrlf
tmp = tmp & "錯誤時間:" & now() & vbcrlf
tmp = tmp & "來訪IP:" & ip & vbcrlf
tmp = tmp & "提示信息:" & str & vbcrlf
tmp = tmp & "錯誤代號:" & err.number & vbcrlf
tmp = tmp & "錯誤信息:" & err.description & vbcrlf
tmp = tmp & "應(yīng)用程序:" & err.source & vbcrlf & vbcrlf & vbcrlf
tmp = tmp & file_read(path)
call file_save(tmp,path,1)
err.clear()
die(str)
end if
end sub
'以下為catch所用到的函數(shù)--------------------
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
'獲取當(dāng)前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") <> 80 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
'函數(shù):讀取文件內(nèi)容到字符串
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 '文本類型
.mode = 3 '讀寫模式
.charset = "gb2312"
.open
.loadfromfile(server.MapPath(path))
tmp = .readtext()
end with
stream.close : set stream = nothing
file_read = tmp
end function
'函數(shù):保存字符串到文件
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 '文本類型
.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
'函數(shù):檢測文件/文件夾是否存在
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
%>
相關(guān)文章
ASP+Access數(shù)據(jù)庫安全設(shè)置方法小結(jié)
Access數(shù)據(jù)庫安全設(shè)置方法小結(jié)2008-12-12
轉(zhuǎn)換中文為unicode 轉(zhuǎn)換unicode到正常文本
轉(zhuǎn)換中文為unicode 轉(zhuǎn)換unicode到正常文本...2006-10-10
ASP同一站點下gb2312和utf-8頁面?zhèn)鬟f參數(shù)亂碼的終極解決方法
要解決ASP同一站點下gb2312和utf-8頁面?zhèn)鬟f參數(shù)亂碼問題,只需嚴(yán)格做到以下4點即可。2010-12-12
在VBScript中實現(xiàn)-函數(shù)/方法名作為參數(shù)傳入另一個函數(shù)
在VBScript中實現(xiàn)-函數(shù)/方法名作為參數(shù)傳入另一個函數(shù)...2007-08-08
ASP 獲取文件擴(kuò)展名函數(shù)getFileExt()
利用了asp的instrrev獲取最后.的位置,然后進(jìn)行截取,大家可以自己測試,其實編程過程也是大量的測試,慢慢就會發(fā)現(xiàn)更多的好東西。2009-08-08
用ASP實現(xiàn)對ORACLE數(shù)據(jù)庫的操作
用ASP實現(xiàn)對ORACLE數(shù)據(jù)庫的操作...2007-03-03

