[RESOLVED] Create a function that will log a file and also read and dispay the log file
I'm creating an application where the user inputs the radius and it outputs the area. The results will be recorded to a log file. I'm trying to create a function that will read the log file and return in inside a rich text box. All the events should include a date and time stamp, along with a message (I will make up the message). I also need to add a log event when the application starts, when the user calculates the area, when the user displays the log in the richtextbox and when the user exits. I have already created the application, I'm just having trouble doing the function for the log requirements above.
Re: Create a function that will log a file and also read and dispay the log file
What part of this are you struggling with? Do you know how to:
Open a file for writing in a way that will create it if it doesn't already exist?
Write/Append data to a file?
Close a file?
Open a file for reading?
Read data from a file?
Populate a Rich Text box with a string?
Get the current date and time?
Use concatenation to put together various pieces of data into a single string?
I would strongly suggest not trying to do everything related to logging in one single function, at the very least the reading of the log file and writing entries to the log file should be separate functions.
Re: Create a function that will log a file and also read and dispay the log file
Here is my code, I ended up figuring it out but I keep getting an error. I have posted a pic of the error at the bottom. Sorry about the clarity, I don't know why it's showing up like that but you should still be able to read the error.
Code:
Public Class Form1
Dim radius As Double
Dim area As Decimal
Dim file As System.IO.StreamWriter
Private Sub Form1_load(sender As Object, e As EventArgs) Handles MyBase.Load
lblArea.Enabled = False
End Sub
Private Sub btnFindArea_Click(sender As Object, e As EventArgs) Handles btnFindArea.Click
If CBool(txtRadius.Text = "") Then
MsgBox("Please Enter Radius")
Else
radius = CDbl(txtRadius.Text)
area = CDec(3.14 * radius * radius)
lblArea.Text = CType(area, String)
addrecordtofile(lblArea.Text)
End If
End Sub
Private Sub btnClearLog_Click(sender As Object, e As EventArgs) Handles btnClearLog.Click
deleteLogFile()
End Sub
Private Sub btnDisplayLog_Click(sender As Object, e As EventArgs) Handles btnDisplayLog.Click
displayFileData() 'display the file content
End Sub
Public Sub displayFileData()
Try
For Each s As String In System.IO.File.ReadAllLines("C:\LogFileTest.txt")
RichTextBox1.AppendText(s + vbNewLine) 'write line by line
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub deleteLogFile()
System.IO.File.WriteAllText("C:\LogFileTest.txt", "") 'clear the text file
RichTextBox1.Text = "" 'after show the textbox is clear
'My.Computer.FileSystem.DeleteFile("D:\LogFileTest.txt") 'This will delete log file
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
WriteDashLine()
Me.Close()
Application.Exit()
End Sub
End Class
Code inside module:
Code:
Module KP
Public Sub addRecordToFile(text As String)
Dim d As String = System.DateTime.Now.ToString("yyyy/mm/dd HH:mm:ss")
Dim FILE_NAME As String = "C:\LogFileTest.txt" 'Create a file on the given location
If System.IO.File.Exists(FILE_NAME) = False Then 'check if file exist not then create new one
'MsgBox("FILE CREATED") ' Message box show the file
System.IO.File.Create(FILE_NAME).Dispose() 'create a file
End If
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) 'create object file writer with filename
objWriter.WriteLine(text & " " & d) 'write the allValue to the file in line
'MsgBox("VALUE INSERTED TO THE FILE") ' Message box show the file
objWriter.Close()
End Sub
Public Sub WriteDashLine()
Dim FILE_NAME As String = "C:\LogFileTest.txt" 'Create a file on the given location
If System.IO.File.Exists(FILE_NAME) = False Then 'check if file exist not then create new one
'MsgBox("FILE CREATED") ' Message box show the file
System.IO.File.Create(FILE_NAME).Dispose()
End If
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) 'create object file writer with filename
objWriter.WriteLine("----------------------------------") 'write the dash to file in line
'MsgBox("VALUE INSERTED TO THE FILE") ' Message box show the file
objWriter.Close()
End Sub
End Module
Re: Create a function that will log a file and also read and dispay the log file
That is hard to read - you should paste the CODE between code tags (#) button, and then paste a screen capture of just the error dialog box.
However, from what I can see, you are attempting to write to C:\LogFileTest.txt, and, depending on the Windows version, you probably do not have permission to write files to the root of the C: drive. Try making a C:\LogFiles folder and then write to C:\LogFiles\LogFileTest.txt