I have an asp.net website and i want to get the number of users currently viewing my s开发者_运维技巧ite. I am aware that there are some third party softwares available, that would give me the list of the users online but i don't want to do that.
Does anyone know how this can be achieved in asp.net? May be if there are any server variables that would keep a track of the website instances that gives the number of users currently visiting the site. Please help me.
if you want to count users which are using your website at the moment you can use the following code in your global.asax file:
private int activeUsers = 0;
protected void Session_Start(Object sender, EventArgs e)
{
activeUsers++;
Context.Items["activeUsers"] = activeUsers;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Context.Items.Add("activeUsers", activeUsers);
}
protected void Session_End(Object sender, EventArgs e)
{
if(activeUsers > 0)
activeUsers--;
}
protected void Application_End(Object sender, EventArgs e)
{
activeUsers = 0;
}
I would use performance counters instead.
Look under ASP.NET Application Performance Counters
http://msdn.microsoft.com/en-us/library/fxk122b4.aspx
精彩评论