I’m developing a web application which takes advantage of Microsoft Enterprise Library’s Logging block to log Errors in a custom sink I’ve developed. Now in order to log unhandled exceptions I’ve hooked on the Application_Error event of the HttpApplication object, the code looks something like that:
void application_Error(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
Exception error = app.Server.GetLastError();
if (error != null)
{
LogEntry errorEntry = new LogEntry();
errorEntry.Title = error.Message;
errorEntry.Categories.Add("General");
errorEntry.EventId = (int)(LoggingErrorCodes.Unhandled_Error);
errorEntry.Severity = System.Diagnostics.TraceEventType.Error;
errorEntry.ExtendedProperties.Add("CallStack", error.StackTrace);
Logger.Write(errorEntry);
app.Server.ClearError();
}
}
So far so good, when I run the application though, I was getting an exception for almost every request made, which resulted of course in a log entry beeing written on the log. The exception had the intuitive message “File does not exist.” , the exception type was HttpException and the call stack indicated that the exception was originating from the ProcessRequestInternal method of HttpStaticHandler class. Finally the page was loading up fine (no obvious errors).
Ok here is the trivia question (don’t read bellow before answering that ;-)) can you imagine why this was happening?
After spending a couple of hours on that and doing a little disassembling on the HttpStaticHandler class (sorry MS had no other way) I’ve discovered that a file was actually missing and that’s why I was getting the exception,
internal static void ProcessRequestInternal(HttpContext context)
{
HttpRequest request1 = context.Request;
HttpResponse response1 = context.Response;
string text1 = request1.PhysicalPath;
if (!HostingEnvironment.UsingMapPathBasedVirtualPathProvider)
{
StaticFileHandler.RespondUsingVirtualFile(request1.FilePath, response1);
}
else
{
FileInfo info1;
if (!FileUtil.FileExists(text1))
{
throw new HttpException(0x194, SR.GetString("File_does_not_exist"));
}
But which file? Once more the page I was requesting was loading up fine. Can you, after these clues, imagine what was going on?
If you still don’t know, here is the solution to the trivia. I’ve put a watch on the ((HttpApplication)sender).context.Request object and set a breakpoint in the application error event handler. From the Request object I found out that the blank.gif (from the name you can see why I was seeing no difference on the page) was missing from a javascript component that was trying to load it at runtime.
If only the guy who wrote the ProcessRequestInternal method of the HttpStaticHandler class had added the text1 local variable in the exception information it would have saved me a lot of time and efford from trying to find out the error. So the lesson learned here is “Always include helpful information to your exceptions” it will make the life of other developers a lot easier…