I'm having a problem writing to a log file using vb.NET. The write only occurs in one public function in a Module. I've re-written my file write logic several different ways but still get the random sharing violations. I've also switching logging from the network drive to the local tmp folder but still get the error.
Method #1
File.AppendAllText(LogPath
, TexttoWrite)
Method #2
Using sw As New StreamWriter(LogPath, True)
sw.Write(TexttoWrite)
End Using
Method #3
Using fs As New FileStream(LogPath, FileMode.OpenOrCreate, FileAccess.Write)
Using sw As New StreamWriter(fs)
sw.Write(TexttoWrite)
End Using
End Using
I ran FileMon to see what is going on and I see the problem being two OPEN requests in a row:
4:19:35 PM prowin32.exe:2432 OPEN C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Options: OpenIf Sequential Access: 00120196
4:19:35 PM prowin32.exe:2432 OPEN C:\tmp\DCMstn18\SENT03_18.
log SHARING VIOLATION Options: OpenIf Sequential Access: 00120196
But it also works fine 90% of the time:
4:19:35 PM prowin32.exe:2432 OPEN C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Options: OpenIf Sequential Access: 00120196
4:19:35 PM prowin32.exe:2432 QUERY INFORMATION C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Length: 109380
4:19:35 PM prowin32.exe:2432 WRITE C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Offset: 109380 Length: 138
4:19:35 PM prowin32.exe:2432 CLOSE C:\tmp\DCMstn18\SENT03_18.
log SUCCESS
4:19:35 PM prowin32.exe:2432 OPEN C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Options: OpenIf Sequential Access: 00120196
4:19:35 PM prowin32.exe:2432 QUERY INFORMATION C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Length: 109518
4:19:35 PM prowin32.exe:2432 WRITE C:\tmp\DCMstn18\SENT03_18.
log SUCCESS Offset: 109518 Length: 138
4:19:35 PM prowin32.exe:2432 CLOSE C:\tmp\DCMstn18\SENT03_18.
log SUCCESS
Any tips would be appreciated, this one really has me stumped. Thanks.
Start Free Trial