IIS: How do I log non-ASPX 404s?
I have a lot of broken links that are not being logged. Is there a way to setup IIS to send them through ASP.NET so that my no开发者_高级运维rmal 404 logging routine catches them?
You could use the host MVC on IIS 6 hack method. Basically, setup a wildcard .* mapping in IIS and have the aspnet_isapi.dll handle this request, which is the same as having .NET handle all request. You may experience a performance decrease (probably minimal), but it should hit your logging routine.
IIS 6 Hack
If you are using IIS7 you can use the httpErrors configuration element. What this does is (after the request has been handled) it checks the response code that has been pushed to the output, and allows you to intercept calls, such as:
<httpErrors existingResponse="PassThrough">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/PageNotFound.aspx" />
</httpErrors>
It's a lot like ASP.NET's customErrors mechanism, with the exception of being processed later in the pipeline. In the above example, I'm telling it to execute the URL /PageNotFound.aspx when it encounters a 404. You have to be careful though, because if you send a 404 from the PageNotFound.aspx page (typically you would!), it can get caught up in a cyclic redirect. To get round this, we add the following attribute:
existingResponse="PassThrough"
What this does is to determine if the current response already has a body, and if so, pass it along (don't process the status code).
I posted this on my blog a while ago: http://www.fidelitydesign.net/?p=21
Hope that helps :)
精彩评论