VB错误处理中心过程,写数据库日志表或写日志文件

 

 1 Option Explicit
 2 Public gstrLogPath As String
 3 
 4 Private Sub Class_Initialize()
 5     gstrLogPath = App.Path & "\Log"     'Log路径
 6 End Sub
 7 
 8 '*************************************************************************************************
 9 '*函数名:  WriteErrLog
10 '*程序功能:写日志
11 '*开发人员:inrg
12 '*异动人员:无
13 '*传入值:  1.strProgramName -- 类模块名
14 '*          2.strProcName -- 函数名
15 '*          3.strErrLog -- ErrorLog內容 eg:Err.Description
16 '*          4.strErrNumber -- Err对象的错误编号(可选) eg:Err.Number
17 '*          5.strErrDesc -- Err对象的错误描叙(可选) eg:Err.Description
18 '*回传值:  boolean  成功 = true,失败 = false
19 '*************************************************************************************************
20 Public Static Function WriteErrLog(ByVal strProgramName As String, ByVal strProcName As String, ByVal strErrLog As String, Optional ByVal strErrNumber As String, Optional ByVal strErrDesc As String) As Boolean
21     Dim FileHandle As Long
22     Dim strTxtName As String        '
23     Dim FSOLog As Object            '
24     
25     On Error GoTo WriteLogFileErr:
26 
27     Set FSOLog = CreateObject("Scripting.FileSystemObject")
28     
29     WriteErrLog = True
30 
31     If (gstrLogPath = "") Then
32         WriteErrLog = False
33         GoTo WriteLogFileErr:
34     End If
35     
36     '文件名
37     strTxtName = Format(Date, "YYYYMMDD") & ".log"
38     
39     '判断是否有Log文件夹
40     If Dir(gstrLogPath, vbDirectory) = "" Then
41             MkDir gstrLogPath
42     End If
43 
44     FileHandle = FreeFile
45     Open (gstrLogPath & "\" & strTxtName) For Append As #FileHandle
46     Lock #FileHandle
47         Print #FileHandle, "************************************************************************"
48         Print #FileHandle, "Date & Time: " & Format(Time, "HH:MM:SS")
49         Print #FileHandle, "Program Name: " & strProgramName
50         Print #FileHandle, "Procedure Name: " & strProcName
51         Print #FileHandle, "Error Number: " & strErrNumber
52         Print #FileHandle, "Error Desc: " & strErrDesc
53         Print #FileHandle, "Log: " & strErrLog
54         Print #FileHandle, "************************************************************************" & vbNewLine
55     Unlock #FileHandle
56     Close #FileHandle
57     
58     Set FSOLog = Nothing
59     Exit Function
60 
61 WriteLogFileErr:
62         WriteErrLog = False
63 End Function

 

 1 Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String)
 2     '
 3     '错误处理中心过程,写数据库日志表或写日志文件
 4     '
 5     'strModule           '模块名称
 6     'strProcedure        '过程名称
 7     'lngErrorNumber      '错误ID号
 8     'strErrorDescription '错误描述
 9     'showMsg             '是否显示本过程内错误显示信息(值:"Y" or "N")
10     
11     'Error表结构(f001 (Date)发生时间,    f002 (nvarchar50)模块名称, f003 (nvarchar50)过程名称, f004 (nvarchar50)错误ID号, _
12                  f005 (nvarchar300)错误描述,f006 (nvarchar50)版 本 号, f007 (nvarchar50)用户名称, f008 (nvarchar50)网卡地址
13     'ErrorCode表结构 f001 (nvarchar20)错误代码,  f002 (nvarchar255)错误信息, f003 (numeric9)错误级别
14     '         级别说明: '10'以下,一般错误,不影响操作
15     '                   '11-20',严重错误,不能操作,程序执行退出
16     
17     On Error GoTo ErrorHandle
18     Dim strMessage As String
19     Dim strCaption As String
20     Dim sVer As String
21     Dim intLogFile As Integer
22     Dim Res As New ADODB.Recordset
23     Dim ResErrorCode As New ADODB.Recordset
24     Dim strSQL As String
25     
26     '对应错误号,从ErrorCode表中找到对应的错误信息,0-1000 错误号保留给VB
27     DBOpen ResErrorCode, "select * from errorcode where f001='" & lngErrorNumber & "'"
28     If Not (ResErrorCode.EOF Or ResErrorCode.BOF) Then
29         strMessage = ResErrorCode.Fields("f002")
30         If ResErrorCode.Fields("f003") > 10 Then
31             MsgBox "产生一个严重错误,可能影响到系统的可操作性,请立即联系本系统开发人员!", vbCritical, "严重错误"
32         End If
33     End If
34           
35     '写错误入文件----------------------------
36     
37     intLogFile = FreeFile
38     Open App.Path & "\" & strIni.LogFile For Append As #intLogFile
39     Print #intLogFile, "***错误"; VBA.Now & "*** " & "Version:" & _
40           str$(App.Major) & "." & str$(App.Minor) & "." & Format(App.Revision, "0000")
41     Print #intLogFile, "Error: " & lngErrorNumber
42     Print #intLogFile, "Description: " & strErrorDescription
43     Print #intLogFile, "Module: " & strModule
44     Print #intLogFile, "Procedure: " & strProcedure
45     Print #intLogFile, ""
46     Close #intLogFile
47     
48     If Len(strMessage) > 2 Then strErrorDescription = strMessage
49     
50     strMessage = "错误: " & "(" & lngErrorNumber & ")" & strErrorDescription & vbCrLf & vbCrLf & _
51                  "模块:" & strModule & ";  过程:" & strProcedure 
52     sVer = Trim(str$(App.Major) & "." & str$(App.Minor) & "." & _
53                  Format(App.Revision, "0000"))
54     strCaption = "错误 Version: " & sVer
55                            
56     '写错误入数据库表--------------------------
57     strSQL = "insert into error(f001,f002,f003,f004,f005,f006,f007,f008) values(" _
58                 & DateFmtB & VBA.Now & DateFmtE & "," _
59                 & IIf(Len(Trim(strModule)) = 0, "null", "'" & strModule & "'") & "," _
60                 & IIf(Len(Trim(strProcedure)) = 0, "null", "'" & strProcedure & "'") & "," _
61                 & IIf(Len(Trim(lngErrorNumber)) = 0, "null", "'" & lngErrorNumber & "'") & "," _
62                 & IIf(Len(Trim(strErrorDescription)) = 0, "null", "'" & Replace(strErrorDescription, "'", "") & "'") & "," _
63                 & IIf(Len(Trim(sVer)) = 0, "null", "'" & sVer & "'") & "," _
64                 & IIf(Len(Trim(sUserName)) = 0, "null", "'" & sUserName & "'") & "," _
65                 & IIf(Len(Trim(sVer)) = 0, "null", "'" & EthernetNO & "'") & ")"
66           
67    
68     Cn.Execute strSQL
69          
70     '是否显示未知错误信息
71     If Trim(UCase(showMsg)) = "Y" Then MsgBox strMessage, vbCritical, strCaption
72     
73 PROC_EXIT:
74     Set Res = Nothing
75     Set ResErrorCode = Nothing
76     Exit Sub
77 ErrorHandle:
78     Resume Next
79 End Sub

 

posted @ 2012-09-08 11:47  wxiuming  阅读(689)  评论(0)    收藏  举报