日期:2014-05-17  浏览次数:20834 次

ASP.NET未处理异常(Unhandled Exception)调试试验

问题介绍

网站运行的时候是否遇到过未处理异常(unhandled exception)造成网站进程退出报503(service unavailable)错误?但是也有时未处理异常仅仅是在页面上显示出错信息,网站进程还在照常运行。

  • 为什么同样发生了异常,结果却截然不同?
  • 这种问题该如何调试?

这片文章通过调试试验来揭开这些问题的答案。


第一种情况

ASP.NET网站运行过程中莫名其妙的退出,在系统事件日志中会有如下记录。

Log Name:      System
Source:        Microsoft-Windows-WAS
Date:          10/6/2012 5:20:03 PM
Event ID:      5011
Task Category: None
Level:         Warning
Keywords:      Classic
User:          N/A
Computer:      system-02
Description:
A process serving application pool 'xxx' suffered a fatal communication error with the Windows Process Activation Service. The process id was '5284'. The data field contains the error number.
应用程序日志中有ASP.NET的错误消息
An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/2/ROOT

Process ID: 3016

Exception: System.Exception

Message: Exception of type 'System.Exception' was thrown.

StackTrace:    at ASP.backgroundexception_aspx.CrashSite(Object arg)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
如果这种错误很短时间内出现次数过多的话,接下来网站就不能访问了,直接返回这样的页面。(这是因为IIS默认提供出错保护,默认情况下在5分钟内出错5次就会自动停掉程序池)


系统日志中也会有这么一条记录。

Log Name:      System
Source:        Microsoft-Windows-WAS
Date:          10/6/2012 6:50:53 PM
Event ID:      5002
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      system-02
Description:
Application pool 'xxx' is being automatically disabled due to a series of failures in the process(es) serving that application pool.


第二种情况

网站同样报出异常,服务器返回错误页面。


应用程序日志中记录为一个警告,当时系统日志中并没有相应的错误。w3wp进程也没有退出。

Log Name:      Application
Source:        ASP.NET 2.0.50727.0
Date:          10/6/2012 5:54:53 PM
Event ID:      1309
Task Category: Web Event
Level:         Warning
Keywords:      Classic
User:          N/A
Computer:      system-02
Description:
Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 10/6/2012 5:54:53 PM 
Event time (UTC): 10/6/2012 9:54:53 AM 
Event ID: 55545893cefc429d895523e8aadc3e01 
Event sequence: 12 
Event occurrence: 3 
Event detail code: 0 
 
Application information: 
    Application domain: /LM/W3SVC/2/ROOT-1-129939906465074620 
    Trust level: Full 
    Application Virtual Path: / 
    Application Path: C:\inetpub\webdebug\ 
    Machine name: DOFWIN8-02 
 
Process information: 
    Process ID: 3016 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\LOCAL SERVICE 
 
Exception information: 
    Exception type: Exception 
    Exception message: Exception of type 'System.Exception' was thrown. 
 
Request information: 
    Request URL: http://localhost:8080/ForegroundException.aspx 
    Request path: /ForegroundException.aspx 
    User host address: ::1 
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: NT AUTHORITY\LOCAL SERVICE 
 
Thread information: 
    Thread ID: 6 
    Thread account name: NT AUTHORITY\LOCAL SERVICE 
    Is impersonating: False 
    Stack trace: