Capturing ASP.NET Application Startup Exceptions

來源:互聯網
上載者:User

標籤:

It has become common practice to perform tasks during an ASP.NET 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 are 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 you capture application startup exceptions, remember to implement Application_Error.

private void Application_Error(object sender, 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 that lets us know what the exception is, and additionally our users get to see an error page that isn‘t of the red and yellow variety. Note, it may be the default error logging mechanism that we‘ve chosen that is causing our application failure. I recommend having a fallback logging mechanism if possible; Trace is a safe bet.

 

Khalid Abuhakmeh – Software Developer and All Around Nice Guy

 

原文連結:http://www.khalidabuhakmeh.com/capturing-asp-net-application-startup-exceptions

Capturing ASP.NET Application Startup Exceptions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.