Thursday, August 11, 2011

IIS 7.5, ASP.NET and log4net FileAppender lock error

Logging is a big part of any application.  In a recent MVC3 web application, I was having trouble getting my log4net configuration to log successfully to a file.  At first I had no idea why nothing was logging.  The symptoms I saw were that the destination file was being created, but nothing was getting logged to it.

Step 1:  Turn on log4net internal debugging.
Visit the Log4net FAQ and look under the troubleshooting section.  There you will find a faq on turning on the log4net internal debugging feature.  Basically it uses the windows trace system and then logs those trace alerts to a file.

Step2: Inspect the debugging
After taking a look at the debugging output, I noticed the following error.

log4net:ERROR [RollingFileAppender] Unable to acquire lock on file xxxxx. Access to the path 'xxxxx' is denied.

At least now I had a place to look.

Solution
There are two things that I had to do to make this solution work.
1)  In a web environment, there could be multiple threads trying to write to the logging file at once (depending on how you have things setup).  By default, log4net tries to acquire an exclusive lock to the files it is trying to write to.  You can override this default behaviour by telling log4net to use minimalistic locking.  You can find out more information in the faq by looking for "How do I get multiple process to log to the same file".

In any event, the configuration you want to add to your fileappender is

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

2) Make sure the application pool you are trying to use has file permission access to write to that file.  In IIS 7.5, you can setup your application pools to use application pool identities, instead of networkservice or another named account.  You can find more information about this here.  Basically, an application pool identity is kind of a "virtual account".  In order to give access to the file, you have to give the user "IIS AppPool\<AppPoolName>" permission to write to that file.

Happy logging!

9 comments:

  1. Keep it up!! You have done the Home Security Companies nice job having provided the latest information.

    ReplyDelete
  2. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. locksmith Leeds

    ReplyDelete
  3. This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… singapore tsa lock supplier

    ReplyDelete
  4. This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… singapore tsa lock supplier

    ReplyDelete
  5. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... Brooklyn, NY

    ReplyDelete
  6. Such an enlightening website. An obligation of appreciation is all together for sharing. Furniture is a thing that we in general need in our home or workplace. In case you thinking about obtaining new goods, by then I propose you buy victorian decorations. Since it's altogether pleasing and the arrangement is so impeccable. front door locks

    ReplyDelete
  7. Extraordinary blog. I took pleasure in scrutinizing your articles. This is extremely a marvelous scrutinized for me. I have bookmarked it and I am suspecting examining new articles. Continue doing magnificent! VAPE STORE

    ReplyDelete
  8. Ordinarily these unlicensed locksmiths who are regularly additionally unpracticed, amateurish and just call themselves "locksmiths" are essentially attempting to acquire however much cash as could reasonably be expected. Locked Out Longmont CO

    ReplyDelete
  9. Proficient business locksmith administrations exist absolutely to evaluate the necessities of a wide range of organizations and address the issues of each with a modified arrangement. locksmith near me

    ReplyDelete