1. Use a file to record and access the file each time: the deadlock is serious and the resource consumption is too large.
2. Use Database records: High Access loss.
3. Use Global. asax and Timer for statistics: It is preferable.
The following is an example:
In general, it is easy to calculate the number of online users. However, to make an online list and save the user's access logs requires a lot of system resources, it is hard to say whether it is cost-effective (I only read the requirement documents, regardless of the other ...);
The IHttpModule method used earlier is also good.
Using System;
Using System. ComponentModel;
Using System. Web;
Using System. Web. SessionState;
Using System. Data;
Using System. Data. OleDb;
Namespace Butterfly {
/// <Summary>
/// Summary of Global.
/// </Summary>
Public class Global: System. Web. HttpApplication
{
Private static System. Threading. Timer timer;
Private const int interval = 1000*60*10; // check the time interval between online users
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. IContainer components = null;
Public Global ()
{
InitializeComponent ();
}
Protected void Application_Start (Object sender, EventArgs e)
{
If (timer = null)
Timer = new System. Threading. Timer (new System. Threading. TimerCallback (ScheduledWorkCallback ),
Sender, 0, interval );
DataTable userTable = new DataTable ();
UserTable. Columns. Add ("UserID"); // user ID
UserTable. Columns. Add ("UserName"); // User Name
UserTable. Columns. Add ("FirstRequestTime"); // time of the first request
UserTable. Columns. Add ("LastRequestTime"); // time of the last request
UserTable. Columns. Add ("ClientIP ");//
UserTable. Columns. Add ("ClientName ");//
UserTable. Columns. Add ("ClientAgent ");//
// UserTable. Columns. Add ("LastRequestPath"); // The Last accessed page
UserTable. PrimaryKey = new DataColumn [] {userTable. Columns [0]};
UserTable. AcceptChanges ();
Application. Lock ();
Application ["UserOnLine"] = userTable;
Application. UnLock ();
}
Protected void Session_Start (Object sender, EventArgs e)
{
}
Protected void Application_BeginRequest (Object sender, EventArgs e)
{
}
Protected void Application_EndRequest (Object sender, EventArgs e)
{
}
Protected void Application_AcquireRequestState (Object sender, EventArgs e)
{
HttpApplication mApp = (HttpApplication) sender;
If (mApp. Context. Session = null) return;
If (mApp. Context. Session ["UserID"] = null) return;
String userID = mApp. Context. Session ["UserID"]. ToString ();
DataTable userTable = (DataTable) Application ["UserOnLine"];
DataRow curRow = userTable. Rows. Find (new object [] {userID });
If (curRow! = Null)
{
This. GetDataRowFromHttpApp (mApp, ref curRow );
}
Else
{
DataRow newRow = userTable. NewRow ();
This. GetDataRowFromHttpApp (mApp, ref newRow );
UserTable. Rows. Add (newRow );
}
UserTable. AcceptChanges ();
Application. Lock ();
Application ["UserOnLine"] = userTable;
Application. UnLock ();
}
Protected void Application_AuthenticateRequest (Object sender, EventArgs e)
{
}
Protected void Application_Error (Object sender, EventArgs e)
{
}
Protected void Session_End (Object sender, EventArgs e)
{
}
Protected void Application_End (Object sender, EventArgs e)
{
}
# Code generated by region Web Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. components = new System. ComponentModel. Container ();
}
# Endregion
Private void GetDataRowFromHttpApp (HttpApplication mApp, ref DataRow mRow)
{
If (mApp. Context. Session = null) return;
If (mApp. Context. Session ["UserID"] = null | mApp. Context. Session ["UserName"] = null) return;
String userID = mApp. Context. Session ["UserID"]. ToString ();
String userName = mApp. Context. Session ["UserName"]. ToString ();
// String requestPath = mApp. Request. Path;
If (mRow ["UserID"]. ToString (). Length <1)
{
& Nb