views:

22

answers:

2

Hi,

In any application, we can do error logging using flat file system.

How do we handle a scenario when there are multiple users having exceptions which are logged in the same flat file?

Many Thanks.

+1  A: 

You'd acquire a write lock to the logfile before logging an exception. If someone else tries to log while you're in the middle of writing, their lock request will wait until you release it.

Alternatively, if such a thing is available, use an atomic file write operation to log the entire exception.

tdammers
So it means we need to use threading to do the same? Also, will it effect the performance?
Priya10
Yes and no. If your application is single-threaded, only one instance ever exists, and no other application writes to the same file, then no extra mechanism is needed. However, most multi-user applications are multi-threaded (or multi-process) at some level; you don't need to be concerned about this, as long as you make sure that no two processes or threads can write to the file at the same time.
tdammers
A: 

You could create one thread for writing exceptions, reading from a queue. The actual exception handling code would write the exception to the queue. Since there's only one thread writing the file, everything is serialized. Of course, you need a thread-safe queue implementation, but your language or framework probably provides one.

Ned Batchelder