GlobalData 全局数据 - 操作/计算访问统计量

2017-06-16 11:05:55  访问(1949) 赞(0) 踩(0)

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;

/// <summary>
/// 全局数据
/// </summary>
public class GlobalData
{
    /// <summary>
    /// GlobalData 全局数据 - 操作/计算访问统计量
    /// </summary>
    public GlobalData()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 是否已经加载
    /// </summary>
    private static bool m_isLoad = false;

    /// <summary>
    /// 网站人数
    /// </summary>
    private static ulong m_StatCount = 0;

    /// <summary>
    /// 网站人数
    /// </summary>
    public static ulong StatCount
    {
        get
        {
            return m_StatCount;
        }
    }

    /// <summary>
    /// 网站的文本地址
    /// </summary>
    /// <returns></returns>
    private static string GetFileName()
    {
        if (HttpContext.Current == null)
            return "";
        
        return HttpContext.Current.Request.PhysicalApplicationPath + "visitlog/StatCount.txt";

    }

    /// <summary>
    /// 读取文件
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private static string ReadFile(string str)
    {
        try
        {
            if (str == null || str.Length == 0 || !File.Exists(str))
                return "";

            return File.ReadAllText(str);
        }
        catch
        {
            return "";
        }
    }

    /// <summary>
    /// 写入文件
    /// </summary>
    /// <param name="str"></param>
    /// <param name="theValue"></param>
    private static void WriteFile(string str, ulong theValue)
    {
        try
        {
            if (str == null || str.Length == 0)
                return;

            if (File.Exists(str))
            {
                File.Delete(str);
                File.WriteAllText(str, theValue.ToString());

                return;
            }

            FileInfo fi = new FileInfo(str);

            if (!fi.Directory.Exists)
                fi.Directory.Create();


            File.WriteAllText(fi.FullName, theValue.ToString());
        }
        catch
        {

        }
    }

    /// <summary>
    /// 应用启动
    /// </summary>
    public static void Application_Start()
    {
        AppStart();
    }

    protected static void AppStart()
    {
        try
        {
            if (HttpContext.Current == null)
                return;


            if (!m_isLoad)
            {
                string fileName = GetFileName();

                if (!File.Exists(fileName))
                    return;

                string fileValue = ReadFile(fileName);

                if (fileValue != null && fileValue.Length > 0)
                {
                    ulong uv = 0;
                    if (ulong.TryParse(fileValue, out uv))
                        m_StatCount = uv;
                }

                m_isLoad = true;


            }

        }
        catch
        {

        }
    }

    /// <summary>
    /// Session启动
    /// </summary>
    public static void Session_Start()
    {
       
        try
        {
            if (HttpContext.Current == null)
                return;

            if (!m_isLoad)
            {
                // 加载数据 //
                AppStart();
            }
             
            //获取用户访问的页面
            string pageurl 
                = 
                HttpContext.Current.Request.Url.ToString();

            //判断访问的是否是默认页
            if (pageurl.ToLower().EndsWith("default.aspx"))
            {
                m_StatCount += 1;

                WriteFile(GetFileName(), m_StatCount);
            }
        }
        catch
        {

        }
    }
}


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)