1. 程式人生 > >asp.net 訪問頁面訪問統計實現

asp.net 訪問頁面訪問統計實現

space error cti test exe select utf-8 sender else

0x00、背景:

1、用戶訪問網站所有頁面就將訪問統計數加1 ,按每月存放。

2、站點並沒有用到母版面來實現,所有各個頁面都很獨立。

3、網站是很早這前的網站,盡量省改動以前的代碼。按理說我們應該做一個第三方統計來處理會更好,

但應客戶要求還要弄到一個站點下!實際情況是盡量小改頁面就好!

0x01、核心代碼:

1、實現Http攔截操作,核心代碼就是這一個了:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.IO; using System.Linq; using System.Web; using System.Xml; namespace HYSWare.Web { public class CurVisitCount { public DateTime BeginTime{get;set;} public int Count { get; set; } } public class RequestHandler : IHttpModule { private static System.Timers.Timer timer1;
private static System.Web.Caching.Cache _Cache; private int _IntervalMinute; public int IntervalMinute { get { string _IntervalMinuteStr = ConfigurationManager.AppSettings["IntervalMinute"]; if (string.IsNullOrEmpty(_IntervalMinuteStr)) { _IntervalMinute
= 5; } else { _IntervalMinute = Convert.ToInt32(_IntervalMinuteStr); } return _IntervalMinute; } } public RequestHandler() { if (_Cache == null) { _Cache = new System.Web.Caching.Cache(); } if (timer1 == null) { timer1 = new System.Timers.Timer(); timer1.Interval = IntervalMinute * 60000; timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); timer1.AutoReset = true;//設置是執行一次(false)還是一直執行(true); timer1.Enabled = true;//是否執行System.Timers.Timer.Elapsed事件; } if(_Cache["CurVisitCount"]==null) _Cache["CurVisitCount"] = new CurVisitCount { BeginTime = DateTime.Now, Count = 0 }; if(_Cache["VisitCount"]==null) _Cache["VisitCount"] = "0"; } void IHttpModule.Dispose() { } void IHttpModule.Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); //context.EndRequest += new EventHandler(context_EndRequest); } void context_BeginRequest(object sender, EventArgs e) { string[] requestEx = { ".aspx",".htm", ".html"}; HttpApplication application = (HttpApplication)sender; var curRequest = application.Context.Request; if (requestEx.Contains(curRequest.CurrentExecutionFilePathExtension)) { if (_Cache["CurVisitCount"] == null) _Cache["CurVisitCount"]=new CurVisitCount { BeginTime = DateTime.Now, Count = 1 }; CurVisitCount curVisitCount = (CurVisitCount)_Cache["CurVisitCount"]; if (curVisitCount.BeginTime.Year + curVisitCount.BeginTime.Month < DateTime.Now.Year + DateTime.Now.Month) { UpdateVisitCount(curVisitCount.BeginTime); curVisitCount = (CurVisitCount)_Cache["CurVisitCount"]; } curVisitCount.Count += 1; _Cache["CurVisitCount"] = curVisitCount; if (_Cache["VisitCountXml"] == null) _Cache["VisitCountXml"] = GetVisitCountInXml(); int visitCountXml = 0; int.TryParse(_Cache["VisitCountXml"].ToString(),out visitCountXml); _Cache["VisitCount"] = (visitCountXml + curVisitCount.Count).ToString(); } //application.Context.Response.Write("自定義ModuleRequest開始"); } private int GetVisitCountInXml() { int ret = 0; string _VisitPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\Xml\\VisitCountData.xml"; XmlDocument doc = new XmlDocument(); if (File.Exists(_VisitPath)) { doc.Load(_VisitPath); var curNode = doc.SelectNodes("//Visit"); foreach (XmlNode m in curNode) { var countAttr = m.Attributes.GetNamedItem("VCount"); int v = 0; int.TryParse(countAttr.Value, out v); ret += v; } } return ret; } private void UpdateVisitCount(DateTime time) { if (_Cache["CurVisitCount"] == null) _Cache["CurVisitCount"]= new CurVisitCount { BeginTime = DateTime.Now, Count = 0 }; CurVisitCount curVisitCount = (CurVisitCount)_Cache["CurVisitCount"]; if (curVisitCount.Count > 0) { string _VisitPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\Xml\\VisitCountData.xml"; XmlDocument doc = new XmlDocument(); if (File.Exists(_VisitPath)) { doc.Load(_VisitPath); var curNode = doc.SelectSingleNode(string.Format("//Visit[@Year=‘{0}‘ and @Month=‘{1}‘]", time.Year, time.Month)); if (curNode != null) { var countAttr = curNode.Attributes.GetNamedItem("VCount"); int count = 0; int.TryParse(countAttr.Value, out count); countAttr.Value = (count + curVisitCount.Count).ToString(); } else { //有文件但沒有本月數據 XmlNode root = doc.SelectSingleNode("Visits"); XmlElement element1 = doc.CreateElement("Visit"); element1.SetAttribute("Year", time.Year.ToString()); element1.SetAttribute("Month", time.Month.ToString()); element1.SetAttribute("VCount", curVisitCount.Count.ToString()); root.AppendChild(element1); } } else { XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); //創建一個根節點(一級) XmlElement root = doc.CreateElement("Visits"); doc.AppendChild(root); //創建節點(二級) XmlElement element1 = doc.CreateElement("Visit"); element1.SetAttribute("Year", time.Year.ToString()); element1.SetAttribute("Month", time.Month.ToString()); element1.SetAttribute("VCount", curVisitCount.ToString()); root.AppendChild(element1); } doc.Save(_VisitPath); _Cache["VisitCountXml"] = GetVisitCountInXml(); _Cache["VisitCount"] = _Cache["VisitCountXml"]; _Cache["CurVisitCount"] = new CurVisitCount { BeginTime = DateTime.Now, Count = 0 }; } } private void timer1_Tick(object sender, EventArgs e) { UpdateVisitCount(DateTime.Now); } } }

2、web.config中添加配置

<!--
  有關如何配置 ASP.NET 應用程序的詳細信息,請訪問
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <appSettings>
......
<!--設置更新訪問統計Xml,間隔時間(分鐘)默認為5分鐘--> <add key="IntervalMinute" value="5"/> </appSettings>  ....... <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="customerModule" type="HYSWare.Web.RequestHandler, HYSWare.Web"/> </modules> ...... </system.webServer> </configuration>

3、存放數據的XML文件

<?xml version="1.0" encoding="utf-8"?>
<Visits>
  <Visit Year="2017" Month="8" VCount="92" />
  <Visit Year="2017" Month="7" VCount="13000000" />
</Visits>

4、取得緩存的的訪問數據 VisitCountHandler.ashx,VisitCountHandler.ashxcs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HYSWare.Web
{
    /// <summary>
    /// VisitCountHandler 的摘要說明
    /// </summary>
    public class VisitCountHandler : IHttpHandler
    {
        private static System.Web.Caching.Cache _Cache;

        public VisitCountHandler() {
            if (_Cache == null)
            {
                _Cache = new System.Web.Caching.Cache();
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (_Cache["VisitCount"] == null)
                _Cache["VisitCount"] = "0";
            context.Response.Write(_Cache["VisitCount"].ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

5、調用取訪問統計數據的有js代碼

var visitCount = 0;
        $.ajax({
            type: "Post",
            url: "VisitCountHandler.ashx",
            data: "{}",
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                visitCount = data;
            },
            error: function (err) {
                alert(err);
            }
        });

0x02、總結:

主要還是覺得緩存使用感覺有點不對的樣子,也許有更好的方法!

還有覺得攔截方法也是挻好用的!

asp.net 訪問頁面訪問統計實現