asp.net 访问页面访问统计实现 for iis7
上一篇博文中《 asp.net 访问页面访问统计实现 》 中在win10 (iis8+)上运行没有问题,
但客户机子是windows server 2008 的 iis7弄死不对,最好加
<system.web>...<customErrors mode="Off"/>...</system.web>
把问题找到了!
1、_Cache = new System.Web.Caching.Cache(); (iis7不支持)
_Cache = HttpContext.Current.Cache;(更改为)
2、System.Timers.Timer(iis7触发不到事件)
System.Threading.Timer(更改为)
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.Threading; 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.Threading.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 = HttpContext.Current.Cache;// new System.Web.Caching.Cache(); } if (timer1 == null) { var autoEvent = new AutoResetEvent(false); timer1 = new Timer(p => FlushData(), autoEvent, 0, IntervalMinute * 60000); //timer1 = new System.Timers.Timer(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 }; } } void FlushData() { UpdateVisitCount(DateTime.Now); } //private void timer1_Tick(object sender, EventArgs e) //{ // UpdateVisitCount(DateTime.Now); //} } }