Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logging issue

Status
Not open for further replies.

BJZeak

Programmer
May 3, 2008
230
CA
Been running a simple one Form VB EXP 2k8 app for several years which monitors a Serial Port data stream, updates some indicators on the Main Form, and captures specific key entries to forward back to the serial port ... I recently added some Roll over Balloons, a Help Form, and some Data Logging. At issue; the app now runs for around 8 hours then crashes with system level exception error (no error code) ... if I remove the code with a remark ' the app is stable again.

Data logging is done by calling me.DebugPrint instead of debug.print where DebugPrint checks a my.settings.LogFlag to determine if a logging is enabled ... log files are also only 1 day in length at 00:05 the current log file overwrites the previous days log file and a fresh log file is started

1) is there any way to determine what is actually failing

2) perhaps there is a better way to do the logging

Private Sub DebugPrint(ByRef stDebug As String)
Do While gloLock
Application.DoEvents()
Loop
If My.Settings.LogFlag And My.Settings.LogFile & "" <> "" Then
My.Computer.FileSystem.WriteAllText(My.Settings.LogFile, stDebug & vbCrLf, True)
End If
End Sub

Private Sub mvLogFile()
Dim src As String
Dim dst As String
src = My.Settings.LogFile
dst = src & ".bak"

Me.DebugPrint("Move: " & src & " - " & dst)

gloLock = True

If System.IO.File.Exists(dst) = True Then
System.IO.File.Delete(dst)
End If

If System.IO.File.Exists(src) = True Then
System.IO.File.Move(src, dst)
End If

gloLock = False
End Sub
 
My guess is that gloLock is getting set to True, then there's an (unhandled) error either deleting or moving one of the files so gloLock never gets reset back to False, which produces an endless loop in DebugPrint.

Throw in some Try...Catch code and write any error messages to a separate log file.

Also, I think you should look into using System.IO.StreamWriter to write your log files.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks ... added try catch surrounding logic in both DebugPrint and mvLogFile last night

1) gloLock (global Boolean) is defined in the Form Class header and is only set/cleared once a day at 00:05:00 in the mvLogFile logic ... the intent of the lock is to ensure the DebugPrint can't write a log during a move event

2) with the app running in debug mode, the log file has grown to 300K+ in the last 10 hours

3) the Catch has captured debug.print("dp err: " & stDebug) errors and displayed them on the immediate window showing that
a) there are numerous errors at random times
b) the content of stDebug isn't suspect ... comparison of log file shows similar messages are being logged
c) all "db err:" errors are being preceded with
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll

4) downloaded and installed Visual Studio 2017, the converted app appears to misbehave in the same way

5) this app runs on three platforms XP, W7 and WX all appear to have the same issue with logging

found an example for possibly expanding the exception error ... going to try this to see if there is more information on these errors ... plus will look at using StreamWriter

 
Ok ... so the exception errors appear to be too many events calling debugprint at the same time ... the exception error is stating the log file in use by another user ... meaning one log event hasn't completed when another one is being requested ... surely there has to be a basic way to prevent this in an event driven app?

Perhaps one has to build a FIFO logger where the app routines push messages and a timed logger event pops messages and exports them to file
 
ok just added another global flag surrounding the LOG to FILE logic which may work provided:
- WriteAllText doesn't return control to the event before it closes the log file
- there isn't a lag with the OS disk routines
- not sure how VB does reentrant/multithreaded events where 2 entries on separate threads might attempt to lock gloWL at the same time

do while gloWL
application.doevents
loop
gloWL = True
My.Computer.FileSystem.WriteAllText(My.Settings.LogFile, stVal & vbCrLf, True)
gloWL = False
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top