views:

900

answers:

3

One of our applications has a mechanism for emailing our Helpdesk automatically should it encounter a certain level of exception. One particular exception, a NullReferenceException, is causing a few problem and I believe it is caused by IIS recycling and losing the session. To prove this I want to log when the application is started/stoped/recycled and have added some code to the global.asax file to do this. Running on Debug mode the log messages are written out and all seems well. The problem comes when I switch to Release build, which triggers a Web Deployment Project to build to a folder configured in IIS.

When I navigate to the application the ApplicationStart method is not being called as the log file is not created and the Event Log is not updated. When I restart IIS the same happens for ApplicationEnd, i.e. not logs are created.

Why is it working for Debug builds but not Release ones? I've been kicking this around for hours now and it's driving me nuts.

Thanks in advance

**[Edit]: I'm not 100% sure what is going on but it now appears to be working. I thought that maybe the assembly we use for logging might not be loaded by the time ApplicationStart is executed so I removed all the code and just created a text file in the method instead. Behold, the file was created! So I added an if(logger != null) type checking to try to output some diagnostics but nothing was written out. So I tried a try/catch and wrote a blank file "exception.txt" if an exception was thrown but still no file was created - although the event log and expected log file were generated! I'm confused! **

+1  A: 

I would make sure that the logging logic is not running into some sort of a permission issue while logging null reference exceptions. Do you have the approprite permissions to access where the log is being written?

Mehmet Aras
@Mehmet: I thought that just as I posted and have configured a folder with Everyone/Full Access and pointed the logger there but still no luck. Nothing appearing in the Event Log either though.
DilbertDave
+1  A: 

If you are publishing the app, then do check for PrecompiledApp.config file. If you remove that file from the published site folder, the events would stop firing.

Kirtan
@Kirtan: The PrecompiledApp.config file is present and in the root of the publish folder.
DilbertDave
@Kirtan: After reading another Blog post I saw that one guy was having problems because the PrecompiledApp.config file WAS there and shouldn't be - unfortunately for me, removing it did not help my situation :-(
DilbertDave
+3  A: 

We had this problem and a missing PrecompiledApp.config file was the solution. Without it global.asax events did not fire under IIS6 using an ISAPI filter and the rewritten.aspx approach documented e.g. on blog.codeville.net. We use msbuild to precompile the site before deploying.

We scratched our heads over this one for a couple of hours when our builds stopped working. Turns out while in obsessive house-keeping mode I had removed this file from source control as I thought it was redundant. Adding it back in fixed the problem.

Woodpigeon