现在的位置: 首页 > 编程语言 > 正文

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

2020年06月02日 编程语言 ⁄ 共 494字 ⁄ 字号 评论关闭

  在asp调试的时候,经常是根据出现的错误提示来找到错误的地方,然后作相应的修改,直到没有错误为止。这不仅是asp调试的方法,也是所有程序的调试方法。下面学步园小编来讲解下asp运行过程中怎样捕捉和保存asp错误的函数?

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

  在asp的运行过程中,如何更明白的显示出现的错误在哪个页面,第几行,但又不能让客户端看到这个错误。所以想出来一个方法:使用onerrorresumenext,将错误不显示到页面上,这样就不会给客户看到。但还要将错误写到一个自定义的日志文件中,以便我们开发人员方便找到错误所在的页面,或者对出现错误的情况下,转向一个自定义的页面上来,这样能给客户更好的体验。过程名:catch(str)

  功能:清除IIS的错误提示信息,自定义错误提示返回给用户,并将出错信息保存到txt文件(当然你也可以稍做修改转向自定义页面等)

  使用方法:

  viewsourceprint?1<%   2onerrorresumenext   3'你的代码,如数据库连接   4callcatch("显示给用户的提示信息")   5%>

  (鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)

  catch函数的具体代码如下:

  viewsourceprint?001<%   002optionexplicit   003'例一---------------------------   004'必须和onerrorresumenext一起使用,但在网页没有正式发布之前最好将其注释掉,以免在调试时看不到出错详细信息   005onerrorresumenext   006'i没有定义,会出错,使用catch清除错误并保存到记事本   007   008callcatch("页面无法访问")   009'-------------------------------   010'例二---------------------------   011functionconn()   012'必须和onerrorresumenext一起使用   013onerrorresumenext   014'……你的连接数据库代码   015callcatch("数据库打开错误")   016endfunction   017'-------------------------------   018subcatch(str)   019iferr.number<>0then

  020dimtmp,path

  021'错误日志绝对路径,如"/error_log.txt"

  022path="/table/error_log.txt"

  023tmp=tmp&"出错页面:"&geturl&vbcrlf

  024tmp=tmp&"错误时间:"&now()&vbcrlf

  025tmp=tmp&"来访IP:"&ip&vbcrlf

  026tmp=tmp&"提示信息:"&str&vbcrlf

  027tmp=tmp&"错误代号:"&err.number&vbcrlf

  028tmp=tmp&"错误信息:"&err.description&vbcrlf

  029tmp=tmp&"应用程序:"&err.source&vbcrlf&vbcrlf&vbcrlf

  030tmp=tmp&file_read(path)

  031callfile_save(tmp,path,1)

  032err.clear()

  033die(str)

  034endif

  035endsub

  036'以下为catch所用到的函数--------------------

  037subecho(str)

  038response.write(str)

  039endsub

  040subdie(str)

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

  041echo(str):response.end()

  042endsub

  043functionip()

  044ip=request.servervariables("remote_addr")

  045endfunction

  046'获取当前URL

  047functiongeturl()

  048dimtmp

  049iflcase(request.servervariables("https"))="off"then

  050tmp="http://"

  051else

  052tmp="https://"

  053endif

  054tmp=tmp&request.servervariables("server_name")

  055ifrequest.servervariables("server_port")<>80then

  056tmp=tmp&":"&request.servervariables("server_port")

  057endif

  058tmp=tmp&request.servervariables("url")

  059iftrim(request.querystring)<>""then

  060tmp=tmp&"?"&trim(request.queryString)

  061endif

  062geturl=tmp

  063endfunction

  064'函数:读取文件内容到字符串

  065functionfile_read(path)

  066dimtmp:tmp="false"

  067ifnotfile_exists(path)thenfile_read=tmp:exitfunction

  068dimstream:setstream=server.CreateObject("ADODB.Stream")

  069withstream

  070.type=2'文本类型

  071.mode=3'读写模式

  072.charset="gb2312"

  073.open

  074.loadfromfile(server.MapPath(path))

  075tmp=.readtext()

  076endwith

  077stream.close:setstream=nothing

  078file_read=tmp

  079endfunction

  080'函数:保存字符串到文件

  081functionfile_save(str,path,model)

  082ifmodel<>0andmodel<>1thenmodel=1

  083ifmodel=0andfile_exists(path)thenfile_save=true:exitfunction

  084dimstream:setstream=server.CreateObject("ADODB.Stream")

  085withstream

  086.type=2'文本类型

  087.charset="gb2312"

  088.open

  089.writetextstr

  090.savetofile(server.MapPath(path)),model+1

  091endwith

  092stream.close:setstream=nothing

  093file_save=file_exists(path)

  094endfunction

  095'函数:检测文件/文件夹是否存在

  096functionfile_exists(path)

  097dimtmp:tmp=false

  098dimfso:setfso=server.CreateObject("Scripting.FilesyStemObject")

  099iffso.fileexists(server.MapPath(path))thentmp=true

  100iffso.folderexists(server.MapPath(path))thentmp=true

  101setfso=nothing

  102file_exists=tmp

  103endfunction

  104%>

  以上就是关于“asp运行过程中怎样捕捉和保存asp错误的函数”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

抱歉!评论已关闭.