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条回答
狗以群分
2楼-- · 2019-02-07 07:12
public class ActiveUsers
{
    private static List<LastUserActivity> users;
    private static object syncroot = new object();
    private static DateTime lastPruned;

    public static int SessionLength = 20;

    static ActiveUsers()
    {
        users = new List<LastUserActivity>();
        lastPruned = DateTime.UtcNow;
    }

    public void RecordUserActivity(int userId)
    {
        lock (syncroot)
        {
            var user = users.FirstOrDefault(x => x.UserId == userId);
            if (user == null)
            {
                user = new LastUserActivity() { UserId = userId };
                users.Add(user);
            }
            user.UtcTime = DateTime.UtcNow;

            if (lastPruned.AddMinutes(5) < DateTime.UtcNow)
            {
                Prune();
                lastPruned = DateTime.UtcNow;
            }
        }
    }

    private static void Prune()
    {
        users.RemoveAll(x => x.UtcTime.AddMinutes(SessionLength) < DateTime.UtcNow);
    }

    public int GetActiveUsers()
    {
        return users.Count;
    }
}
public class LastUserActivity
{
    public int UserId { get; set; }
    public DateTime UtcTime { get; set; }
}

Add a call to ActiveUsers into a method in global.asax (eg. BeginRequest, AcquireRequestState).

查看更多
We Are One
3楼-- · 2019-02-07 07:14

Does the website log the user in? If it does, then you can update a "Last Visit" field in the user table every time they request a new page, and then every hour, just do a SQL query that grabs everybody who has a "Last Visit" timestamp within the last 15 minutes or so (assumed to currently be on the site).

If you're not having people log in, you could just as easily do it by IP address instead of username. With this method, though, you may run into some proxy issues (ie multiple users from the same corporate network may all come from a single IP address, so they only count as one user in your totals), but that should be minimal.

查看更多
Lonely孤独者°
4楼-- · 2019-02-07 07:15

I think what I've used in the past was the Global.asax functions mainly centering around the Session_Start and Session_End, increasing a count with the Start, and then the end is a bit tricky because of the session timeout. If you are not concerned a lot with how exactly accurate the count is then you can stop here.

If not then you'd probably use a combination of the javascript onUnload event with some sort of ajax request to invalidate the session and subtract the user. The event would have to see if the user was actually leaving the page or just going to another page on the site.

Anyway, start there. I remember having to do with ASP sites so there is definately some information out there on this method.

查看更多
登录 后发表回答