Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To measure web request execution time in C#, create a custom TimeLoggerMessageHandler, use Stopwatch inside the handler, insert it into INetworkService.MessageHandlers, and open the HTML document with that configuration.
Performance diagnostics often require knowing how long a request takes to pass through the HTML loading pipeline. For example, you may need to measure the time required to read a file from the file system or download data from a remote server before deciding whether the workflow needs caching, timeout handling, or request filtering.
The following code snippet shows how to create a TimeLoggerMessageHandler to log the time taken for reading a file from the file system.
1// Log request time with custom C# message handler
2
3// Define the TimeLoggerMessageHandler class that is derived from the MessageHandler class
4public class TimeLoggerMessageHandler : MessageHandler
5{
6 // Override the Invoke() method
7 public override void Invoke(INetworkOperationContext context)
8 {
9 // Start the stopwatch
10 Stopwatch stopwatch = Stopwatch.StartNew();
11
12 // Invoke the next message handler in the chain
13 Next(context);
14
15 // Stop the stopwatch
16 stopwatch.Stop();
17
18 // Print the result
19 Debug.WriteLine("Request: " + context.Request.RequestUri);
20 Debug.WriteLine("Time: " + stopwatch.ElapsedMilliseconds + "ms");
21 }
22}Use the Stopwatch class in the System.Diagnostics namespace to measure how long the web request takes. The StartNew() method starts the timer, and Stop() ends measurement after the request has completed.
After the handler is implemented, add it to the network pipeline:
INetworkService instance from the configuration.MessageHandlers.Insert() to place TimeLoggerMessageHandler first in the handler collection.The Insert() method adds TimeLoggerMessageHandler at the first position in the collection, so the request is measured before later handlers continue processing.
1// Track HTML load time in C# using a custom network message handler
2
3// Create an instance of the Configuration class
4using Configuration configuration = new Configuration();
5
6// Add the TimeLoggerMessageHandler to the chain of existing message handlers
7INetworkService service = configuration.GetService<INetworkService>();
8MessageHandlerCollection handlers = service.MessageHandlers;
9
10handlers.Insert(0, new TimeLoggerMessageHandler());
11
12// Prepare path to a source document file
13string documentPath = Path.Combine(DataDir, "input.htm");
14
15// Initialize an HTML document with specified configuration
16using HTMLDocument document = new HTMLDocument(documentPath, configuration);You can download the complete examples and data files from GitHub.
It measures how long a web request takes while an HTML document is opened with the configured message handler pipeline.
Placing the logger first lets it measure the request before later handlers or default processing continue the pipeline.
Yes. Use a time logger for diagnostics and a timeout handler when slow or unavailable resources should be interrupted.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.