On an ASP.NET site, what are some techniques that could be used to track how many users are logged in to the site at any given point in time?
So for example, I could
On Global.asax
protected void Application_Start(object sender, EventArgs e)
{
Application["SessionCount"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) + 1;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) - 1;
Application.UnLock();
}
Get Application["SessionCount"] on the page you want
There are performance monitor stats for Sessions Active within the ASP.NET performance objects, and you can track all instances, or individual apps. You can access these stats through Admin Tools → Performance, or programmatically via WMI.
A very basic PowerShell script to get such a total of such counters:
(Get-WmiObject Win32_PerfRawData_ASPNET_ASPNETApplications SessionsActive).SessionsActive
A filter should be able to get the stat for a specific site.
This sort of depends on your site. If your using the ASP.Net Membership Providers there is a method: Membership.GetNumberOfUsersOnline()
which can tell you how many logged in users there are. I believe there are also performance counters. The notion of a logged in user is a user who did something within the last x minutes where x is configurable.
You could also use performance counters to track incoming requests if you want to get a sense of how much activity there is.
Just so you know the SQL ASP Membership providers implements this by recording an activity date on a field in the DB. It when just queries it for all activity within x minutes.
I added a client side polling function which hits our server every 2 minutes, so while a user is sitting on the page I know they are there even if there is no activity. This also let me force the user out of the system, provides a method deliver other system messages. Kind of nice.