Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 27, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Basic Event Logging

Any production application should include event logging for troubleshooting and auditing purposes. This code can be placed in any module and called whenever you have an error or event that you want to log.
Public Function LogEvent(sFileName As String, sEvent As String) As Boolean
'   Use to log errors or any other events, useful for audits and troubleshooting
'   SYNTAX: if LogEvent( app.path & "\" & app.title & "Log.txt", "System Exploded") = True then ....
'   Bob Feldsien, MSCD
Dim ff As Long
On Error GoTo ErrHandler   
    ff = FreeFile
    Open sFileName For Append As #ff    '   Will create file if it doesn't already exist
    Print #ff, Now() & ": " & sEvent
    Close #ff
    LogEvent = True
    Exit Function
    LogEvent = False
    Exit Function
End Function
Public Function DeleteLog(sFileName As String) As Boolean
'   Call from Sub Main to recreate file every time app is run
'   SYNTAX: DeleteLog app.path & "\" & app.title & "Log.txt"
    On Error Resume Next
    If Dir(sFileName) <> "" Then Kill sFileName
    DeleteLog = True    '   if previous line fails, then file didn't exist anyway
End Function
Bob Feldsien
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date