Say I have some code that does this:
Public Function AppendToLogFile(ByVal s As String) As Boolean
Dim success As Boolean = True
Dim fs As IO.FileStream = Nothing
Dim sw As IO.StreamWriter = Nothing
Static LogFileLock As New Object()
SyncLock LogFileLock
Try
fs = New IO.FileStream(LogFilePath)
sw = New IO.StreamWriter(fs)
sw.WriteLine(s)
Catch ex As Exception
success = False
Finally
If Not sw Is Nothing Then sw.Close()
If Not fs Is Nothing Then fs.Close()
End Try
End SyncLock
Return success
End Function
First of all: is it a problem that I have that Try/Catch/Finally block inside of a SyncLock?
Second of all: suppose this code runs, on an event, potentially many times within a small timeframe--say, ten times in one second. Is it OK to have it SyncLock like this, or would it make more sense to have it add a line to a Queue, and then write all the lines from the Queue to the file on a timer that goes off, say, every second?