01/11/2011

How to create a log file ? VB.Net Code Visual Basic software program for beginners


       A log file in programming is very important especially during trial/testing period of a software development  because the log file in any programming language or Visual Basic  can be used to catch the error which were generated while using the software and get it solved while debugging. For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer.So a log files help a programmer  lots as it gives details about actual error, otherwise you have to depend upon the user who is using the
software and some time, user is not able to explain the error exactly. In other way, a log can also be used to monitor and analyse  the behavior of a software user. 

       Therefore, a log file play very important role in vb.net programming or software development or on any where in software development fields like   JAVA , C#, C++ etc.  Here, I am explaining about, How to crate a log file ? which can monitor the users of a Visual Basic software means who were the users who logged in or logged out with their in and out timings. In visual basic programming, these  informational can be gather at the time the user making logging in and log out the visual basic project or software. 
For that I have written following simple Visual Basic.net code at the click event of OK button.
                    
Textbox -          Name property- TxtUserId.Text
Try

            Dim s As StreamWriter
            Dim portfolioPath As String = My.Application.Info.DirectoryPath
            If Not Directory.Exists("C:\PISBackup") Then
                Directory.CreateDirectory("C:\PISBackup")
                File.Create("C:\PISBackup\log.rtf").Close()
                s = New StreamWriter("C:\PISBackup\log.rtf", True)
                s.WriteLine("User Id login " & TxtUserId.Text & "-" & Date.Now)
                s.Flush()
                s.Close()
            Else
                s = New StreamWriter("C:\PISBackup\log.rtf", True)
                s.WriteLine("User Id login in " & TxtUserId.Text & "-" & Date.Now)
                s.Flush()
                s.Close()
            End If
        Catch ex As Exception
             ex.Message
            End Try

    The above visual basic code will be written at click even of OK button to catch login ID and login time of the user.By that way we can generate a log file in visual basic 

    This simple visual basic code can be use to catch the log out time of the user with just minor  modification in visual basic program code can be use at the click even of exit button of the visual basic program. You just change the USER ID LOGIN IN to  USER ID LOGIN OUT or any thing as you wish.

The PISBackup  and log.rtf is directory and log file which will be created in this case, if you want to give another name and root directory you can modify as per your wish. Similar loging can be applied to catch the error message and create a separate log file.This is my way of creating log file in Visual basic.

If you like to understand Visual Basic Computer programming in better way, it is suggested to have a book written by Anne Prince " Murach Visual Basic"  which is available on  Amazon as well as Flipkart.
 which is very good for understanding visual basic step by step 





wishing Happy coding



No comments:

Post a Comment

What & How