以下網站訪問量統計為給予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); /*********************************/ }
