- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > ASP >
- 捕捉并保存ASP運行錯誤的函數代碼
過(guò)程名:catch(str)
使用方法:
代碼如下:
on error resume next
'你的代碼,如數據庫連接
call catch("顯示給用戶(hù)的提示信息")
功能:清除IIS的錯誤提示信息,自定義錯誤提示返回給用戶(hù),并將出錯信息保存到txt文件(當然你也可以稍做修改轉向自定義頁(yè)面等)
代碼:
代碼如下:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網(wǎng)頁(yè)沒(méi)有正式發(fā)布之前最好將其注釋掉,以免在調試時(shí)看不到出錯詳細信息
on error resume next
'i沒(méi)有定義,會(huì )出錯,使用catch清除錯誤并保存到記事本
i
call catch("頁(yè)面無(wú)法訪(fǎng)問(wèn)")
'-------------------------------
'例二---------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連接數據庫代碼
call catch("數據庫打開(kāi)錯誤")
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯誤日志絕對路徑,如"/error_log.txt"
path = "/table/error_log.txt"
tmp = tmp & "出錯頁(yè)面:" & geturl & vbcrlf
tmp = tmp & "錯誤時(shí)間:" & now() & vbcrlf
tmp = tmp & "來(lái)訪(fǎng)IP:" & ip & vbcrlf
tmp = tmp & "提示信息:" & str & vbcrlf
tmp = tmp & "錯誤代號:" & err.number & vbcrlf
tmp = tmp & "錯誤信息:" & err.description & vbcrlf
tmp = tmp & "應用程序:" & 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所用到的函數--------------------
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
'獲取當前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
'函數:讀取文件內容到字符串
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 '文本類(lèi)型
.mode = 3 '讀寫(xiě)模式
.charset = "gb2312"
.open
.loadfromfile(server.MapPath(path))
tmp = .readtext()
end with
stream.close : set stream = nothing
file_read = tmp
end function
'函數:保存字符串到文件
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 '文本類(lèi)型
.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 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ā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站