How can I find number of visitors/users at my site (IIS7/asp.net) at any given moment?

oekstrem picture oekstrem · Apr 27, 2010 · Viewed 18.9k times · Source

I need to display how many users are browsing my site. The site is running on iis7, and we are using asp.net 3.5.

Is the number of active sessions a good way to go? The number does not need to be very accurate.

No history is needed, I just want to know how many users are "online" right now, and display that on the page itself.

Answer

David Neale picture David Neale · Apr 27, 2010

You can use Windows Performance counters for this (perfmon)

ASP.NET Applications > Sessions Active counter.

You can access these performance counters using the System.Diagnostics namespace.

This code worked for me:

        PerformanceCounter pc = new PerformanceCounter("ASP.NET Applications",
     "Sessions Active", "__Total__", "MYSERVERHOSTNAME.domain");

        while (true)
        {
            Console.WriteLine(pc.NextValue());
            System.Threading.Thread.Sleep(1000);
        }

I had this problem so take a look here if the counter seems too high: http://support.microsoft.com/kb/969722