ASP.NET How to log 404 for non-executed end points (images etc.)

bgx

Posted on
Feb 04 2018

Is there a recommended way to enable logging of 404 errors for not executed file types like missing css, js or image files?

Only for .aspx, .axd files etc. is a HttpException thrown and routed through the Application_Error event. It seems using the Raygun HttpModule also only logs 404 application errors, but not e.g. missing images, css or js files.


j5alive

Posted on
Feb 05 2018

Raygun won't pick these up as IIS will be using the native StaticFileModule to handle requests for these types of files. This means the request does not go through the managed pipeline and offers performance benefits (like caching) compared to serving these files through a managed handler.

If you set <modules runAllManagedModulesForAllRequests="true"> then requests for static files will also go through the managed modules but this will have a performance impact. Read more about this https://docs.microsoft.com/en-us/iis/configuration/system.webserver/modules/


bgx

Posted on
Feb 05 2018

Thanks for the info j5alive.

I managed to get it done by:

  • disabling the logging of 404 errors by default
  • directing all 404 errors to a dynamic custom errors page by configuring system.webServer > httpErrors to execute custom error pages
  • manually log 404 errors from the 404 error page
  • seting system.web > customErrors to Off (otherwise 404 for non-static endpoints gets logged twice)

Seems to work so far.


Reply