Determining an 'active' user count of an A

2019-02-07 06:45发布

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 produce a report showing something like this:

        10:00am  11:00am  12:00pm  1:00pm  2:00pm ....
 3/25      25      32       45      40      37
 3/26      31      38       50      57      40
 3/27      28      37       46      35      20
 etc. 

EDIT: No, we're not using ASP.NET Membership provider, it uses a home-grown/hacky session based method of determining whether or not a user is in 'logged in' status.

9条回答
Evening l夕情丶
2楼-- · 2019-02-07 06:51

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

查看更多
别忘想泡老子
3楼-- · 2019-02-07 06:56

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.

Edit

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.

查看更多
We Are One
4楼-- · 2019-02-07 06:56

First set Session timeout for 1 minute.

Create a simple heartbeat.aspx page with no HTML content just the following javascript code:

<html>
  <head>
  <script language="javascript">
  function reloadPage()
  {
    window.navigate("heartbeat.aspx");
  }
  </script>
  </head>
<body onload="window. setTimeout(‘reloadPage()’, 30000)">
</body>
</html>

This will re-request itself every 30 seconds and keep session alive.

Put heatbeat.aspx page in a hidden frame.

To get user count just get session count by using static counter in Session_Start and Session_End events in Global.asax.

查看更多
forever°为你锁心
5楼-- · 2019-02-07 07:10

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 ToolsPerformance, 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.

查看更多
6楼-- · 2019-02-07 07:10

If you are using InProc session state, you can do it like this in global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Application("ActiveSessionCount") = 0
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    Application("ActiveSessionCount") += 1
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    Application("ActiveSessionCount") -= 1
End Sub

To access the value on a web form is just as simple

Dim Count as Integer = Application("ActiveSessionCount")
查看更多
老娘就宠你
7楼-- · 2019-02-07 07:12

If using SQL Server for Session storage (i.e. <sessionState> mode is "SQLServer" in web.config), you can't use a solution that relies on Session_End in global.asax, as the method will never be called.

However, assuming that SQL Agent is running the DeleteExpiredSessions job correctly (which it should be by default when SQL State was installed), you can just run the following SQL against the Session Db:

SELECT COUNT(SessionId) FROM ASPStateTempSessions
查看更多
登录 后发表回答