- 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.


Sign In
Create Account

Back to top









