症状
如果在 Microsoft .NET Framework 2.0 上构建的基于 Microsoft ASP.NET 的应用程序中引发未处理的异常,该应用程序将会意外退出。如果出现这个问题,不会在应用程序日志中记录了解此问题所必需的异常信息。但是,系统日志中可能会记录类似于以下内容的事件消息:事件类型:警告事件来源:W3SVC事件类别:无事件 ID: 1009日期: 9/28/2005时间:3:18:11PM 用户: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.
原因
出现这个问题的原因在于未处理异常的默认策略在 .NET Framework 2.0 中已发生更改。默认情况下,未处理异常的策略是结束工作进程。在 Microsoft .NET Framework 1.1 和 Microsoft .NET Framework 1.0 中,会忽略托管线程上的未处理异常。除非附加调试程序以捕获异常,否则您可能意识不到出错。ASP.NET 在 .NET Framework 2.0 中使用未处理异常的默认策略。引发未处理的异常时,基于 ASP.NET 的应用程序将会意外退出。如果在请求上下文中出现异常,则不会发生上述行为。这类异常仍由
HttpException 对象进行处理和包装。在请求上下文中出现的异常不会导致工作进程结束。但是,请求上下文之外的未处理异常(如计时器线程上或回调函数中的异常)会导致工作进程结束。
解决方案
要解决此问题,请使用下列方法之一:
方法 1
修改
IHttpModule 对象的源代码,以便将异常信息记录到应用程序日志中。记录的信息将包含以下内容:
• | 出现异常的虚拟目录路径 |
• | 异常名称 |
• | 消息 |
• | 堆栈跟踪 |
要修改
IHttpModule 对象,请按照下列步骤操作。
注意:此代码将会在应用程序日志中记录事件类型为“错误”且事件来源为“ASP.NET 2.0.50727.0”的消息。要测试模块,可以请求使用
ThreadPool.QueueUserWorkItem 方法的 ASP.NET 页,以调用引发未处理的异常的方法。
1. | 将下面的代码放在名为 UnhandledExceptionModule.cs 的文件中。 |
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Diagnostics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Globalization;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
namespace WebMonitor ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public class UnhandledExceptionModule: IHttpModule ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static int _unhandledExceptionCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static string _sourceName = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static object _initLock = new object();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static bool _initialized = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void Init(HttpApplication app) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Do this one time for each AppDomain.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!_initialized) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
lock (_initLock) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!_initialized) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!File.Exists(webenginePath)) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception(String.Format(CultureInfo.InvariantCulture,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webenginePath));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!EventLog.SourceExists(_sourceName)) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception(String.Format(CultureInfo.InvariantCulture,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_sourceName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_initialized = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void Dispose() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
void OnUnhandledException(object o, UnhandledExceptionEventArgs e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Let this occur one time for each AppDomain.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringBuilder message = new StringBuilder(" UnhandledException logged by UnhandledExceptionModule.dll: appId=");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (appId != null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.Append(appId);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Exception currentException = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.AppendFormat(" type={0} message={1} stack= {2} ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentException.GetType().FullName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentException.Message,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentException.StackTrace);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EventLog Log = new EventLog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Log.Source = _sourceName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
2. | 将 UnhandledExceptionModule.cs 文件保存到下面的文件夹中:C:/Program Files/Microsoft Visual Studio 8/VC |
3. | 打开 Microsoft Visual Studio 2005 命令提示符窗口。 |
4. | 键入 sn.exe -k key.snk,然后按 Enter。 |
5. | 键入 csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs,然后按 Enter。 |
6. | 键入 gacutil.exe /if UnhandledExceptionModule.dll,然后按 Enter。 |
7. | 键入 ngen install UnhandledExceptionModule.dll,然后按 Enter。 |
8. | 键入 gacutil /l UnhandledExceptionModule,然后按 Enter 以显示 UnhandledExceptionModule 文件的强名称。 |
9. | 9. 将下面的代码添加到基于 ASP.NET 的应用程序的 Web.config 文件中。 <add name="UnhandledExceptionModule"type="WebMonitor.UnhandledExceptionModule, <strong name>" /> |
方法 2将未处理异常策略更改回 .NET Framework 1.1 和 .NET Framework 1.0 中发生的默认行为。
注意:我们不建议您更改默认行为。如果忽略异常,应用程序可能会泄漏资源并放弃锁定。要启用这种默认行为,请将下面的代码添加到位于以下文件夹的 Aspnet.config 文件中:%WINDIR%/Microsoft.NET/Framework/v2.0.50727
<configuration><runtime><legacyUnhandledExceptionPolicy enabled="true" /></runtime></configuration>