loyung

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

以下网站访问量统计为给予Session_Start的简单网站访问量统计,如果需要做IP和时间段访问统计需另行整理数据。

如果需要查看访问记录,可以查看App_Data下AccessRecord.txt文件

第一步:在App_Data下建立一个XML文件“SystemVisitCount.xml”,将借助文件来进行访问量记录。XML文件大致如下:

<?xml version="1.0" encoding="utf-8"?>
<Condition>
  <Count>100517</Count>
  <DayCount>3,2015/12/13 00:00:00</DayCount>
</Condition>


第二步:打开“Global.asax”全局配置文件

(重要)每个会话建立,根据当前时间判断如何记录当前访问。

在“Session_Start”中可以这样定义:

 void Session_Start(object sender, EventArgs e) 
    {
        Application.Lock();       
        /**********总访问量****************/
        int intStat = 0;
        intStat = (int)Application["counter"];
        ++intStat;
        object objCount = intStat;
        Application["counter"] = objCount;
        string serverFile = Server.MapPath("~/App_Data/SystemVisitCount.xml");
        System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
        xmldoc.Load(serverFile);
        xmldoc.SelectSingleNode("Condition/Count").InnerText = intStat.ToString();
        xmldoc.Save(serverFile); 
        /*********************************/

        /**********今日访问量****************/
        int count;// 记录文本中的日访问量
        string NowDay; //记录文本中访问时间
        string serverFileDay = Server.MapPath("~/App_Data/SystemVisitCount.xml");
        System.Xml.XmlDocument xmldocDay = new System.Xml.XmlDocument();
        xmldocDay.Load(serverFileDay);
        string strs = xmldocDay.SelectSingleNode("Condition/DayCount").InnerText; //保存从文件中读取当天访问信息
        string[] str = strs.Split(','); //将读取的信息存放在字符串数组str中
        count = Convert.ToInt32(str[0]); //日访问量
        string day = str[1]; //最近一次访问时间
        NowDay = DateTime.Now.ToString();
        //文件中保存的时间值与系统时间相比,如果系统时间大,则重新开始计数
        if (DateTime.Compare(Convert.ToDateTime(NowDay), Convert.ToDateTime(day)) >= 0)
        {
            count = 0;
            day = DateTime.Now.AddDays(1).ToShortDateString() + " " + "00:00:00"; //day保存下一天的开始时间
            string NewDayStr = "0" + "," + day.ToString();
            // 将数据记录写入文件
            //string file_path0 = Server.MapPath("counter.txt");
            xmldocDay.SelectSingleNode("Condition/DayCount").InnerText = NewDayStr;
            xmldocDay.Save(serverFile);
        }
        object objcount = count;
        object objday = day;
        //日访问量
        Application["dayCounter"] = objcount;
        //最近一次访问时间
        Application["day"] = objday;
        // 数据累加
        int Stat = 0;
        //获取Application对象中的日访问量
        Stat = (int)Application["dayCounter"];
        Stat += 1;
        object obj = Stat;
        Application["dayCounter"] = obj;
        //保存日期
        string day0 = (string)Application["day"];
        string str0 = obj.ToString() + "," + day0.ToString();
        // 将数据记录写入文件
        xmldocDay.SelectSingleNode("Condition/DayCount").InnerText = str0;
        xmldocDay.Save(serverFile);   
        /**************记录访问来源***************/
        string RecordPath = Server.MapPath("~/App_Data/AccessRecord.txt");
        FileInfo fileInfo = new FileInfo(RecordPath);
        if (!fileInfo.Exists)
        {
            using (FileStream fs = fileInfo.Create()) { }
        }
        //记录访问者IP地址,防止恶意攻击。
        string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        //记录访问页面
        string IpVisitPage = Request.Url.ToString();
        //获取访问时间
        DateTime ipDatetime = DateTime.Now;
        System.IO.StreamWriter sw = new System.IO.StreamWriter(RecordPath, true);
        sw.WriteLine("【IP】" + ipAddress + "; 【页面】" + IpVisitPage + " ;【访问时间】" + ipDatetime + "; 【当日访问】" + Application["dayCounter"]);
        sw.Close(); sw.Dispose();
        //判断则只保留最新1000条访问记录
        string[] lines = System.IO.File.ReadAllLines(RecordPath);       
        if (lines.Length > 1000)
        {
            int overflow = lines.Length - 1000;
            List<string> list = new List<string>();
            list.AddRange(lines);
            for (int i = 0; i < overflow; i++)
            {
                list.RemoveAt(i);
            }
            lines = list.ToArray();
            System.IO.File.WriteAllLines(RecordPath, lines);
            Application["Countee"] = list.Count;
        }
        
        Application.UnLock();
    }

 

 

重置当日访问量,这里是给予Application,当程序开始跑起来时开始记录当日访问,具体可以深入了解“Application_Start”执行。

在“Application_Start”中可以这样定义:

 

    void Application_Start(object sender, EventArgs e) 
    {
        /**********总访问量****************/
        string serverFile = Server.MapPath("~/App_Data/SystemVisitCount.xml");
        int intCount = 0;
        System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
        xmldoc.Load(serverFile);
        string strCount = xmldoc.SelectSingleNode("Condition/Count").InnerText;
        intCount = int.Parse(strCount);
        object obj = intCount;
        Application["counter"] = obj;
        /*********************************/

        /**********今日访问量****************/
        Application["dayCounter"] = 0;
        Application["day"] = DateTime.Now.ToString();
        /*********************************/ 
    }

 

当我们了解到“Application_End”在引用程序池回收,网站关闭,IIS重启时执行,可以在“Application_End”中定义刷新记录访问量。

在“Application_End”中可以这样定义:

 

    void Application_End(object sender, EventArgs e) 
    {
        /**********总访问量****************/
        string serverFile = Server.MapPath("~/App_Data/SystemVisitCount.xml");
        int intStat = 0;
        intStat = (int)Application["counter"];
        System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
        xmldoc.Load(serverFile);
        xmldoc.SelectSingleNode("Condition/Count").InnerText = intStat.ToString();
        xmldoc.Save(serverFile);
        /*********************************/

        /**********今日访问量****************/
        int Stat = 0;
        Stat = (int)Application["dayCounter"];
        string day0 = (string)Application["day"];         //保存日期
        string str = Stat.ToString() + "," + day0.ToString();
        string serverFileDay = Server.MapPath("~/App_Data/SystemVisitCount.xml");
        System.Xml.XmlDocument xmldocDay = new System.Xml.XmlDocument();
        xmldocDay.Load(serverFileDay);
        xmldocDay.SelectSingleNode("Condition/DayCount").InnerText = str;
        xmldocDay.Save(serverFileDay);      
        /*********************************/
    }

 

posted on 2015-04-13 11:51  loyung  阅读(4165)  评论(0编辑  收藏  举报