|
-
Oct 20th, 2000, 01:28 AM
#1
Hallo
To honest I don't even know where to start. I were thinking of using a text file for the logs. I need to log who and at what time a user onto the computer as well as when he logged out.
I have an idea how to write in a text file from VB...what I don't know is: When you open an existing file, and start writing in it, will it automatically add the new text at the end of the file?
If you have any other suggestion I would appreciate it!
Thanx
-
Oct 20th, 2000, 02:32 AM
#2
Addicted Member
This is a simplified version of my logging function, put this in a module.
Code:
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nSize As Long, ByVal lpBuffer As String) As Long
Public Function Logging(Optional strLogMessage)
Dim intFileNum As Integer
Dim lngLength As Long
Dim strLogFile As String
Dim strPath As String
Dim strMessage As String
Dim strUser As String
Const cLogExtension As String = ".log"
Const cUser As String = "User : "
Const cDatumTijd As String = "Date/time : "
'All these are usually in some general module.
Const cUnknown As String = "Unknown"
Const cMaxNameLength As Integer = 25
Const cMaxPathLength As Integer = 255
Const cDateTimeFormat As String = "dd-mm-yyyy hh:mm"
Const cstrEmpty As String = ""
Dim strUserId As String * cMaxNameLength
Dim strTempPath As String * cMaxPathLength
intFileNum = FreeFile
lngLength = GetTempPath(ByVal cMaxPathLength, ByVal strTempPath)
strPath = Left$(strTempPath, lngLength)
strLogFile = strPath & App.EXEName & cLogExtension
Open strLogFile For Append Access Read Write Shared As intFileNum
strMessage = cDatumTijd & Format$(CStr(Now), cDateTimeFormat)
Print #intFileNum, strMessage
lngLength = cMaxNameLength
If GetUserName(strUserId, lngLength) Then
strUser = Left$(strUserId, lngLength - 1)
End If
If Trim(strUser) = cstrEmpty Then
strUser = Environ("USERNAME")
End If
If Trim(strUser) = cstrEmpty Then
strUser = cUnknown
End If
strMessage = cUser & strUser
Print #intFileNum, strMessage
If Not IsMissing(strLogMessage) Then
strMessage = Trim(strLogMessage)
Print #intFileNum, strMessage
End If
strMessage = String(80, "-")
Print #intFileNum, strMessage
Close #intFileNum
End Function
Usage:
Call Logging ("User logged in")
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Oct 20th, 2000, 05:49 AM
#3
Hyperactive Member
I think he ment like a log file. Like when something goes wrong...
Do it in a .TXT file but call it .LOG and it will access it the same as .TXT.
-
Oct 20th, 2000, 05:51 AM
#4
Hyperactive Member
By the way, if you don't want it to write with the current file, you can just use:
Code:
Kill "C:\MyLogFile.LOG"
Hope I helped...
,Asaf Sagi
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|