Multiple processes writing to a single log file

This is supposed to be an easy, universal solution, although the problem is currently related to the IIS CGI application, which should record the timeline of events (second resolution) to troubleshoot a situation where a later query ends in the MySQL database BEFORE the previous query!

Thus, it comes down to logging debug statements in a single text file.

I could write a service that manages the queue, as suggested in this thread: The problem of writing a single file to a web service in .NET, but deploying the service on each machine is a pain

or I could use a global mutex, but for this each instance would have to open and close the file for each entry

or I could use a database that would handle this for me, but it doesn't make sense to use a database like MySQL to try to fix a history issue. SQLite is another feature, but this thread

http://www.perlmonks.org/?node_id=672403

Suggests that this is also a bad choice.

I'm really looking for a simple approach, something as rude as writing separate files for each process and consolidating them using a planned application. I do not want to do this, and do not spend a week on this. This is only necessary from time to time.

Suggestions?

+4
source share
5 answers

First, try the simplest solution - each log entry opens and closes the file. If you are having problems with this, which you probably would not want, find another solution.

+4
source

You can use file locking. Lock the file for writing, write a message, unlock.

+3
source

My suggestion is to maintain performance and then think about asynchronous logging. Why not send the data of your data log using UDP to serve the listening port, and it writes to the log file.

+1
source

I would also suggest some kind of central registrar that can be called by every process in an asynchronous way. If the communication is UDP or RPC or something else will be an implementation detail.

+1
source

I even thought it was an old post, does anyone have an idea why not use the following concept:

  • Create / open a file with FILE_SHARE_WRITE sharing mode.
  • The presence of the name of the global mutex and its discovery.
  • Whenever you want to write a file, first lock the mutexes, then write to the file.

Any input?

0
source

All Articles