Dealing with IO Exceptions when more than one website writes to a common log file

 

I have dealt with few such cases where customers are trying to write to a single log file from more than one websites. Now this can lead to file IO exceptions to be generated at runtime if by any chance, that file is being used by a process and at the same time some other process also tries to write to that file. Now here, we need to plug-in the proper exceptional handling code to prevent the application from crashes and prevents us to see error messages such as

System.IO.IOException: The process cannot access the file "F:\web.log" because it is being used by another process.

   at System.IO.__Error.WinIOError(Int32 errorCode, String str)

   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)

   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)

   at System.IO.StreamWriter.CreateFile(String path, Boolean append)

   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)

   at System.IO.StreamWriter..ctor(String path, Boolean append)

We can easily view the access denied to the process, thread by analyzing the Filemon logs. It’s an excellent tool to troubleshoot any kind of permission issues on the file system. You can download it from www.sysinternals.com and search for Filemon.

 “Since sharing violations can occur when two or more websites, threads or processes access the same file we need to change the code a bit by adding a loop around lines of code when StreamWriter is opened. If it generates IOException this loop will wait for some time and then again try to write to a file.

If some other exception is thrown it will re-throw the exception to the parent code. It means that if loop exits without exceptions the log file is opened successfully.”

Const ERROR_SHARING_VIOLATION As ULong = 32

'True means we can't open the file because it's in use

Dim fileAlreadyInUse As Boolean = False

   Do

      Try

         System.Diagnostics.Trace.WriteLine("Opening File")

         objWriter = New StreamWriter(strFileName, True)

         fileAlreadyInUse = False ' We opened the file.

      Catch ex As IOException

         If Marshal.GetLastWin32Error() = ERROR_SHARING_VIOLATION Then

            fileAlreadyInUse = True

' Wait a little bit before trying again

Thread.Sleep(500)

     Else

     

'Some other error occurred while trying to open the file

            throw new Exception(“Error occurred in opening the file”)

         End If

      End Try

   Loop While fileAlreadyInUse