Beispiel #1
0
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "抓取日志:");
            try
            {
                WebCatch wc = new WebCatch(0);
                wc.GetArticleByCatch();
                sb.Append("完成抓取数据" + wc.totalcatchartend.ToString() + "条,完成入库数据" + wc.totalinsertdbend.ToString() + "条,完成更新数据" + wc.totalupdatedbend.ToString() + "条。");
            }
            catch (Exception ex)
            {
                sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "出现抓取异常:" + ex.Message);
            }

            if (!System.IO.File.Exists(System.Configuration.ConfigurationSettings.AppSettings["LogPath"].ToString() + "catchLog.txt"))
            {
                System.IO.File.Create(System.Configuration.ConfigurationSettings.AppSettings["LogPath"].ToString() + "catchLog.txt");
            }
            string logfile = System.Configuration.ConfigurationSettings.AppSettings["LogPath"].ToString() + "catchLog.txt";
            using (StreamWriter SW = File.AppendText(logfile))
            {
                SW.WriteLine(sb.ToString());
                SW.Close();
            }
        }
 protected void Page_Load(object sender, EventArgs e)
 {
     if (Session["CatchArticle"] == null)
         {
             wc = new WebCatch(Convert.ToInt32(Request["id"]));
             Session["CatchArticle"] = wc;
         }
         else
         {
             wc = (WebCatch)Session["CatchArticle"];
         }
         switch (wc.State)
         {
             case 0:
                 {
                     this.div_load.Visible = false;
                     break;
                 }
             case 1:
                 {
                     this.lab_state.Text = "正在抓取<font color=blue>" + wc.ParseLogicstr + "</font>数据...<br/>已抓取数据:<font color=blue>" + wc.totalcatchartend + " </font>条<br/>已入库数据:<font color=blue>" + wc.totalinsertdbend + " </font>条<br/>已索引数据:<font color=blue>" + wc.totalindexend.ToString() + " </font>条<br/>已更新数据:<font color=blue>" + wc.totalupdatedbend.ToString() + " </font>条<br/>已运行<font color=blue>" + ((TimeSpan)(DateTime.Now - wc.StartTime)).TotalSeconds.ToString("0.000") + " </font><font color='#666699'>秒";
                     this.btn_startwork.Enabled = false;
                     this.btn_startwork.Text = "数据抓取中...";
                     Page.RegisterStartupScript("", "<script>window.setTimeout('location.href=location.href',2000);</script>");
                     this.lab_jg.Text = "";
                     break;
                 }
             case 2:
                 {
                     this.lab_jg.Text = "<font color='#666699'>任务执行完成!<br/>完成抓取数据<font color=blue>" + wc.totalcatchartend.ToString() + "</font>条;<br/>完成入库数据<font color=blue>" + wc.totalinsertdbend.ToString() + "</font>条;<br/>完成索引数据<font color=blue>" + wc.totalindexend.ToString() + " </font>条;<br/>完成更新数据<font color=blue>" + wc.totalupdatedbend.ToString() + "</font>条;<br/>用时<font color=blue>" + ((TimeSpan)(wc.FinishTime - wc.StartTime)).TotalSeconds.ToString("0.000") + " </font>秒.</font><br/><a href='CreateTopStockInfo.aspx'>生成首页资讯</a>";
                     this.btn_startwork.Enabled = true;
                     this.btn_startwork.Text = "点击开始抓取";
                     this.div_load.Visible = false;
                     Session["CatchArticle"] = null;
                     break;
                 }
             case 3:
                 {
                     this.lab_jg.Text = "<font color='#666699'>任务结束!<br/>在<font color=blue>" + ((TimeSpan)(wc.ErrorTime - wc.StartTime)).TotalSeconds.ToString("0.000") + "</font>秒的时候发生错误导致任务失败.</font>'";
                     this.btn_startwork.Enabled = true;
                     this.btn_startwork.Text = "点击开始抓取";
                     this.div_load.Visible = false;
                     Session["CatchArticle"] = null;
                     break;
                 }
         }
 }