dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Create a function that will log a file and also read and dispay the log file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    12

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

  2. #2
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,139

    Re: Create a function that will log a file and also read and dispay the log file

    What kind of logging? Text file, database, etc. Post what you have for the function so far.
    Please remember next time...elections matter!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Create a function that will log a file and also read and dispay the log file

    I'm currently not at home on my PC where my code is at but it is a textfile.

  4. #4
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    400

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    12

    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




    Pic of the error
    Attachment 157063
    Attached Images Attached Images  
    Last edited by BCVB; Mar 6th, 2018 at 07:23 PM.

  6. #6
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,176

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Create a function that will log a file and also read and dispay the log file

    Sorry about that, it's my first time posting code but I went back and fixed it. And alright, I will try that and get back on the outcome.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    12

    Re: Create a function that will log a file and also read and dispay the log file

    Thank You, it fixed the error!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width