简体   繁体   English

如何确定ASP.NET站点(IIS)上的用户数? 他们的信息?

[英]How can I determine the number of users on an ASP.NET site (IIS)? And their info?

Is there a way to determine the number of users that have active sessions in an ASP.NET application? 有没有办法确定ASP.NET应用程序中具有活动会话的用户数? I have an admin/tools page in a particular application, and I would like to display info regarding all open sessions, such as the number of sessions, and perhaps the requesting machines' addresses, or other credential information for each user. 我在特定应用程序中有一个管理员/工具页面,我想显示有关所有打开会话的信息,例如会话数,可能还有请求计算机的地址,或每个用户的其他凭据信息。

In global.aspx 在global.aspx中

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate 
    // mode is set to InProc in the Web.config file. 
    // If session mode is set to StateServer or SQLServer, 
    // the event is not raised.
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

Note: The Application.Lock and Application.Unlock methods are used to prevent multiple threads from changing this variable at the same time. 注意: Application.Lock和Application.Unlock方法用于防止多个线程同时更改此变量。

In Web.config 在Web.config中

Verify that the SessionState is "InProc" for this to work 验证SessionState是否为“InProc”以使其正常工作

    <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="20" />
    </system.web>

In your .aspx file 在.aspx文件中

Visitors online: <%= Application["OnlineUsers"].ToString() %>

Note: Code was originally copied from http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx (link no longer active) 注意:代码最初是从 http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx 复制的(链接不再有效)

ASP.NET Performance Counters like State Server Sessions Active (The number of active user sessions) should help you out. ASP.NET状态服务器会话活动(活动用户会话数)等ASP.NET性能计数器可以帮助您。 Then you can just read and display the performance counters from your admin page.. 然后,您只需从管理页面中阅读并显示性能计数器即可

If you are using .net Membership you could use 如果您使用.net会员资格,您可以使用

Membership.GetNumberOfUsersOnline()

More about it: http://msdn.microsoft.com/en-us/library/system.web.security.membership.getnumberofusersonline.aspx 更多关于它: http//msdn.microsoft.com/en-us/library/system.web.security.membership.getnumberofusersonline.aspx

If you'd like to implement the same mechanism by yourself, you can define like a CurrentUserManager class and implement the singleton pattern here. 如果您想自己实现相同的机制,可以像CurrentUserManager类一样定义并在此处实现单例模式。 This singleton object of class CurrentUserManager would be unique in the AppDomain. 类CurrentUserManager的这个单例对象在AppDomain中是唯一的。 In this class you will create its self instance once, and you will prohibit the others from creating new instances of this class by hiding its constructor. 在这个类中,您将创建一次自我实例,并且您将通过隐藏其构造函数来禁止其他人创建此类的新实例。 Whenever a request comes to this object, that single instance will give the response. 每当请求到达此对象时,该单个实例将给出响应。 So, if you implement a list that keeps the records of every user (when a user comes in, you add him to the list; when he goes out, you remove him from the list). 因此,如果您实现一个列表来保存每个用户的记录(当用户进入时,您将他添加到列表中;当他出去时,您将他从列表中删除)。 And lastly, if you want the current user count you could just ask the list count to this singleton object. 最后,如果你想要当前用户数,你可以只询问列表计数到这个单例对象。

The way I've seen this done in the past is adding extra code to the Session_OnStart event in the Global.asax file to store the information in a session agnostic way, eg a database or the HttpApplicationState object. 我在过去看到这种方式的方法是在Global.asax文件中向Session_OnStart事件添加额外的代码,以便以会话无关的方式存储信息,例如数据库或HttpApplicationState对象。 Depending upon your needs you could also use Session_OnEnd to remove this information. 根据您的需要,您还可以使用Session_OnEnd删除此信息。

You may want to initialise and clean up some of this information using the Application_Start and Application_End events. 您可能希望使用Application_Start和Application_End事件初始化并清除部分信息。

The administration page can then read this information and display statistics etc. 然后,管理页面可以读取此信息并显示统计信息等。

This is explained in more depth at http://msdn.microsoft.com/en-us/library/ms178594.aspx and http://msdn.microsoft.com/en-us/library/ms178581.aspx . 这将在http://msdn.microsoft.com/en-us/library/ms178594.aspxhttp://msdn.microsoft.com/en-us/library/ms178581.aspx中进行更深入的解释。

如果您使用sql server作为会话状态提供程序,则可以使用此代码计算在线用户的数量:

SELECT Count(*) As Onlines FROM ASPStateTempSessions WHERE Expires>getutcdate()

You can use PerformanceCounter to get data from System.Diagnostics namespace. 您可以使用PerformanceCounter从System.Diagnostics命名空间获取数据。 It allows you to get "Sessions Active" and much more. 它允许您获得“会话活动”等等。 It allows you to get from local server as well as remote. 它允许您从本地服务器和远程服务器获取。

Here is an example of how to do it on local machine 以下是如何在本地计算机上执行此操作的示例

void Main()
{
    var pc = new PerformanceCounter("ASP.NET Applications", "Sessions Active", "__Total__");

    Console.WriteLine(pc.NextValue());
}

or for remote server you would do: 或者对于远程服务器,你会做:

void Main()
{
    var pc = new PerformanceCounter("ASP.NET Applications", "Sessions Active", "__Total__", "ServerHostName.domain");

    Console.WriteLine(pc.NextValue());
}

Performance Counters for ASP.NET provides full list of ASP.NET counters that you can monitor ASP.NET的Performance Counters提供了可以监视的ASP.NET计数器的完整列表

Google Analytics comes with an API that can be implemented on your ASP.NET MVC Application. Google Analytics附带了一个可以在ASP.NET MVC应用程序上实现的API。 It has RealTime functionality so the current amount of users on your website can be tracked and returned to your application. 它具有RealTime功能,因此可以跟踪您网站上的当前用户数量并将其返回到您的应用程序。

Here's some information 这是一些信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM