Web Request Execution Time | C# example

In this article, you find a simple example of how to create a custom message handler for a Web request execution time logging.

Sometimes, to optimize the performance, you may need to find out the Web request execution time. For example, you want to know how much time is taken to read a file in the file system or download data from a remote server.

Message Handler for Logging the Time of File Loading

Creating a TimeLoggerMessageHandler for a Web Request Execution Time Logging

The following code snippet shows how to create a TimeLoggerMessageHandler to log the time taken for reading a file from the file system.

 1using System;
 2using System.Diagnostics;
 3using Aspose.Html;
 4using Aspose.Html.Net;
 5...	
 6
 7	// Define the TimeLoggerMessageHandler class that is derived from the MessageHandler class
 8	public class TimeLoggerMessageHandler : MessageHandler
 9	{
10	    // Override the Invoke() method
11		public override void Invoke(INetworkOperationContext context)
12	    {
13	        // Start the stopwatch
14			var stopwatch = Stopwatch.StartNew();
15			
16	        // Invoke the next message handler in the chain
17			Next(context);
18			
19	        // Stop the stopwatch
20			stopwatch.Stop();
21			
22	        // Print the result
23			Console.WriteLine("Request: " + context.Request.RequestUri);
24	        Console.WriteLine("Time: " + stopwatch.ElapsedMilliseconds + "ms");
25	    }
26	}

Use the Stopwatch class in the System.Diagnostics Namespace to accurately measure the time it takes to execute the Web request. The StartNew() method starts to log time for completing the Web request until the Stop() method is called.

Adding the TimeLoggerMessageHandler to the Pipeline

The Configuration() constructor initializes an instance of the Configuration class. After the configuration is created, the GetService <INetworkService>() and MessageHandlers.Insert() methods are invoked. The Insert() method adds the TimeLoggerMessageHandler at the first place in the collection of message handlers.

Use the HTMLDocument(address, configuration) constructor to initialize an HTML Document and implement the example for logging the document’s loading time.

 1using Aspose.Html;
 2using Aspose.Html.Net;
 3using Aspose.Html.Services;
 4...
 5
 6	// Create an instance of the Configuration class
 7	using var configuration = new Configuration();
 8
 9	// Add the TimeLoggerMessageHandler to the chain of existing message handlers
10	var service = configuration.GetService<INetworkService>();
11	var handlers = service.MessageHandlers;
12
13	handlers.Insert(0, new TimeLoggerMessageHandler());
14
15	// Prepare path to a source document file
16	string documentPath = Path.Combine(DataDir, "input.htm");
17
18	// Initialize an HTML document with specified configuration
19	using var document = new HTMLDocument(documentPath, configuration);

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.