Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String)
創(chuàng)新互聯(lián)專注于慶城企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站建設(shè)。慶城網(wǎng)站建設(shè)公司,為慶城等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
'
'錯(cuò)誤處理中心過(guò)程,寫(xiě)數(shù)據(jù)庫(kù)日志表或?qū)懭罩疚募?/p>
'
'strModule '模塊名稱
'strProcedure '過(guò)程名稱
'lngErrorNumber '錯(cuò)誤ID號(hào)
'strErrorDescription '錯(cuò)誤描述
'showMsg '是否顯示本過(guò)程內(nèi)錯(cuò)誤顯示信息(值:"Y" or "N")
'Error表結(jié)構(gòu)(f001 (Date)發(fā)生時(shí)間, f002 (nvarchar50)模塊名稱, f003 (nvarchar50)過(guò)程名稱, f004 (nvarchar50)錯(cuò)誤ID號(hào), _
f005 (nvarchar300)錯(cuò)誤描述,f006 (nvarchar50)版 本 號(hào), f007 (nvarchar50)用戶名稱, f008 (nvarchar50)網(wǎng)卡地址
'ErrorCode表結(jié)構(gòu) f001 (nvarchar20)錯(cuò)誤代碼, f002 (nvarchar255)錯(cuò)誤信息, f003 (numeric9)錯(cuò)誤級(jí)別
' 級(jí)別說(shuō)明: '10'以下,一般錯(cuò)誤,不影響操作
' '11-20',嚴(yán)重錯(cuò)誤,不能操作,程序執(zhí)行退出
On Error GoTo ErrorHandle
Dim strMessage As String
Dim strCaption As String
Dim sVer As String
Dim intLogFile As Integer
Dim Res As New ADODB.Recordset
Dim ResErrorCode As New ADODB.Recordset
Dim strSQL As String
'對(duì)應(yīng)錯(cuò)誤號(hào),從ErrorCode表中找到對(duì)應(yīng)的錯(cuò)誤信息,0-1000 錯(cuò)誤號(hào)保留給VB
DBOpen ResErrorCode, "select * from errorcode where f001='" lngErrorNumber "'"
If Not (ResErrorCode.EOF Or ResErrorCode.BOF) Then
strMessage = ResErrorCode.Fields("f002")
If ResErrorCode.Fields("f003") 10 Then
MsgBox "產(chǎn)生一個(gè)嚴(yán)重錯(cuò)誤,可能影響到系統(tǒng)的可操作性,請(qǐng)立即聯(lián)系本系統(tǒng)開(kāi)發(fā)人員!", vbCritical, "嚴(yán)重錯(cuò)誤"
End If
End If
'寫(xiě)錯(cuò)誤入文件----------------------------
intLogFile = FreeFile
Open App.Path "\" strIni.LogFile For Append As #intLogFile
Print #intLogFile, "***錯(cuò)誤"; VBA.Now "*** " "Version:" _
str$(App.Major) "." str$(App.Minor) "." Format(App.Revision, "0000")
Print #intLogFile, "Error: " lngErrorNumber
Print #intLogFile, "Description: " strErrorDescription
Print #intLogFile, "Module: " strModule
Print #intLogFile, "Procedure: " strProcedure
Print #intLogFile, ""
Close #intLogFile
If Len(strMessage) 2 Then strErrorDescription = strMessage
strMessage = "錯(cuò)誤: " "(" lngErrorNumber ")" strErrorDescription vbCrLf vbCrLf _
"模塊:" strModule "; 過(guò)程:" strProcedure
sVer = Trim(str$(App.Major) "." str$(App.Minor) "." _
Format(App.Revision, "0000"))
strCaption = "錯(cuò)誤 Version: " sVer
'寫(xiě)錯(cuò)誤入數(shù)據(jù)庫(kù)表--------------------------
strSQL = "insert into error(f001,f002,f003,f004,f005,f006,f007,f008) values(" _
DateFmtB VBA.Now DateFmtE "," _
IIf(Len(Trim(strModule)) = 0, "null", "'" strModule "'") "," _
IIf(Len(Trim(strProcedure)) = 0, "null", "'" strProcedure "'") "," _
IIf(Len(Trim(lngErrorNumber)) = 0, "null", "'" lngErrorNumber "'") "," _
IIf(Len(Trim(strErrorDescription)) = 0, "null", "'" Replace(strErrorDescription, "'", "") "'") "," _
IIf(Len(Trim(sVer)) = 0, "null", "'" sVer "'") "," _
IIf(Len(Trim(sUserName)) = 0, "null", "'" sUserName "'") "," _
IIf(Len(Trim(sVer)) = 0, "null", "'" EthernetNO "'") ")"
Cn.Execute strSQL
'是否顯示未知錯(cuò)誤信息
If Trim(UCase(showMsg)) = "Y" Then MsgBox strMessage, vbCritical, strCaption
PROC_EXIT:
Set Res = Nothing
Set ResErrorCode = Nothing
Exit Sub
ErrorHandle:
Resume Next
其實(shí)有很多種方法的,例如用log4net就可以實(shí)現(xiàn)的,再配置文件設(shè)一些參數(shù),在需要記錄日志的地方調(diào)用就行
很多種方法,最常用的一是用文本文件或數(shù)據(jù)庫(kù)記錄,二是調(diào)用API函數(shù)直接寫(xiě)入系統(tǒng)日志或應(yīng)用程序日志
'記錄數(shù)據(jù)
open "c:\log.txt" For Output As #2 ’寫(xiě)文件
Print #2, "用戶:" USERNAME "操作時(shí)間:" date " " time "操作項(xiàng)目:登陸系統(tǒng)!"
Close #2
' 查看數(shù)據(jù),并且在list1中一條條顯示出來(lái)
Open "c:\log.txt" For Input As #1 ’讀文件
Dim i As String
Do While Not EOF(1)
Line Input #1, i '逐行把1號(hào)文件讀入到變量i
list1.AddItem i
Loop
Close #1