Click here to Skip to main content
13,258,055 members (48,814 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone

we are writing logs to a .txt file for a web application, using our own class. What the class does is create a semaphore with maximum maximum no. of current entries as 1. we use this semaphore to gain write access to the log file. like this....

private static Semaphore _ExceptionFileLock = new Semaphore(1, 1);

  // Take Hold of File Lock
  Open the file strea, write, and close....
catch (Exception objException)
  // Other Exception
  throw objException;
  //Release File Lock

Now what I'd like to know is once we publish it on IIS and multiple users hit this application, will IIS create a global semaphore for this or for different requests will IIS create different semaphore objects resulting in exception "file is alrady in use"

Any light on semaphore with reference to IIS will be greatly appreciated.

Thanks and regards
Posted 2-Dec-12 21:15pm
Rai Pawan2.6K

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I don't know about Semaphores, but with Mutexes it does (when you create it this way)
What I found worked for me was create a Mutex with a string as key. (Bad practices blabla, but it works)
This way the lock is on the string value, and that will be the same for every user.

Another way to do this is using the Singleton pattern on your logger library, if you are not already doing that.
That way it should create only a single Semaphore object, and thus hold the lock.

Hope this helps.
Rai Pawan 3-Dec-12 4:31am
Thanks chris

your suggestion is appreciated. On mutex vs. semaphores here a good link to read Furthermore as part of some testing that I did it seems that since the same IIS worker process is shared so the semaphore remains global and there are no practical chances to get exception.
Thanks again.
Christiaan Rakowski 5-Dec-12 2:52am
If you have only one process this way will indeed work fine, I was assuming you were using multiple ones, I should have asked. Also, thanks for the link. It was a good read.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100