Results 1 to 4 of 4

Thread: Keeping Log-file

  1. #1
    Guest

    Unhappy

    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

  2. #2
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  3. #3
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    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.
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  4. #4
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    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
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width