VB写日志类

Option Explicit
Public gstrLogPath As String

Private Sub Class_Initialize()
    gstrLogPath = App.Path & "/Log"     'Log路径
End Sub

'*************************************************************************************************
'*函数名:  WriteErrLog
'*程序功能:写日志
'*开发人员:inrg
'*异动人员:无
'*传入值:  1.strProgramName -- 类模块名
'*          2.strProcName -- 函数名
'*          3.strErrLog -- ErrorLog內容 eg:Err.Description
'*          4.strErrNumber -- Err对象的错误编号(可选) eg:Err.Number
'*          5.strErrDesc -- Err对象的错误描叙(可选) eg:Err.Description
'*回传值:  boolean  成功 = true,失败 = false
'*************************************************************************************************
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
    Dim FileHandle As Long
    Dim strTxtName As String        '
    Dim FSOLog As Object            '
   
    On Error GoTo WriteLogFileErr:

    Set FSOLog = CreateObject("Scripting.FileSystemObject")
   
    WriteErrLog = True

    If (gstrLogPath = "") Then
        WriteErrLog = False
        GoTo WriteLogFileErr:
    End If
   
    '文件名
    strTxtName = Format(Date, "YYYYMMDD") & ".log"
   
    '判断是否有Log文件夹
    If Dir(gstrLogPath, vbDirectory) = "" Then
            MkDir gstrLogPath
    End If

    FileHandle = FreeFile
    Open (gstrLogPath & "/" & strTxtName) For Append As #FileHandle
    Lock #FileHandle
        Print #FileHandle, "************************************************************************"
        Print #FileHandle, "Date & Time: " & Format(Time, "HH:MM:SS")
        Print #FileHandle, "Program Name: " & strProgramName
        Print #FileHandle, "Procedure Name: " & strProcName
        Print #FileHandle, "Error Number: " & strErrNumber
        Print #FileHandle, "Error Desc: " & strErrDesc
        Print #FileHandle, "Log: " & strErrLog
        Print #FileHandle, "************************************************************************" & vbNewLine
    Unlock #FileHandle
    Close #FileHandle
   
    Set FSOLog = Nothing
    Exit Function

WriteLogFileErr:
        WriteErrLog = False
End Function
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值