Simple access statistics counters in ASP. NET

Source: Internet
Author: User

It is a headache to complete the Website access counter. Today, the first version is completed. The record information is as follows:
1. record the number of visitors per day
2. record the number of clicks per day

To achieve these two simple functions, it also makes me really a headache. At present, I have not considered much about performance, just to achieve it.
Brief description:
To precisely implement the counter, you can either simply use application. Lock to lock the thread or write the multi-threadProgram, They will cause a serious impact on the system performance. Therefore, the less things you do during the lock thread, the better. Therefore, you cannot frequently write the Count statistics of user access to the database, I used the write interval once method to reduce the number of data records to the database.CodeIn application_endrequest, check whether the time of the user's last record data has exceeded the set time interval. If yes, record the data again, which greatly reduces the number of database writes, the impact on performance and database servers is also smaller.
If the server is stopped, user access during the last interval may not be recorded, so it is recorded in the application_end event.

The parameters passed to the database are: current date (in the yyyy-mm-dd format), Count type: Number of users/Page clicks, Count. In the stored procedure, data is first updated using the date parameter as the primary key. If the update fails (no record has been recorded on the current day), new data is inserted. In the database, the table has three fields: todaydate (date), todayvisitcnt (number), and todayclickcnt (number ). Todaydate is the primary key.

Option explicit on
Option strict on

Imports system
Imports system. Web
Imports system. Web. sessionstate
Imports system. runtime. remoting

Imports system. Io
Imports Microsoft. VisualBasic

Namespace myweb
Public class global
Inherits system. Web. httpapplication

'Save access count
Private shared _ visitcount as new hashtable
'The time when the last count information was updated
Private shared _ lastupdate as datetime

# Region "code generated by the component designer"

Public sub new ()
Mybase. New ()

'The call is required by the component designer.
Initializecomponent ()

'Add any initialization after initializecomponent () is called.

End sub

'Required by the component designer
Private components as system. componentmodel. icontainer

'Note: The following process is required by the component designer
'You can use the component designer to modify this process.
'Do not use the code editor to modify it.
<System. Diagnostics. debuggerstepthrough ()> private sub initializecomponent ()
Components = new system. componentmodel. Container
End sub

# End Region

# Region "system events"

sub application_start (byval sender as object, byval e as eventargs)
'the system startup time is set to the last update time
_ lastupdate = datetime. now
end sub

Sub session_start (byval sender as object, byval e as eventargs)
'The number of users accessing the current date plus 1
Application. Lock ()
Dim key as string = now. tostring ("yyyy-mm-dd") & "_ usercount"
_ Visitcount (key) = CINT (_ visitcount (key) + 1
Application. Unlock ()
End sub

Sub application_endrequest (byval sender as object, byval e as eventargs)
'The number of user date page requests increases by 1, and is recorded in the database after a certain amount of access
Application. Lock ()

Dim key as string = now. tostring ("yyyy-mm-dd") & "_ pagecount"
Dim count as integer

Count = CINT (_ visitcount (key ))
Count + = 1
_ Visitcount (key) = count

'If the last record time exceeds the specified interval, record the counter again
If datetime. Now. Subtract (_ lastupdate). totalseconds> 60 then
1. Record all access information to the database
Dim item as string
Dim mdate as date
Dim mtype as string
Dim mcount as integer

For each item in _ visitcount. Keys
Mcount = CINT (_ visitcount (item ))
Mdate = cdate (split (item, "_") (0 ))
Mtype = CSTR (split (item, "_") (1 ))
'Write data to the database
With new businessfacade. pagessystem
. Visitcountlog (mdate, mtype, mcount)
End
Next

'2. Return the counter to zero
_ Visitcount. Clear ()
'Update the record time
_ Lastupdate = datetime. Now
End if

Application. Unlock ()

End sub

Sub application_end (byval sender as object, byval e as eventargs)
'Triggered at the end of the application

1. Record all access information to the database
Dim item as string
Dim mdate as date
Dim mtype as string
Dim mcount as integer

For each item in _ visitcount. Keys
Mcount = CINT (_ visitcount (item ))
Mdate = cdate (split (item, "_") (0 ))
Mtype = CSTR (split (item, "_") (1 ))
With new businessfacade. pagessystem
. Visitcountlog (mdate, mtype, mcount)
End
Next
'2. Return the counter to zero
_ Visitcount. Clear ()

End sub

# End Region

End Class
End namespace

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.