asp运行过程中捕捉和保存asp错误的函数

yibin 2015-02-09 建站源码 568

在asp调试的时候,经常是根据出现的错误提示来找到错误的地方,然后作相应的修改,直到没有错误为止。这不仅是asp调试的方法,也是所有程序的调试方 法。在asp的运行过程中,如何更明白的显示出现的错误在哪个页面,第几行,但又不能让客户端看到这个错误。所以想出来一个方法:使用on error resume next,将错误不显示到页面上,这样就不会给客户看到。但还要将错误写到一个自定义的日志文件中,以便我们开发人员方便找到错误所在的页面,或者对出现 错误的情况下,转向一个自定义的页面上来,这样能给客户更好的体验。    过程名:catch(str)
    功   能:清除IIS的错误提示信息,自定义错误提示返回给用户,并将出错信息保存到txt文件(当然你也可以稍做修改转向自定义页面等)
    使用方法:
    view sourceprint?1 <%
    2 on error resume next
    3 '你的代码,如数据库连接
    4 call catch("显示给用户的提示信息")
    5 %>
    (鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助
    catch 函数的具体代码如下:
    view sourceprint?001 <%
    002 option explicit
    003 '例一---------------------------
    004 '必须和on error resume next一起使用,但在网页没有正式发布之前最好将其注释掉,以免在调试时看不到出错详细信息
    005 on error resume next
    006 'i没有定义,会出错,使用catch清除错误并保存到记事本
    007
    008 call catch("页面无法访问")
    009 '-------------------------------
    010 '例二---------------------------
    011 function conn()
    012   '必须和on error resume next一起使用
    013   on error resume next
    014   '……你的连接数据库代码
    015   call catch("数据库打开错误")
    016 end function
    017 '-------------------------------
    018 sub catch(str)
    019     if err.number <> 0 then
    020     dim tmp,path
    021     '错误日志绝对路径,如"/error_log.txt"
    022     path = "/table/error_log.txt"
    023     tmp = tmp & "出错页面:" & geturl & vbcrlf
    024     tmp = tmp & "错误时间:" & now() & vbcrlf
    025     tmp = tmp & "来访IP:" & ip & vbcrlf
    026     tmp = tmp & "提示信息:" & str & vbcrlf
    027     tmp = tmp & "错误代号:" & err.number & vbcrlf
    028     tmp = tmp & "错误信息:" & err.description & vbcrlf
    029     tmp = tmp & "应用程序:" & err.source & vbcrlf & vbcrlf & vbcrlf
    030     tmp = tmp & file_read(path)
    031     call file_save(tmp,path,1)
    032     err.clear()
    033     die(str)
    034     end if
    035 end sub
    036 '以下为catch所用到的函数--------------------
    037 sub echo(str)
    038     response.write(str)
    039 end sub
    040 sub die(str)
    041     echo(str) : response.end()
    042 end sub
    043 function ip()
    044     ip = request.servervariables("remote_addr")
    045 end function
    046 '获取当前URL
    047 function geturl()
    048     dim tmp
    049     if lcase(request.servervariables("https")) = "off" then
    050     tmp = "http://"
    051     else
    052     tmp = "https://"
    053     end if
    054     tmp = tmp & request.servervariables("server_name")
    055     if request.servervariables("server_port") <> 80 then
    056     tmp = tmp & ":" & request.servervariables("server_port")
    057     end if
    058     tmp = tmp & request.servervariables("url")
    059     if trim(request.querystring) <> "" then
    060     tmp = tmp & "?" & trim(request.queryString)
    061     end if
    062     geturl = tmp
    063 end function
    064 '函数:读取文件内容到字符串
    065 function file_read(path)
    066     dim tmp : tmp = "false"
    067     if not file_exists(path) then file_read = tmp : exit function
    068     dim stream : set stream = server.CreateObject("ADODB.Stream")
    069     with stream
    070     .type = 2 '文本类型
    071     .mode = 3 '读写模式
    072     .charset = "gb2312"
    073     .open
    074     .loadfromfile(server.MapPath(path))
    075     tmp = .readtext()
    076     end with
    077     stream.close : set stream = nothing
    078     file_read = tmp
    079 end function
    080 '函数:保存字符串到文件
    081 function file_save(str,path,model)
    082     if model<>0 and model<>1 then model=1
    083     if model=0 and file_exists(path) then file_save=true : exit function
    084     dim stream : set stream = server.CreateObject("ADODB.Stream")
    085     with stream
    086     .type = 2 '文本类型
    087     .charset = "gb2312"
    088     .open
    089     .writetext str
    090     .savetofile(server.MapPath(path)),model+1
    091     end with
    092     stream.close : set stream = nothing
    093     file_save = file_exists(path)
    094 end function
    095 '函数:检测文件/文件夹是否存在
    096 function file_exists(path)
    097     dim tmp : tmp = false
    098     dim fso : set fso = server.CreateObject("Scripting.FilesyStemObject")
    099     if fso.fileexists(server.MapPath(path)) then tmp = true
    100     if fso.folderexists(server.MapPath(path)) then tmp = true
    101     set fso = nothing
    102     file_exists = tmp
    103 end function
    104 %>

扫码添加微信

13013082126 扫描微信 建站咨询 优化咨询