Count the access time of the Asp.net page

Source: Internet
Author: User

During project performance, a simple httpmodule is written to facilitate tracking of the time spent on the PostBack or callback of each page to print the access time.

Compile the following class into a DLL during use, configure it in Web. config, and then open debugview.

Configuration

<Httpmodules>
<Clear/>
<Add name = "tracingmodule" type = "performancetracing. tracingmodule, performancetracing"/>

.....

</Httpmodules>

Code

 

Code

  Using  System;
Using System. Collections. Generic;
Using System. text;
Using System. Web;
Using System. diagnostics;

Namespace Performancetracing
{
Public Class Tracingmodule: ihttpmodule
{
# Region Ihttpmodule members

Public Void Dispose ()
{
}

Public Void Init (httpapplication context)
{
Context. beginrequest + = New Eventhandler (context_beginrequest );
Context. endrequest + = New Eventhandler (context_endrequest );
}

Private String Getfilepath ( Object Sender)
{
String Strpath = (Httpapplication) sender). Request. filepath;

If (Strpath ! = Null && Strpath. endswith ( " Aspx " ))
{
Return Strpath. substring (strpath. lastindexof ( " / " ));
}

Return String . Empty;
}

Void Context_endrequest ( Object Sender, eventargs E)
{
String Filename = Getfilepath (sender );
If (Filename. endswith ( " Aspx " ))
{
Stopwatch perfmonwatch =
Httpcontext. Current. items [ " Stopwatch " ] As Stopwatch;

If (Perfmonwatch ! = Null )
{
Perfmonwatch. Stop ();
String MSG = " Page name: " + Filename + " . The total response time: "
+ Perfmonwatch. elapsed. totalseconds + " S. iscallback: " +
(Httpcontext. Current. Request. Form [ " _ Callbackparam " ] ! = Null ). Tostring ();
Trace. Write (MSG );
}
}
}

Void Context_beginrequest ( Object Sender, eventargs E)
{
If (Httpapplication) sender). Request. filepath. endswith ( " Aspx " ))
{
Stopwatch perfmonwatch = New Stopwatch ();
Httpcontext. Current. items [ " Stopwatch " ] = Perfmonwatch;
Perfmonwatch. Start ();
}
}

# Endregion
}
}

 

 

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.