I am struggling to capture errors that are thrown by IIS itself. I would like to log an IIS error as well as the MVC errors.
I am using the latest version of ASP.NET MVC 5 and running the website in IIS 7.5 and debugging it locally. I am debugging though Visual Studio 2013.
I am trying to make use of the httpErrors tag in the web.config. I have starting to test 404 errros, but the same applies to all errors, 500, 403, etc. I just needed a starting point and decided to start with 404 errors.
I have the following configuration in my web.config:
<system.web>
<customErrors mode="Off" />
<system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="Http404.html" responseMode="File" />
</httpErrors>
</system.webServer>
I tried the following to URLS:
www.mywebsite.com/foo/bar- Error is caught and my breakpoint is hit in the Application_Error methodwww.mywebsite.com/test.html- This file doesn't exist. It is not caught in my Application_Error method so I can't log it. The correct error page is displayed, same as above
This is what I have in my Application_Error method:
protected void Application_Error(object sender, EventArgs e)
{
// I have minimal code here
// First want to see if I can capture IIS errors here
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
}
How do I log an IIS error if it is not caught in the Application_Error method?
I have also tried this in an action method, it is also not caught in my Application_Error method, but correct error page is displayed:
public ActionResult Index()
{
return HttpNotFound();
}