捕捉并保存ASP運(yùn)行錯(cuò)誤的函數(shù)代碼
更新時(shí)間:2012年03月03日 21:54:47 作者:
捕捉并保存ASP運(yùn)行錯(cuò)誤的函數(shù)代碼,需要獲取asp代碼運(yùn)行錯(cuò)誤的朋友可以參考下
過(guò)程名:catch(str)
使用方法:
on error resume next
'你的代碼,如數(shù)據(jù)庫(kù)連接
call catch("顯示給用戶的提示信息")
功能:清除IIS的錯(cuò)誤提示信息,自定義錯(cuò)誤提示返回給用戶,并將出錯(cuò)信息保存到txt文件(當(dāng)然你也可以稍做修改轉(zhuǎn)向自定義頁(yè)面等)
代碼:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網(wǎng)頁(yè)沒(méi)有正式發(fā)布之前最好將其注釋掉,以免在調(diào)試時(shí)看不到出錯(cuò)詳細(xì)信息
on error resume next
'i沒(méi)有定義,會(huì)出錯(cuò),使用catch清除錯(cuò)誤并保存到記事本
i
call catch("頁(yè)面無(wú)法訪問(wèn)")
'-------------------------------
'例二---------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連接數(shù)據(jù)庫(kù)代碼
call catch("數(shù)據(jù)庫(kù)打開(kāi)錯(cuò)誤")
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯(cuò)誤日志絕對(duì)路徑,如"/error_log.txt"
path = "/table/error_log.txt"
tmp = tmp & "出錯(cuò)頁(yè)面:" & geturl & vbcrlf
tmp = tmp & "錯(cuò)誤時(shí)間:" & now() & vbcrlf
tmp = tmp & "來(lái)訪IP:" & ip & vbcrlf
tmp = tmp & "提示信息:" & str & vbcrlf
tmp = tmp & "錯(cuò)誤代號(hào):" & err.number & vbcrlf
tmp = tmp & "錯(cuò)誤信息:" & 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 '讀寫(xiě)模式
.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ù):檢測(cè)文件/文件夾是否存在
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ù)庫(kù)連接
call catch("顯示給用戶的提示信息")
功能:清除IIS的錯(cuò)誤提示信息,自定義錯(cuò)誤提示返回給用戶,并將出錯(cuò)信息保存到txt文件(當(dāng)然你也可以稍做修改轉(zhuǎn)向自定義頁(yè)面等)
代碼:
復(fù)制代碼 代碼如下:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網(wǎng)頁(yè)沒(méi)有正式發(fā)布之前最好將其注釋掉,以免在調(diào)試時(shí)看不到出錯(cuò)詳細(xì)信息
on error resume next
'i沒(méi)有定義,會(huì)出錯(cuò),使用catch清除錯(cuò)誤并保存到記事本
i
call catch("頁(yè)面無(wú)法訪問(wèn)")
'-------------------------------
'例二---------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連接數(shù)據(jù)庫(kù)代碼
call catch("數(shù)據(jù)庫(kù)打開(kāi)錯(cuò)誤")
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯(cuò)誤日志絕對(duì)路徑,如"/error_log.txt"
path = "/table/error_log.txt"
tmp = tmp & "出錯(cuò)頁(yè)面:" & geturl & vbcrlf
tmp = tmp & "錯(cuò)誤時(shí)間:" & now() & vbcrlf
tmp = tmp & "來(lái)訪IP:" & ip & vbcrlf
tmp = tmp & "提示信息:" & str & vbcrlf
tmp = tmp & "錯(cuò)誤代號(hào):" & err.number & vbcrlf
tmp = tmp & "錯(cuò)誤信息:" & 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 '讀寫(xiě)模式
.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ù):檢測(cè)文件/文件夾是否存在
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獲取網(wǎng)頁(yè)全部圖片地址并保存為數(shù)組的正則
ASP常用函數(shù):getIMG()獲取網(wǎng)頁(yè)全部圖片地址并保存為數(shù)組2008-03-03實(shí)現(xiàn)UTF8轉(zhuǎn)換GB2312國(guó)標(biāo)碼的asp代碼
ASP來(lái)實(shí)現(xiàn)UTF8轉(zhuǎn)換GB2312國(guó)標(biāo)碼-GB2312轉(zhuǎn)UTF-8,需要的朋友可以參考下。2010-04-04解決使用良精企業(yè)建站7.0未注冊(cè)問(wèn)題
解決使用良精企業(yè)建站7.0未注冊(cè)問(wèn)題...2007-04-04asp 關(guān)鍵詞高亮顯示(不區(qū)分大小寫(xiě))
用ASP做搜索很容易,但要實(shí)現(xiàn)智能搜索這類就比較累一點(diǎn),其實(shí)任何程序都差不多,主要還是看數(shù)據(jù)庫(kù)的處理能力,一般小網(wǎng)站ASP經(jīng)常跟ACCESS數(shù)據(jù)庫(kù)搭配2009-03-03asp內(nèi)置對(duì)象 ObjectContext 事務(wù)管理 詳解
asp內(nèi)置對(duì)象 ObjectContext 事務(wù)管理 詳解...2007-11-11asp 過(guò)濾尖括號(hào)內(nèi)所有內(nèi)容的正則代碼
正常ASP中對(duì)錄入內(nèi)容的過(guò)濾僅僅是對(duì)左尖括號(hào)和右尖括號(hào)的HTML源碼的替換,所以在頁(yè)面中顯示為左右尖括號(hào),而不是將尖括號(hào)作為HTML標(biāo)簽執(zhí)行了。2008-12-12ASP語(yǔ)言實(shí)現(xiàn)對(duì)SQL SERVER數(shù)據(jù)庫(kù)的操作
目前有很多介紹用ASP開(kāi)發(fā)網(wǎng)絡(luò)數(shù)據(jù)庫(kù)的程序例子,但絕大部分是利用ACCESS作底層數(shù)據(jù)庫(kù)。相對(duì)于ACCESS而言,SQL SERVER數(shù)據(jù)庫(kù)系統(tǒng)要復(fù)雜得多,因此在程序開(kāi)發(fā)中需要多做一些工作。筆者結(jié)合自己開(kāi)發(fā)管理信息系統(tǒng)的經(jīng)驗(yàn),在此試舉一例,與感興趣的朋友共同交流2015-09-09