File system level file write synchronization

I have a text file and several threads / processes will be written to it (this is a log file).

A file is sometimes damaged due to simultaneous emails.

I want to use the write mode of files from all streams, which is sequential at the file system level.

I know that you can use locks (mutex for several processes) and synchronize writing to this file, but I prefer to open the file in the correct mode and leave the System.IO task.

Is it possible? What is the best practice for this scenario?

+4
source share
3 answers

It is best to use locks / mutexex. This is a simple approach, it works, and you can easily understand it and reason about it.

When it comes to synchronization, you often have to start with the simplest solution that can work, and only try to clarify if you run into problems.

+3
source

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).

+2
source

you could use File.Open() with FileShare to None and make each thread FileShare if it cannot access the file.

+1
source

Source: https://habr.com/ru/post/1340366/


All Articles