当前位置: 移动技术网 > IT编程>开发语言>.net > 在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

丝诺萄官网,女士交友,袁颖芬

但是,系统日志中可能会记录类似于以下内容的事件消息:
事件类型:警告
事件来源: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 对象的源代码,以便将异常信息记录到应用程序日志中。记录的信息将包含以下内容:
出现异常的虚拟目录路径
异常名称
消息
堆栈跟踪
要修改 ihttpmodule 对象,请按照下列步骤操作。
注意:此代码将会在应用程序日志中记录事件类型为“错误”且事件来源为“asp.net 2.0.50727.0”的消息。要测试模块,可以请求使用 threadpool.queueuserworkitem 方法的 asp.net 页,以调用引发未处理的异常的方法。
将下面的代码放在名为 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 中发生的默认行为。
注意:我们不建议您更改默认行为。如果忽略异常,应用程序可能会泄漏资源并放弃锁定。
要启用这种默认行为,请将下面的代码添加到位于以下文件夹的 aspnet.config 文件中:
复制代码 代码如下:

%windir%\microsoft.net\framework\v2.0.50727
<configuration>
<runtime>
<legacyunhandledexceptionpolicy enabled="true" />
</runtime>
</configuration>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网