Avatar of armitdept
armitdept
Flag for United States of America

asked on 

Creating a "log" file triggered once per day at certain time needs to pull from text, run a calculation, then input the value back into same text file.

Ok I am creating a really simple log program as a server monitor to run as a windows service. Basically it is setup to poll the current time every 60 seconds and when the time hits a predetermined value it will run this script. I have this working prett much the way I could deal with it to work but I want it to be more dynamic. The way I have it setup now at 3:12pm everyday this windows service will read from a text file, take the current value, add 1 to this value, then place it back into the text file. This is monitoring "server up time" in days so if the server is up and running this will count up indefinatly.

I am also including a date compare function that if the next time the service polls the file if there is more than 24 hour difference it will raise a red flag "so to speak". When this happens the count will automatically start back at 1 since there was an issue with the server the previous day and alert the Admin of this (allthough he should allready know server was down by this point).

Now what I want to do is create a sort of log file where the file gets appended every day instead of overwriting the previous value (like it currently does). So that after day 1 the code will copy the 1 out of the file, add 1 to it, insert a carriage return (pushing the 1 down a line), then inserting the new value (would be 2 next day and so on and so forth).

So I thought I had this figured out using streamreader/writer but I am running into some issues. So I just need someone to overlook this really simple code and see what I am missing. This is just pasted from my test project since the "live" version works I do not want to break it testing this functionality out. So keep in mind there is some functions I listed above not included since they will come later.

Here is my code below:

Now the main issue I am running into is that the code is not letting itself insert a carriage return while using append method. I do not want to use write method because it will overwrite the file and I do not want to do this. Maybe I am looking at this completely wrong I have only actually been coding less than a year and I have never taken an formal training just have a couple beginers books I reference, google, and here on EE.
Imports Microsoft.VisualBasic.ControlChars
Imports System.IO
Public Class Form1
    Dim LastValue As Double
    Dim NewValue As Double
    Dim test As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.Computer.FileSystem.FileExists("C:\sample.txt") = True Then
            Dim oRead As System.IO.StreamReader
            oRead = File.OpenText("C:\sample.txt")
            oRead.BaseStream.Seek(0, SeekOrigin.Begin)
            'reads first line of file and outputs the value
            LastValue = oRead.ReadLine
            oRead.Close()
            Dim aWrite As System.IO.StreamWriter
            aWrite = File.AppendText("C:\sample.txt")
            'should move cursor to home position
            aWrite.BaseStream.Seek(0, SeekOrigin.Begin)
            'insert carriage return (or new line)
            aWrite.WriteLine()
            'adds 1 to the previously pulled out value
            NewValue = (LastValue + 1)
            'writes on the top most line the new value
            aWrite.WriteLine(NewValue)
            aWrite.Close()
            'this is just a test textbox to test the output so I do not have to open the file each time
            TextBox1.Text = (NewValue)
        Else
            MsgBox("File does not exist. Creating file C:\sample.txt")
            Dim oWrite As System.IO.StreamWriter
            'creates file if does not exist
            oWrite = File.CreateText("C:\sample.txt")
            'enters default value of 1
            oWrite.WriteLine(1)
            oWrite.Close()
        End If
        
    End Sub
End Class

Open in new window

Visual Basic Classic

Avatar of undefined
Last Comment
Jim P.

8/22/2022 - Mon