Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
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);
 
try
{
  // Take Hold of File Lock
  _ExceptionFileLock.WaitOne();
 
  ......
  Open the file strea, write, and close....
  ......
}
catch (Exception objException)
{
  // Other Exception
  throw objException;
}
finally
{
  //Release File Lock
  _ExceptionFileLock.Release();
}
 
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
Pawan
Posted 2-Dec-12 20:15pm
Rai Pawan2.4K

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
Rai Pawan at 3-Dec-12 4:31am
   
Thanks chris
 
your suggestion is appreciated. On mutex vs. semaphores here a good link to read http://www.geeksforgeeks.org/archives/9102. 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.
Pawan
Christiaan Rakowski at 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
0 OriginalGriff 448
1 Sergey Alexandrovich Kryukov 290
2 PhilLenoir 95
3 Uma Shankar Patel 80
4 CB Sharma 79
0 Sergey Alexandrovich Kryukov 6,532
1 OriginalGriff 5,976
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,550


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
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