Welcom ,I hope yo will enjoy here!

导致Asp.Net站点重启的10个原因(转载)

http://www.cnblogs.com/yukaizhao/archive/2011/08/12/asp-net-site-restart-reasons.html

Asp.Net站点有时候会莫名其妙的重启,什么原因导致的却不得而知,经过一番折腾后,我总结了导致Asp.Net站点重启的10个原因

1. 回收应用程序池会导致站点重启,记录的原因是:

HostingEnvironment initiated shutdown HostingEnvironment caused shutdown

2. 修改应用程序池回收规则会导致重启,记录的重启原因:

HostingEnvironment initiated shutdown HostingEnvironment caused shutdown

3. 在IIS中修改站点的名字,不会导致重启

4. 修改站点根目录的配置文件web.config,在配置文件注释中添加几个空格会导致重启,记录的重启原因是:

CONFIG change HostingEnvironment initiated shutdown

但是修改子目录的web.config文件不一定会导致马上重启

5. 修改aspx,master文件不一定会导致重启;但是每修改一次都会导致一次重新编译,重新编译次数达到15次之后会导致站点重启,重启原因是:

Recompilation limit of 15 reached HostingEnvironment initiated shutdown

15次后重启这个数字可以在web.config中做配置,修改compilation的numRecompilesBeforeAppRestart属性值即可。

<compilation debug="false" numRecompilesBeforeAppRestart="15">

6. 删除bin目录下的pdb文件,会导致重启,记录的重启原因是:

Change Notification for critical directories.

在bin目录下新建一个空的文件夹,会导致站点重启,重启原因是:

Directory rename change notification for 'D:\projects\TestWebApp\TestWeb'.T estWeb dir change or directory rename

在bin目录下删除空文件夹,会导致站点重启,记录原因是:

Directory rename change notification for 'D:\projects\TestWebApp\TestWeb'.T estWeb dir change or directory rename

7. 修改Global.asax文件会导致站点重启,即使加几个空格也会重启,记录的重启原因是:

Change in GLOBAL.ASAX HostingEnvironment initiated shutdown

8. 对App_Code目录做修改会导致站点重启

在站点根目录下添加一个名字为App_Code的文件夹,会导致重启,记录的重启原因是:

Change Notification for critical directories. App_Code dir change or directory rename

删除App_Code文件夹会导致站点重启,记录的重启原因是:

File Change Notification Error in D:\projects\TestWebApp\TestWeb\app_code

Change Notification for critical directories.

App_Code dir change or directory rename

在App_Code文件夹下面新建或者删除一个cs文件,会导致站点重启,记录的原因是:

Change Notification for critical directories. App_Code dir change or directory rename

9. 对关键路径的修改都会导致站点重启,关键路径包括:

bin, App_Code, Web References,App_Browsers,App_GlobalResources,App_LocalResources 

10. 另外杀毒软件对文件的扫描,有可能会导致asp.net进程误认为文件或者关键路径发生了变化,也会导致重启。

以上是我总结的导致Asp.net站点重启的10个原因。 总的来说有三个方面会导致站点重启:一方面是IIS配置发生变化;另一方面是asp.net相关文件配置文件,global文件,aspx,ascx,master等类型的文件发生变法;第三个方面是关键路径bin,app_Code, Web References,App_Browsers,App_GlobalResources,App_LocalResources发生变化。

如何记录Asp.Net站点重启的原因呢?

如何记录asp.net站点重启的原因?

在站点执行Application_End事件中添加记录方法。在Global.asax.cs文件中添加如下代码即可:

01 void Application_End(object sender, EventArgs e)
02 {
03     //  Code that runs on application shutdown
04     RecordEndReason();
05 }
06  
07 protected void RecordEndReason()
08 {
09     HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
10         BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField,
11         null,
12         null,
13         null);
14  
15     if (runtime == null)
16         return;
17  
18     string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",
19         BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
20         null,
21         runtime,
22         null);
23  
24     string shutDownStack = (string)runtime.GetType().InvokeMember(
25         "_shutDownStack",
26         BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
27         null,
28         runtime,
29         null);
30  
31     EventLog log = new EventLog();
32  
33     log.Source = "ASP.NET 2.0.50727.0";
34     log.WriteEntry(String.Format("\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}", shutDownMessage, shutDownStack), EventLogEntryType.Information);
35 }

以上方法将重启的原因和重启时的堆栈信息记录到了windows的事件查看器中,当然你也可以记录到文本文件中。

posted on 2011-08-12 09:50  北国的雨  阅读(198)  评论(0编辑  收藏  举报

导航