views:

433

answers:

3

I would like to know the number of users logged into my ASP.NET 2.0 application.

Points to be considered: 1) Simplest way would be to use Application or Cache object to have the counts on Session start or end. However this would fail if there is a worker process recycle. Wouldn't it?

2) Should't make a difference whether the session is inproc/state server managed/ or SQL server managed.

3) Should preferably be seamless to a web-farm architecture.

+1  A: 

You should store a user's online status in a database. Each time a page is navigated, their LastActivity information (in the database table) is updated.

Create a SQL job that runs and logs users off if there is X amount of inactivity (and of course, if they actually do hit logout, update the database to mark the user offline)

TheGeekYouNeed
I was hoping to avoid an explicit CRUD operation for this purpose...
Srikanth Venugopalan
A: 

ASP.Net comes with several performance counters

http://msdn.microsoft.com/en-us/library/fxk122b4.aspx

State Server Sessions Active The number of currently active user sessions. This counter is available only on the computer where the state server service (aspnet_state) is running.

Requests/Sec The number of requests executed per second. This represents the current throughput of the application. Under constant load, this number should remain within a certain range, barring other server work (such as garbage collection, cache cleanup thread, external server tools, and so on).

Raj Kaimal
How does this approach rate when looking at a web-farm architecture? Also, what is the alternative when using InProc mode?
Srikanth Venugopalan
+1  A: 

If you use the built-in ASP.NET membership provider, then there's the ever-so-handy Membership.GetNumberOfUsersOnline() method.

(Of course, it only works authenticated users...)

Dean Harding
+1 to you. Any do's and don'ts in a web-farm architecture? Is this a machine dependant approach?
Srikanth Venugopalan
The ASP.NET membership stuff uses a provider-based architecture, so it depends which provider you use. All of the built-in ones (SqlMembershipProvider, ActiveDirectoryMembershipProvider) work in a web farm, though.
Dean Harding