跟踪访问我网站的用户信息 [英] trace user information who is visiting my website

查看:52
本文介绍了跟踪访问我网站的用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪正在访问我的网站的用户信息(无论是否登录),并将用户信息存储在DB或Text文件中,但是,我只想为一次访问创建一个文件.
如何批准此功能.

1.我应该创建HttpModule还是
2.在Global.ascx文件中启动会话
什么是最好的方式...


我只是在下面写了一些代码.....

I would like to trace user information who is visiting my website(regardless of log in or not) and store user information in DB or Text file however, I only want to create one file for one visit.
How can I approch this functionality.

1. Should I creat HttpModule or
2. Session Start in Global.ascx file
what is the best way to approcah ...


I just scracthed some code below.....

void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        try
        {
            UserInfo uInfo = new UserInfo();
            uInfo.SessionID = this.Session.SessionID;
            uInfo.UserName = HttpContext.Current.User.Identity.Name;
            uInfo.StartTime = DateTime.Now.ToString();
            uInfo.MachineName = CallingMachineName;

            TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);

            HttpContext.Current.Cache.Insert(uInfo.CacheKey, uInfo, null, DateTime.MaxValue, SessTimeOut);
        }
        catch (Exception ex)
        {
            throw new Exception("Error in Session_Start function", ex);
        }
    }


 void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    {

        //Every call to the asp.net application can be intercepted
        //through this event
        try
        {
            UserInfo uInfo = new UserInfo();
            uInfo.SessionID = this.Session.SessionID;

            if (HttpContext.Current.Cache[uInfo.CacheKey] != null)
            {
                // Accessing the Cache Item extends the Sliding Expiration automatically.
                string sUrl = HttpContext.Current.Request.RawUrl;
                if (sUrl.LastIndexOf("/") != 0)
                {
                    sUrl = sUrl.Substring(sUrl.LastIndexOf("/") + 1);
                }
                ((UserInfo)HttpContext.Current.Cache[uInfo.CacheKey]).LastPageVisited = sUrl;
            }
        }
        catch (Exception ex)
        {
            //do nothing
        }
    }




我在标签
中进行了测试




AND I tested in label

private void Page_Load(object sender, System.EventArgs e)
{
	try
	{
		IDictionaryEnumerator CacheEnum =Cache.GetEnumerator();
                StringBuilder sb = new StringBuilder();

		int intCount=0;
		while (CacheEnum.MoveNext())
		{
		    UserInfo uInfoCacheItem = (UserInfo)CacheEnum.Entry.Value;
                    sb.Append("UserName : ");
                    sb.Append(uInfoCacheItem.UserName + "");

                    sb.Append("SessionID : ");
                    sb.Append(uInfoCacheItem.SessionID + "");

                    sb.Append("StartTime : ");
                    sb.Append(uInfoCacheItem.StartTime + "");


                    sb.Append("Duration: ");
                    sb.Append(uInfoCacheItem.Duration + "");


                    sb.Append("LastPageVisited: ");
                    sb.Append(uInfoCacheItem.LastPageVisited + "");


                    sb.Append("MachineName:");
                    sb.Append(uInfoCacheItem.MachineName + "");

                    Label1.Text = string.Format("{0}",  sb.ToString());
		    intCount +=1;
		}
	}
	catch(Exception ex)
	{
		//TODO:
	}


}

推荐答案

由于您每次访问只想登录一次,所以我说Session_Start事件将是处理此问题的好地方. HttpModule确实为将来的增强或扩展提供了更大的灵活性.

我还将选择数据库而不是文件.在这种情况下,数据库的性能和可靠性要高得多.
Since you only want to log once per visit the I would say the Session_Start event would a good place to handle this. A HttpModule does give more flexibility for future enhancements or extensions though.

I would also opt for a database rather than a file. A database is much more performant and reliable for this situation.


这篇关于跟踪访问我网站的用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆