当前位置:中国站长下载文章中心网页编程ASP编程 → 浅谈asp编程中的测试打印、有效性检查及错误处理

浅谈asp编程中的测试打印、有效性检查及错误处理

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-13 0:46:27
strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误时间:" + CStr(Now()) + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误类型:Err错误" + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误号 :" + CStr(Err.Number) + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误源 :" + Err.Source + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误描述:" + Err.Description + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
  
   ''清空Err
   Err.Clear
  
   ''在Redirect之前写日志
   WriteLog(strLogMsg)
  
   strMsg = Server.UrlEncode(strMsg)
   ''如果是调试则显示错误代码
   if Application("DEBUG") = 1 then
   Response.Redirect("../include/error.asp?" + _
   "ErrText="&strMsg&"")
   else
   Response.Redirect("../include/error.asp")
   end if
  
   ''如果是程序错误,则写日志
   else
  
   strMsg = strMsg + "*********************************************************************************************" + "<BR>"
   strMsg = strMsg + "错误时间:" + CStr(Now()) + "<BR>"
  
   strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
   strLogMsg = strLogMsg + "错误时间:" + CStr(Now()) + Chr(13) + Chr(10)
  
   Dim strWhichErr
   Select Case a_intErrCode
  
   Case 99001 ''程序错误从99001开始
   strWhichErr = "断言错误"
   Case 99002
   strWhichErr = "Case Else 错误"
   Case else
   strWhichErr = "未知的错误"
   End Select
  
   strMsg = strMsg + "错误类型:" + strWhichErr + "<BR>"
   if a_strErrText <> "" then
   strMsg = strMsg + "错误描述:" + a_strErrText + "<BR>"
   end if
   strMsg = strMsg + "*********************************************************************************************" + "<BR>"
  
   strLogMsg = strLogMsg + "错误类型:" + strWhichErr + Chr(13) + Chr(10)
   if a_strErrText <> "" then
   strLogMsg = strLogMsg + "错误描述:" + a_strErrText + Chr(13) + Chr(10)
   end if
   strLogMsg = strLogMsg + "*********************************************************************************************" + Chr(13) + Chr(10)
  
   ''写日志
   WriteLog(strLogMsg)
  
   ''如果是调试状态则指向错误页
   if Application("DEBUG") = 1 then
   strMsg = Server.UrlEncode(strMsg)
   Response.Redirect("http://server1/4biz/include/error.asp?" + _
   "ErrText="&strMsg&"")
   end if
   end if
  End Sub
  
  
  ''--------------------------------------------------------
  ''Name: WriteLog
  ''Argument: a_strMsg:日志内容
  ''Return:
  ''Description: 错误处理
  ''Hitory: Create by Yaozhigang
  ''--------------------------------------------------------
  Function WriteLog(a_strMsg)
   Dim strFileName
  
   if Application("g_strLogFileName") = "" then
   strFileName = "c:\AspLog.txt"
   else
   strFileName = Application("g_strLogFileName")
   end if
  
   Dim objFSO
   Dim objFile
   Set objFSO = CreateObject("Scripting.FileSystemObject")
  
   ''只有一个人写日志
   Application.Lock
  
   Set objFile = objFSO.OpenTextFile(strFileName, 8, True, 0)
   objFile.Write(a_strMsg)
   objFile.Close
  
   Application.UnLock
  
   Set objFile = Nothing
   Set objFSO = Nothing
  End Function
  
   以上几个函数及过程的作用是处理数据库错误,注意使用时在global.asp里加上on eroor resume,以使错误处理程序能够运行。其中可以显示错误代码、写日志文件,详细内容我就不做解释了,请自己研究一下源代码。
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。

上一页  [1] [2]