2.7Request Analysis Tool
Modern browsers typically include built-in developer tools that can capture and visually display all request and response data when users visit a website. Similarly, we also provide a set of analysis tools for the HTTP remote request module.
How to Enable
The following is an example of how to enable the request analysis tool:
// Builder approachawait httpRemoteService.SendAsync(HttpRequestBuilder.Get("https://furion.net") .WithHeader("X-Header", "custom") .Profiler()); // Enable the request analysis tool, or use Debugger()// HTTP request verb approachawait httpRemoteService.GetAsync("https://furion.net" , builder => builder.Profiler()); // Enable the request analysis tool, or use Debugger()// You can also obtain the analysis data from the request analysis toolawait httpRemoteService.GetAsync("https://furion.net" , builder => builder.Profiler(analyzer => { Console.WriteLine(analyzer.Data); }));Once enabled, when executing an HTTP remote request, the console outputs the following detailed information:
Request Headers: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0 X-Header: customGeneral: Request URL: https://furion.net/ Request Method: GET Status Code: 200 OK HTTP Version: 1.1 HTTP Content: Content Type: HttpClient Name: Request Duration (ms): 149.00Response Headers: Server: nginx/1.22.1 Date: Thu, 14 Nov 2024 15:35:41 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "67091697-f32f" Cache-Control: max-age=315360000 Accept-Ranges: bytes Content-Type: text/html Content-Length: 62255 Last-Modified: Fri, 11 Oct 2024 12:14:15 GMT Expires: Thu, 31 Dec 2037 23:55:55 GMTGlobal Enablement and Advanced Configuration
In addition to enabling the profiler for a single request, you can also register it globally to enable it within HttpClient:
// Enable for the default clientservices.AddHttpClient(string.Empty) .AddProfilerDelegatingHandler();// You can also provide a conditional disable, for example disable in production environmentsservices.AddHttpClient(string.Empty) .AddProfilerDelegatingHandler(disableIn: () => builder.Environment.EnvironmentName == "Production");services.AddHttpClient(string.Empty) .AddProfilerDelegatingHandler(disableInProduction: true);// Enable for a specific client//services.AddHttpClient("weixin")// .AddProfilerDelegatingHandler();// You can also enable it for all client configurations with one clickservices.ConfigureHttpClientDefaults(clientBuilder => clientBuilder.AddProfilerDelegatingHandler());// Or use the IHttpRemoteBuilder extension method for one-click configurationservices.AddHttpRemote() .ConfigureHttpClientDefaults(clientBuilder => clientBuilder.AddProfilerDelegatingHandler());Usage in Declarative Requests
At the same time, HTTP Declarative Requests also support enabling the request profiler via the [Profiler] attribute:
[Profiler] // Enable the request profiler for all methods within the interfacepublic interface IHttpService : IHttpDeclarative{ [Get("https://furion.net")] Task<string> ProfilerMethod(); [Profiler(false)] // Disable the request profiler for this method [Get("https://furion.net")] Task<string> NonProfilerMethod();}public interface IHttpService : IHttpDeclarative{ [Get("https://furion.net")] Task<string> NonProfilerMethod(); [Profiler] // Enable the request profiler for this method [Get("https://furion.net")] Task<string> ProfilerMethod();}By enabling the request profiler, developers can observe and debug HTTP requests more intuitively and conveniently, thereby improving development efficiency and debugging accuracy.
Custom Log Output Target
By default, the request profiler's information is output to the console via Console.WriteLine. However, in environments such as WinForms, WPF, or MAUI, console output may be invisible or unsupported. In this case, you can redirect the logs to other targets by configuring FallbackLogger, for example to debug output (System.Diagnostics.Debug.WriteLine):
services.AddHttpRemote(builder => {}) .ConfigureOptions(options => { // Fallback log output delegate used when the logging service or console output is unavailable options.FallbackLogger = message => System.Diagnostics.Debug.WriteLine(message); });In this way, the profiling information will be displayed in the "Output" window (debug view) of Visual Studio or other locations supported by the debugger.