Jump to content

A very useful Logfile

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
SimonCoder

SimonCoder

    Newbie

  • Members
  • Pip
  • 8 posts
When I write code for my applications, I find it very useful sometimes to track the activity of certain things. I like this type of logfile for a couple of reasons:
  • It places the last activity on top of the logfile
  • It can be customized easily
  • it can be used in most any part of your code

Here is the code that I use quite often to write to a log file.


                Dim myfile As String = My.Settings.logloc

                Dim itxt As New TextBox

                If IO.File.Exists(myfile) Then

                    itxt.Text = IO.File.ReadAllText(myfile)

                End If

                Dim vt As String = vbCrLf & Date.Now & " - " & My.Application.Info.Version.ToString & "--Report was printed..." & My.User.Name & itxt.Text

                My.Computer.FileSystem.WriteAllText(myfile, vt, False)

                itxt.Clear()


The first part of the code sets a variable for the location of the logfile.
Dim myfile As String = My.Settings.logloc

The next part sets up a "Virtual" textbox to store the current text in the logfile. this is done so that the information that you want to add to the logfile can be placed at the top of the logfile.

                Dim itxt As New TextBox

                If IO.File.Exists(myfile) Then

                    itxt.Text = IO.File.ReadAllText(myfile)

                End If


next we set a variable that will determine what information we want to add to the logfile.
                Dim vt As String = vbCrLf & Date.Now & " - " & My.Application.Info.Version.ToString & "--Report was printed..." & My.User.Name & itxt.Text


vbCrLf will create a new line in the logfile, "Date.Now" is the current computers system date and time, My.Application.Info.Version.ToString, the version of the application, "--Report was printed" text that represents what action we are logging, My.User.Name, the name of the current user and then itxt.text, the rest of the logfile we set as a variable to place back into the log.

finally we write to the logfile and then clear the variable.

#2
Sharper_Software

Sharper_Software

    Newbie

  • Members
  • PipPip
  • 17 posts
Great Tutorial, +Rep

#3
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
Nice work mate, Looks decent :)