5.21Enabling the Request Profiler
Modern browsers typically have built-in developer tools that can capture and visually present all request and response data when users visit websites. Similarly, we provide a set of profiling tools for the HTTP remote request module.
HTTP Declarative Requests enable the request profiler through the ProfilerAttribute attribute. The corresponding HTTP declarative extractor is implemented as the ProfilerDeclarativeExtractor type, which is responsible for parsing the ProfilerAttribute attribute and building the request-profiler-enabling configuration required for an HttpRequestBuilder instance.
// Applied at the interface level, affects all methods[Profiler]public interface IHttpService : IHttpDeclarative{ // Applied at the method level [Profiler] [Get("https://furion.net/")] Task<string> GetStringAsync(); [Profiler(false)] // Disable the request profiler [Get("https://furion.net/")] Task<string> GetStringAsync();}Once enabled, when an HTTP remote request is executed, the console outputs the following detailed information:
General: Request URL: https://furion.net/ Request Method: GET Status Code: 200 OK HTTP Version: 1.1 HTTP Content: Content Type: Declarative: System.Threading.Tasks.Task<System.String> GetStringAsync() | HttpAgent.Samples.IHttpService HttpClient Name: Request Duration (ms): 24.00Response Headers: Server: nginx/1.22.1 Date: Sun, 24 Nov 2024 16:48:50 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "67426a3f-f366" Cache-Control: max-age=315360000 Accept-Ranges: bytes Content-Type: text/html Content-Length: 62310 Last-Modified: Sat, 23 Nov 2024 23:50:23 GMT Expires: Thu, 31 Dec 2037 23:55:55 GMTIn addition to enabling the profiler for individual requests, you can also register it globally to enable it in HttpClient:
// Enable for the default clientservices.AddHttpClient(string.Empty) .AddProfilerDelegatingHandler();// You can also provide conditional disabling, e.g., disable in productionservices.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 in one stepservices.ConfigureHttpClientDefaults(clientBuilder => clientBuilder.AddProfilerDelegatingHandler());// Or use the IHttpRemoteBuilder extension method for one-step configurationservices.AddHttpRemote() .ConfigureHttpClientDefaults(clientBuilder => clientBuilder.AddProfilerDelegatingHandler());By enabling the request profiler, developers can observe and debug HTTP requests more intuitively and conveniently, thereby improving development efficiency and debugging accuracy.
ProfilerAttribute contains the following constructors and properties:
-
Constructors:
new(): Applies to methods or interfaces and enables the request profiler.new(enabled): Applies to methods or interfaces and sets whether to enable the request profiler.
-
Properties:
Enabled: Whether to enable (typebool), defaulting totrue(enabled).