Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event logging

Status
Not open for further replies.

ITphile

IS-IT--Management
Nov 27, 2001
51
IN
Hi all,


I need to have a log file where all the events doen in my VB program will be recorded with userid and date/time of the event.


Thanks

ITphile
 
Just make a public sub that opens a logfile and appends any string you want to it.
ex:

(if userid is a global variable)
Private Sub WriteLog(strEvent as String)
Open "c:\test.txt" For Append As #1
Print #1, cstr(userid) & format(date(), _
"m/d/yyyy" & "test")
Close #1
End Sub


 
Write it as a function which returns boolean and you can can easily error check it.

(if userid is a global variable)
Private Function WriteLog(strEvent as String) AS Boolean
On Error GoTo Error_Handler

Open "c:\test.txt" For Append As #1
Print #1, cstr(userid) & format(date(), _
"m/d/yyyy" & "test")
Close #1

WriteLog = True

:Success_Exit
End
:Error_Handler
WriteLog = False
Resume Success_Exit

End Function


Hey presto quick n easy handler

Give a man a program and tomorrow he will be hungry.
Teach a man to program and he will never hunger again.
--Sunr¿se

 
You can let VB handle the file opening/closing bit of the log file by using the app.LogEvent command.

At the start of you program you need to call

App.StartLogging "c:\mylog.log", vbLogToFile

then when you want to log something call

App.LogEvent "String to log.", EventType

here event type can be one of the following, vbLogEventTypeError, vbLogEventTypeWarning or vbLogEventTypeInformation.

I like to keep log files in the application path, this is easy to do by using

App.StartLogging App.Path & "\mylog.log", vbLogToFile

happy logging!

Matt
 
Opening and closing any file on each event occurance in a program will bog down mst any / all programms. I have done this, with a few programs and decided that the app should open the log file ONCE (at the start) and close it when the app is closed. As a MINOR comment on most of the above, you should not generally use a specific file handle to open/close files, but obtain the handle through "freeFile".

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
you can also log to the system log on an nt/2000 box.

instead of using VbLogToFile use VbLogToNT then you can use the event viewer.

Mat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top