As far as I know, Windows does not have what you are looking for. There is no file descriptor object that performs automatic synchronization, blocking all other users when you write the file.
If your logging involves three steps, open the file, write, close the file, and then your threads will try to open the file in exclusive mode ( FileShare.None ), catch an exception if it cannot open, and then try again to success. I found this tiresome at best.
In my programs that register from multiple threads, I created a descendant of TextWriter , which is essentially a queue. Streams call the Write or WriteLine methods on this object, which formats the output and queues it (using a BlockingCollection ). A separate log stream serves the queue - pulls things from it and writes them to the log file. This has several advantages:
- The threads do not need to wait from each other to record
- Only one stream is written to the file.
- It is trivial to rotate logs (i.e. run a new log file every hour, etc.).
- There is no zero error, because I forgot to lock in some thread.
Doing this through processes would be a lot harder. I have not even considered trying to share a log file through processes. If I needed this, I would create a separate application (registration service). This application will make the actual recordings, while other applications transmit the strings to be recorded. Again, this ensures that I cannot mess things up, and my code remains simple (i.e. there is no explicit lock code for clients).
source share