统计网站当天的访问量

 /// <summary>
        /// 今日访问人数
        /// </summary>
       public  static int todayCount;

        /// <summary>
        /// 更新今日访问数的标志
        /// </summary>
        static DateTime lastCleanUp;

        /// <summary>
        /// 锁对象
        /// </summary>
        static object _obj = new object();

        /// <summary>
        /// 日志文件的路径
        /// </summary>
        string logFile = AppDomain.CurrentDomain.BaseDirectory + "visitLog.txt";

        protected void Application_Start(object sender, EventArgs e)
        {

            //刚启动,为了防止服务器意外死机重启等因素,需要从记录文件中读取数目
            if (System.IO.File.Exists(logFile))
            {
                string[] lines = System.IO.File.ReadAllLines(logFile);
                if (lines.Length >= 2)
                {

                    int.TryParse(lines[0], out todayCount);
                    DateTime.TryParse(lines[1], out lastCleanUp);
                }
            }
          
        }

        protected void Session_Start(object sender, EventArgs e)
        {

            //锁定对象确定单线程访问
            lock (_obj)
            {
                //如果日期变化了,将今日访问归零
                if (DateTime.Now.Day != lastCleanUp.Day)
                {
                    lastCleanUp = DateTime.Now;
                    todayCount = 0;
                }

                //计数
                todayCount++;
             

                //为了防止服务器死机重启等意外因素丢失数据,每隔50个访客更新一下记录文件
                //这个需要根据访问量调整
                if (todayCount % 50 == 0)
                {
                    string[] fns = new string[] {  todayCount.ToString(), lastCleanUp.ToString() };
                    System.IO.File.Delete(logFile);
                    System.IO.File.WriteAllLines(logFile, fns);
                }

            }


            #region

            ////获取用户访问的页面
            //string pageurl = Request.Url.ToString();
            //if (pageurl.EndsWith("index.aspx")) //判断访问的是否是首页
            //{
            //    //锁定变量
            //    Application.Lock();
            //    //页面访问量加一
            //    Application["StatCount"] = int.Parse(Application["StatCount"].ToString()) + 1;
            //    //解锁
            //    Application.UnLock();
            //}

            #endregion

 

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
          

        }

        protected void Application_End(object sender, EventArgs e)
        {
            //保存当前访问
            string[] fns = new string[] { todayCount.ToString(), lastCleanUp.ToString() };
            System.IO.File.Delete(logFile);
            System.IO.File.WriteAllLines(logFile, fns);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章