It has become common practice to perform tasks during an ASP. Applications start up process. These tasks may include registering routes, configuring filters, wiring up third party dependencies, and so much more. Here is the default ASP. NET MVC Application startup code pulled from the sample.
protected void Application_Start () { arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles);}
These tasks is crucial to the function of the application, but they don't always execute cleanly. Exceptions in the startup process can mean the application never got a change-to-wire up exception handlers. This can make it difficult-to-debug issues in production, causing us to turn customErrors off.
<customerrors mode="Off" />
To insure your capture application startup exceptions, remember to implement Application_Error .
Private voidApplication_Error (Objectsender, EventArgs e) { //a static Boolean at the application level if(failedtostartup) {//Step 1:write to a dependable logging storage//option 1:trace//option 2:raygun//option 3:filesystem//Step 2:redirect to Non-app based Error page//obviously needs to exist outside your app//since it failed to startup }}
Now we should get a log message This lets us know what the exception are, and additionally our users get to see an error PA GE that isn ' t of the red and yellow variety. Note, it may be the default error logging mechanism that we've chosen that's causing our application failure. I recommend having a fallback logging mechanism if possible; Is Trace a safe bet.
Khalid abuhakmeh– software Developer and all Around nice guy
Original link: http://www.khalidabuhakmeh.com/capturing-asp-net-application-startup-exceptions
Capturing ASP. Application Startup Exceptions