Capturing ASP.NET Application Startup Exceptions

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

时间: 2024-10-14 05:21:54

Capturing ASP.NET Application Startup Exceptions的相关文章

ASP.NET 5 入门——Application Startup

ASP.NET 5 入门--Application Startup? Startup 类? 在 ASP.NET 5 中,Startup 类是一个应用的入口点,我们可以为不同环境配置不同的内容. 编译器会查找项目文件夹下的所有 *.cs 文件进行编译,而运行时会寻找所有命名空间下类名为 Startup 的类作为启动方式. 注解 可以通过设置 project.json 文件选择需要(或不需要)编译的文件和文件夹:也可以设置在不同的程序集中搜索 Startup 类. Startup 类必须定义一个 C

Examining Application Startup in ASP.NET 5

By Steve Smith  June 23, 2015 ASP.NET 5 differs from previous versions of ASP.NET in many ways. Gone is the default ASP.NET event life cycle, and along with it, the global.asax file (which itself was an evolved version of global.asa from the ASP days

ASP.NET Application Life Cycle

The table in this topic details the steps performed while an XAF ASP.NET application is running. Note Currently, only the steps that are performed until the main page is shown are detailed. Stage Description Ways to Interfere Application is requested

Debug your ASP.NET Application while Hosted on IIS

转摘:http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS This article describes how to debug a web application which is hosted on IIS. It also describes how to select the correct process to attach to when multip

[转]ASP.net Application 生命周期事件

生命周期事件和 Global.asax 文件 在应用程序的生命周期期间,应用程序会引发可处理的事件并调用可重写的特定方法.若要处理应用程序事件或方法,可以在应用程序根目录中创建一个名为 Global.asax 的文件. 如果创建了 Global.asax 文件,ASP.NET 会将其编译为从 HttpApplication 类派生的类,然后使用该派生类表示应用程序. HttpApplication 进程的一个实例每次只处理一个请求.由于在访问应用程序类中的非静态成员时不需要将其锁定,这样可以简化

ASP.NET Application,Session,Cookie和ViewState等对象用法和区别 (转)

在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 整个应用程序/所有用户 服务器端 Cache 任意大小 可以根据需要设定 整个应用程序/所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cook

asp.net application

Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对应的.Application 对象握有会被应用程序中的许多页面使用的信息(比如数据库连接信息).这意味着可以从任何的页面访问这些信息.同时也意味着你可在一个地点改变这些信息,然后这些改变会自动反映在所有的页面上. application的创建和获取和session一样: application["k

php仿照asp实现application缓存的代码分享

php 怎么没有asp 那样的application缓存呢?最近我找了很多,都只有自己写,下面我分享一段代码 class php_cache{ //相对或者绝对目录,末尾不要加 '/' var $cache_dir = './cache'; var $cache_extension = '.cache.php'; function set_cache($name, $value){ $pre = "< ?\n//Cache Created at: ".date('Y-m-d H:

ASP.NET——Application全局对象

Application是应用全局对象,被全体共享.无论通过哪个页面操作Application,另一个页面都可以读取Application信息. 由于Application是共享的,操作之前先Lock,操作完成后UnLock. 在一个页面设置数据: Application.Lock(); Application.Set("address", "上海"); Application.UnLock(); 在另一个页面取数据: string s = (string)Appli