Having an issue with text file writing. Below is my code to write a string to a log file for diagnosing customer issues. Every time I restart the app, the program will start a new file, even though I am opening my file for append. Subsequent calls to this routine will append to the file properly. If I run in debug mode and step through the routine, it will append properly to the pre-existing file with the same name. To summarize, if the user closes the app, then reopens the app, a new log file for that day is created, where I want to have it append to that days log file if there already is one. Again, if I step through this routine the first time it is called in the app, it works properly, but will not append if I let it run at full program speed.
Any Ideas please!
Sub LogEvent(TextToLog As String)
Try
Dim FW1 As TextWriter
Dim FileName As String
Dim Now As Long
Dim Month As Int
Dim Day As Int
Dim Year As Int
Dim LogEntry As String
Now = DateTime.Now
Month = DateTime.GetMonth(Now)
Day = DateTime.GetDayOfMonth (Now)
Year = DateTime.GetYear(Now)
FileName = NumberFormat(Month,2,0) & "-" & NumberFormat(Day,2,0) & "-" & Year & ".log"
FW1.Initialize(File.OpenOutput (LogFilePath, FileName, True))
LogEntry = NumberFormat(DateTime.GetHour(Now),2,0) & ":" & NumberFormat(DateTime.GetMinute(Now),2,0)& ":" & NumberFormat(DateTime.GetSecond (Now),2,0)
LogEntry =LogEntry & " " & TextToLog
FW1.WriteLine(LogEntry)
FW1.Close
Catch
Log("Error in Sub LogEvent")
End Try
End Sub