Writing to log file - file in use error - best way to avoid

  • Thread starter Thread starter wingers
  • Start date Start date
W

wingers

Guest
Hi

In one of my apps a line is written to a log file when an error occurs

Normally this isn't an issue, but lately noticed problem where if two errors happen in quick succession then app crashes because "file was in use"

Now I know I can just use try..catch to stop it bombing out

But just wondered if any better or cleverer ways to write to a text file, wait for it to finish writing before letting code continue, that way it should avoid issue?

Might be over thinking this, but as ever wanted some of your useful input!! - thanks

So basically I use the lines below:-

Using LogWriter As New StreamWriter(logfilename, True)
LogWriter.WriteLine("string_goes_here")
End Using

Options so far:-

1) simply wrap in try..catch to avoid error

2) use something like below to see if file in use first

Public Function IsFileInUse(sFile As String) As Boolean
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
End Using
Catch Ex As Exception
Return True
End Try
Return False
End Function

3) somehow wait until it is not in use before writing to it?

4) or something much better perhaps???








Darren Rose



1) simply wrap in

Continue reading...
 


Write your reply...
Back
Top