在 .NET Framework 2.0 中未處理的異常導致基于 ASP.NET 的應用程序意外退出
更新時間:2009年11月20日 11:47:27 作者:
如果在 Microsoft .NET Framework 2.0 上構建的基于 Microsoft ASP.NET 的應用程序中引發(fā)未處理的異常,該應用程序將會意外退出。如果出現(xiàn)這個問題,不會在應用程序日志中記錄了解此問題所必需的異常信息。
但是,系統(tǒng)日志中可能會記錄類似于以下內容的事件消息:
事件類型:警告
事件來源:W3SVC
事件類別:無
事件 ID: 1009
日期: 9/28/2005
時間:3:18:11
PM 用戶:N/A
計算機:IIS-SERVER
描述:
為應用程序池“DefaultAppPool”提供服務的進程意外終止。進程 ID 是“2548”。進程退出代碼是“0xe0434f4d”。
而且,應用程序日志中可能會記錄類似于以下內容的事件消息:
事件類型:錯誤
事件來源:.NET Runtime 2.0 錯誤報告
事件類別:無
事件 ID: 5000
日期: 9/28/2005
時間:3:18:02 PM
用戶:N/A
計算機:IIS-SERVER
描述:
EventType clr20r3, P1 w3wp.exe, P2 6.0.3790.1830, P3 42435be1, P4 app_web_7437ep-9, P5 0.0.0.0, P6 433b1670, P7 9, P8 a, P9 system.exception, P10 NIL.
解決辦法:
方法 1修改 IHttpModule 對象的源代碼,以便將異常信息記錄到應用程序日志中。記錄的信息將包含以下內容:
出現(xiàn)異常的虛擬目錄路徑
異常名稱
消息
堆棧跟蹤
要修改 IHttpModule 對象,請按照下列步驟操作。
注意:此代碼將會在應用程序日志中記錄事件類型為“錯誤”且事件來源為“ASP.NET 2.0.50727.0”的消息。要測試模塊,可以請求使用 ThreadPool.QueueUserWorkItem 方法的 ASP.NET 頁,以調用引發(fā)未處理的異常的方法。
將下面的代碼放在名為 UnhandledExceptionModule.cs 的文件中。
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Web;
namespace WebMonitor {
public class UnhandledExceptionModule: IHttpModule {
static int _unhandledExceptionCount = 0;
static string _sourceName = null;
static object _initLock = new object();
static bool _initialized = false;
public void Init(HttpApplication app) {
// Do this one time for each AppDomain.
if (!_initialized) {
lock (_initLock) {
if (!_initialized) {
string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");
if (!File.Exists(webenginePath)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",
webenginePath));
}
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
_sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);
if (!EventLog.SourceExists(_sourceName)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",
_sourceName));
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
_initialized = true;
}
}
}
}
public void Dispose() {
}
void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
// Let this occur one time for each AppDomain.
if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
return;
StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException logged by UnhandledExceptionModule.dll:\r\n\r\nappId=");
string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
if (appId != null) {
message.Append(appId);
}
Exception currentException = null;
for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {
message.AppendFormat("\r\n\r\ntype={0}\r\n\r\nmessage={1}\r\n\r\nstack=\r\n{2}\r\n\r\n",
currentException.GetType().FullName,
currentException.Message,
currentException.StackTrace);
}
EventLog Log = new EventLog();
Log.Source = _sourceName;
Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
}
}
}
將 UnhandledExceptionModule.cs 文件保存到下面的文件夾中:
C:\Program Files\Microsoft Visual Studio 8\VC
打開 Microsoft Visual Studio 2005 命令提示符窗口。
鍵入 sn.exe -k key.snk,然后按 Enter。
鍵入 csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs,然后按 Enter。
鍵入 gacutil.exe /if UnhandledExceptionModule.dll,然后按 Enter。
鍵入 ngen install UnhandledExceptionModule.dll,然后按 Enter。
鍵入 gacutil /l UnhandledExceptionModule,然后按 Enter 以顯示 UnhandledExceptionModule 文件的強名稱。
9. 將下面的代碼添加到基于 ASP.NET 的應用程序的 Web.config 文件中。
<add name="UnhandledExceptionModule"
type="WebMonitor.UnhandledExceptionModule, <strong name>" />
方法 2將未處理異常策略更改回 .NET Framework 1.1 和 .NET Framework 1.0 中發(fā)生的默認行為。
注意:我們不建議您更改默認行為。如果忽略異常,應用程序可能會泄漏資源并放棄鎖定。
要啟用這種默認行為,請將下面的代碼添加到位于以下文件夾的 Aspnet.config 文件中:
%WINDIR%\Microsoft.NET\Framework\v2.0.50727
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
</configuration>
事件類型:警告
事件來源:W3SVC
事件類別:無
事件 ID: 1009
日期: 9/28/2005
時間:3:18:11
PM 用戶:N/A
計算機:IIS-SERVER
描述:
為應用程序池“DefaultAppPool”提供服務的進程意外終止。進程 ID 是“2548”。進程退出代碼是“0xe0434f4d”。
而且,應用程序日志中可能會記錄類似于以下內容的事件消息:
事件類型:錯誤
事件來源:.NET Runtime 2.0 錯誤報告
事件類別:無
事件 ID: 5000
日期: 9/28/2005
時間:3:18:02 PM
用戶:N/A
計算機:IIS-SERVER
描述:
EventType clr20r3, P1 w3wp.exe, P2 6.0.3790.1830, P3 42435be1, P4 app_web_7437ep-9, P5 0.0.0.0, P6 433b1670, P7 9, P8 a, P9 system.exception, P10 NIL.
解決辦法:
方法 1修改 IHttpModule 對象的源代碼,以便將異常信息記錄到應用程序日志中。記錄的信息將包含以下內容:
出現(xiàn)異常的虛擬目錄路徑
異常名稱
消息
堆棧跟蹤
要修改 IHttpModule 對象,請按照下列步驟操作。
注意:此代碼將會在應用程序日志中記錄事件類型為“錯誤”且事件來源為“ASP.NET 2.0.50727.0”的消息。要測試模塊,可以請求使用 ThreadPool.QueueUserWorkItem 方法的 ASP.NET 頁,以調用引發(fā)未處理的異常的方法。
將下面的代碼放在名為 UnhandledExceptionModule.cs 的文件中。
復制代碼 代碼如下:
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Web;
namespace WebMonitor {
public class UnhandledExceptionModule: IHttpModule {
static int _unhandledExceptionCount = 0;
static string _sourceName = null;
static object _initLock = new object();
static bool _initialized = false;
public void Init(HttpApplication app) {
// Do this one time for each AppDomain.
if (!_initialized) {
lock (_initLock) {
if (!_initialized) {
string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");
if (!File.Exists(webenginePath)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",
webenginePath));
}
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
_sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);
if (!EventLog.SourceExists(_sourceName)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",
_sourceName));
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
_initialized = true;
}
}
}
}
public void Dispose() {
}
void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
// Let this occur one time for each AppDomain.
if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
return;
StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException logged by UnhandledExceptionModule.dll:\r\n\r\nappId=");
string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
if (appId != null) {
message.Append(appId);
}
Exception currentException = null;
for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {
message.AppendFormat("\r\n\r\ntype={0}\r\n\r\nmessage={1}\r\n\r\nstack=\r\n{2}\r\n\r\n",
currentException.GetType().FullName,
currentException.Message,
currentException.StackTrace);
}
EventLog Log = new EventLog();
Log.Source = _sourceName;
Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
}
}
}
將 UnhandledExceptionModule.cs 文件保存到下面的文件夾中:
C:\Program Files\Microsoft Visual Studio 8\VC
打開 Microsoft Visual Studio 2005 命令提示符窗口。
鍵入 sn.exe -k key.snk,然后按 Enter。
鍵入 csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs,然后按 Enter。
鍵入 gacutil.exe /if UnhandledExceptionModule.dll,然后按 Enter。
鍵入 ngen install UnhandledExceptionModule.dll,然后按 Enter。
鍵入 gacutil /l UnhandledExceptionModule,然后按 Enter 以顯示 UnhandledExceptionModule 文件的強名稱。
9. 將下面的代碼添加到基于 ASP.NET 的應用程序的 Web.config 文件中。
<add name="UnhandledExceptionModule"
type="WebMonitor.UnhandledExceptionModule, <strong name>" />
方法 2將未處理異常策略更改回 .NET Framework 1.1 和 .NET Framework 1.0 中發(fā)生的默認行為。
注意:我們不建議您更改默認行為。如果忽略異常,應用程序可能會泄漏資源并放棄鎖定。
要啟用這種默認行為,請將下面的代碼添加到位于以下文件夾的 Aspnet.config 文件中:
復制代碼 代碼如下:
%WINDIR%\Microsoft.NET\Framework\v2.0.50727
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
</configuration>
您可能感興趣的文章:
- asp.net服務器上幾種常見異常的解決方案.
- ASP.NET生成eurl.axd Http異常錯誤的處理方法
- asp.net Http異常eurl.axd出錯信息解決方法
- Asp.net Mvc 身份驗證、異常處理、權限驗證(攔截器)實現(xiàn)代碼
- ASP.NET mvc異常處理的方法示例介紹
- asp.net 錯誤:0x8007000B 異常的解決方法
- asp.net開發(fā)中常見公共捕獲異常方式總結(附源碼)
- ASP.NET MVC異常處理模塊詳解
- 在ASP.NET 2.0中操作數(shù)據(jù)之十八:在ASP.NET頁面中處理BLL/DAL層的異常
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十八:處理BLL和DAL的異常
相關文章
asp.net中Timer無刷新定時器的實現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時器的實現(xiàn)方法,是一個非常具有實用價值的技巧,需要用到Ajax技術,需要的朋友可以參考下2014-08-08詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
這篇文章主要介紹了詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11.NET開發(fā)實現(xiàn)一個微信跳一跳的輔助程序
最近比較火的小游戲就是微信跳一跳了,下面這篇文章主要給大家介紹了關于如何利用.NET開發(fā)實現(xiàn)一個微信跳一跳輔助程序的相關資料,利用此輔助可以輕松的實現(xiàn)高分,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01VS2010/VS2013項目創(chuàng)建 ADO.NET連接mysql/sql server詳細步驟
這篇文章主要介紹了VS2010/VS2013項目創(chuàng)建,及ADO.NET連接mysql/sql server詳細步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10