<%@ Application Language="C#" %>

<%@ Import Namespace="System.IO" %>

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Threading" %>


 
<script runat="server">
    





   public  string cidd="3";

   public   System.Timers.Timer myTimer = new System.Timers.Timer(60000);





 public static   string host ="";
 public static   string port ="";


     void Application_Start(object sender, EventArgs e) 
     {
               // 在应用程序启动时运行的代码
      




         myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);

         myTimer.Interval = 120*1000;//2fen


         myTimer.Enabled = true;//就放这里，如果打开后未有Application_Beginrequest，但是在新打开页面后也启动了Application_Start和Application_Beginrequest




       

     wtlog("sws cms server starting ok...");
  













     }
    
     void Application_End(object sender, EventArgs e) 
     {
       


    if(host!=""){
       //在应用程序关闭时运行的代码
        //下面的代码是关键，可解决IIS应用程序池自动回收的问题
        Thread.Sleep(1000);
        //这里设置你的web地址，可以随便指向你的任意一个aspx页面甚至不存在的页面，目的是要激发Application_Start
        string url = "http://"+host+":"+port+"/fd_search.aspx?q=1111111111111";
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流


     wtlog("sws cms server restarting ...");
  
      }







     }
        
     void Application_Error(object sender, EventArgs e) 
     { 
  


     }

     void Session_Start(object sender, EventArgs e) 
     {
         
       Session.Timeout=60000;
      // HttpApplication ap=(HttpApplication)sender;
      // host=ap.Request.Url.Host;

     }

     void Application_Beginrequest(object sender, EventArgs e) 
     {
         

         //myTimer.Enabled = true;


       HttpApplication ap=(HttpApplication)sender;
       host=ap.Request.Url.Host;
      port=ap.Request.Url.Port.ToString();

     }

     void Session_End(object sender, EventArgs e) 
     { 
       


     }






   private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e){



     wtlog("timer thread...");
  


   try{  //try{}重要，防止终止

   

    if(host!=""){
       //在应用程序关闭时运行的代码
        //下面的代码是关键，可解决IIS应用程序池自动回收的问题
        Thread.Sleep(1000);
        //这里设置你的web地址，可以随便指向你的任意一个aspx页面甚至不存在的页面，目的是要激发Application_Start
        string url = "http://"+host+":"+port+"/fd_search.aspx?q=1111111111111";
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流


     wtlog("sws cms server flush ...");
  
      }















    }catch(Exception err){
     wtlog(err.Message + err.StackTrace);
   }





     }
       


       

  


  private void wtlog(string msg){

      try
      {
          string m_fileName = HttpRuntime.AppDomainAppPath + "log.txt";

          StreamReader sr1 = new StreamReader((System.IO.Stream)File.OpenRead(m_fileName), System.Text.Encoding.Default);
          string html = "";



          html = sr1.ReadToEnd();


          sr1.Close();

          if (html.Length > 400000)
          {

              System.IO.File.Delete(m_fileName);

          }



          if (File.Exists(m_fileName))
          {
              ///如果日志文件已经存在，则直接写入日志文件
              StreamWriter sr = File.AppendText(m_fileName);

              sr.WriteLine("sws cms server--> " + DateTime.Now.ToString() + ":");
       

              sr.WriteLine(msg);
              sr.WriteLine("\r\n");
         sr.WriteLine("\r\n");
         sr.WriteLine("\r\n");
              sr.Close();
          }
          else
          {
              ///创建日志文件
              StreamWriter sr = File.CreateText(m_fileName);
              sr.Close();
          }
      }
      catch
      {


      }
   }







</script>

