欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

记录asp.net网站是什么原因导致停止运行的代码

程序员文章站 2024-02-23 16:01:22
记录网站是什么原因导致停止运行还是有必要的,下面是具体的实现方式。 复制代码 代码如下: protected void application_end(object sen...
记录网站是什么原因导致停止运行还是有必要的,下面是具体的实现方式。
复制代码 代码如下:

protected void application_end(object sender, eventargs e)
{
recordendreason();
}

/// <summary>
/// 记录网站停止运行原因
/// </summary>
protected void recordendreason()
{
httpruntime runtime = (httpruntime)typeof(system.web.httpruntime).invokemember("_theruntime", system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.static | system.reflection.bindingflags.getfield,
null,
null,
null);
if (runtime == null)
return;
string shutdownmessage = (string)runtime.gettype().invokemember("_shutdownmessage",
system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance | system.reflection.bindingflags.getfield,
null,
runtime,
null);
string shutdownstack = (string)runtime.gettype().invokemember(
"_shutdownstack",
system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.instance | system.reflection.bindingflags.getfield,
null,
runtime,
null);
string reasonstring="网站application_end,停止运行,shutdownmessage=" + shutdownmessage + ",shutdownstack=" + shutdownstack;
loghelper.writeerrorlog(reasonstring,null);
loghelper.writesmtp(reasonstring, null);
//以下方法将重启的原因和重启时的堆栈信息记录到了windows的事件查看器中,当然你也可以记录到文本文件中。
//eventlog log = new eventlog();
//log.source = "asp.net 2.0.50727.0";
//log.writeentry(string.format("\r\n\r\n_shutdownmessage={0}\r\n\r\n_shutdownstack={1}", shutdownmessage, shutdownstack), eventlogentrytype.information);
}

效果截图
记录asp.net网站是什么原因导致停止运行的代码