views:

653

answers:

2

I have several process writing to the same log file. How can I synchronize them? My application is a web application written in c#.

+3  A: 

I would either make them to log in different log files, or start using a proxy process which logs the messages into the file for the various logging processes, that way, you only have to send messages to the logging process and isn't that bound to I/O requests that they would be otherwise too.

Or you can use a shared mutex lock on the file.

Tobias Wärre
+1 for a named mutex. No need to muck about with proxy processes.
RichieHindle
A: 

Have a look at named semaphores. I think they should work across processes.

System.Threading.Semaphore sem = new System.Threading.Semaphore(0, 1, "FileWriter");

while (sem.WaitOne())
{
    // Do work
    sem.Release();
}
sindre j