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
}
}