Note that there are some explanatory texts on larger screens.

plurals
  1. PORaised exception in the case of concurrent file access with StreamReader
    primarykey
    data
    text
    <p>I found a <a href="https://stackoverflow.com/questions/1882637/how-to-handle-concurrent-file-access-with-a-filestream-streamwriter">post talking about handling concurrent file access with StreamWriter</a>.</p> <p>The problem is that the answers do not manage the scenario where the file is being accessed but multiple processes.</p> <p>Let's tell it shortly :</p> <ul> <li>I have multiple applications</li> <li>I need a centralised logging system in dataBase</li> <li>If database fail, I need a fallback on a file system log</li> </ul> <p>There is a known concurrency scenario, where multiple applications (processes) will try to write in that file. This can be managed by re-attempt the writing after a short delay. But I don't want ot reattempt if it's a security error or filename syntax error.</p> <p>The code is here :</p> <pre><code>// true if an access error occured bool accessError = false; // number fo writing attemps int attempts = 0; do { try { // open the file using (StreamWriter file = new StreamWriter(filename, true)) { // write the line file.WriteLine(log); // success result = true; } } /////////////// access errors /////////////// catch (ArgumentException) { accessError = true; } catch (DirectoryNotFoundException) { accessError = true; } catch (PathTooLongException) { accessError = true; } catch (SecurityException) { accessError = true; } /////////////// concurrent writing errors /////////////// catch (Exception) { // WHAT EXCEPTION SHOULD I CATCH HERE ? // sleep before retrying Thread.Sleep(ConcurrentWriteDelay); } finally { attempts++; } // while the number of attemps has not been reached } while ((attempts &lt; ConcurrentWriteAttempts) // while we have no access error &amp;&amp; !accessError // while the log is not written &amp;&amp; !result); </code></pre> <p>My only question is <strong>the type of exception</strong> that will be raised in the case of concurrency writting. I already know things can be done differently. Let me add a few considerations :</p> <ul> <li>No, I don't want to use NLog in that scenario</li> <li>Yes I handle concurrency with IOC + Mutex for the in-process concurrency</li> <li>Yes I really want all log to be written in the same file</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload