Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.....
C#
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

C#
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:
	}


}
Posted
Updated 11-Apr-12 10:01am
v3
Comments
Steve Maier 11-Apr-12 15:45pm    
Edit: Just some simple formatting...

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900