在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意

2019-05-11 19:23:10王振洲

}
}
}
}
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("rnrnUnhandledException logged by UnhandledExceptionModule.dll:rnrnappId=");
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("rnrntype={0}rnrnmessage={1}rnrnstack=rn{2}rnrn",
currentException.GetType().FullName,
currentException.Message,
currentException.StackTrace);
}
EventLog Log = new EventLog();
Log.Source = _sourceName;
Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
}
}
}


将 UnhandledExceptionModule.cs 文件保存到下面的文件夹中:
C:Program FilesMicrosoft Visual Studio 8VC
打开 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 中发生的默认行为。
注意:我们不建议您更改默认行为。如果忽略异常,应用程序可能会泄漏资源并放弃锁定。
要启用这种默认行为,请将下面的代码添加到位于以下文件夹的 Aspnet.config 文件中:

%WINDIR%Microsoft.NETFrameworkv2.0.50727
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
</configuration>