Concurrent logging to a disk file

Topics: Logging Application Block
Dec 19, 2013 at 1:59 PM
I'm currently using Microsoft's Enterprise Library 5.0 with an ASP.NET 4.0 application.

I was curious about how the Enterprise Library behaves in regards to logging to a disk file, and wondered if anyone could shed some light on how it works?

On a first attempt at using the logging library, I was creating an instance of the Enterprise Library per user session. As a result of this, when I write to the log file from separate user sessions at exactly the same time, one of the log messages fails to get written to the error log. In this event, the Enterprise Library catches the failure and prepends a GUID to a new log file (because the Enterprise Library gets a lock on the disk file when writing to it).

The code has now evolved, and I'm now creating a single instance of the Enterprise Library container at the application level (in the global.asax file) that is shared by all user sessions. Now, when separate user sessions write to the log at the exact same time, the concurrency issue doesn't appear to exist. I haven't seen the issue any longer where the Enterprise Library logger fails to get the lock on the error log and creates a new log file instead.

So my question is, how does this work? Is there some sort of queuing mechanism?
Dec 27, 2013 at 5:51 AM
As you mention, trace listeners are locked. The behavior (locking and GUID file name) is all handled by the System.Diagnostics.TextWriterTraceListener (which some Enterprise Library trace listeners extend).

As a rule, only create one trace listener per file which should avoid the file locking issue. Internally Enterprise Library synchronizes write access to the trace listener so you don't have to worry about concurrency.

Randy Levy
Enterprise Library support engineer
Support How-to