11# Microsoft.Extensions.Http.Diagnostics
22
3- Telemetry support for HTTP Client .
3+ Telemetry support for ` HttpClient ` that allows tracking latency and enriching and redacting log output .
44
55## Install the package
66
@@ -18,6 +18,102 @@ Or directly in the C# project file:
1818</ItemGroup >
1919```
2020
21+ ## Usage Example
22+
23+ ### HTTP Client Logs Enrichment and Redaction
24+
25+ These components enable enriching and redacting ` HttpClient ` request logs. They remove built-it HTTP Client logging.
26+
27+ In order to use the redaction feature, you need to reference the ` Microsoft.Extensions.Compliance.Redaction ` package.
28+
29+ The services can be registered using the following methods:
30+
31+ ``` csharp
32+ public static IServiceCollection AddExtendedHttpClientLogging (this IServiceCollection services );
33+ public static IServiceCollection AddExtendedHttpClientLogging (this IServiceCollection services , IConfigurationSection section );
34+ public static IServiceCollection AddExtendedHttpClientLogging (this IServiceCollection services , Action < LoggingOptions > configure );
35+ public static IServiceCollection AddHttpClientLogEnricher <T >(this IServiceCollection services ) where T : class, IHttpClientLogEnricher;
36+ ```
37+
38+ For example:
39+
40+ ``` csharp
41+ var builder = Host .CreateApplicationBuilder (args );
42+
43+ // Register IHttpClientFactory:
44+ builder .Services .AddHttpClient ();
45+
46+ // Register redaction services:
47+ builder .Services .AddRedaction ();
48+
49+ // Register HttpClient logging enrichment & redaction services:
50+ builder .Services .AddExtendedHttpClientLogging ();
51+
52+ // Register a logging enricher (the type should implement IHttpClientLogEnricher):
53+ builder .Services .AddHttpClientLogEnricher <MyHttpClientLogEnricher >();
54+
55+ var host = builder .Build ();
56+ ```
57+
58+ You can also use the following extension methods to apply the logging to the specific ` IHttpClientBuilder ` :
59+
60+ ``` csharp
61+ public static IHttpClientBuilder AddExtendedHttpClientLogging (this IHttpClientBuilder builder );
62+ public static IHttpClientBuilder AddExtendedHttpClientLogging (this IHttpClientBuilder builder , IConfigurationSection section );
63+ public static IHttpClientBuilder AddExtendedHttpClientLogging (this IHttpClientBuilder builder , Action < LoggingOptions > configure );
64+ ```
65+
66+ For example:
67+
68+ ``` csharp
69+ var builder = Host .CreateApplicationBuilder (args );
70+
71+ // Register redaction services:
72+ builder .Services .AddRedaction ();
73+
74+ // Register named HttpClient:
75+ var httpClientBuilder = builder .Services .AddHttpClient (" MyNamedClient" );
76+
77+ // Configure named HttpClient to use logging enrichment & redaction:
78+ httpClientBuilder .AddExtendedHttpClientLogging ();
79+
80+ var host = builder .Build ();
81+ ```
82+
83+ ### Tracking HTTP Request Client Latency
84+
85+ These components enable tracking and reporting the latency of HTTP Client request processing.
86+
87+ The services can be registered using the following methods:
88+
89+ ``` csharp
90+ public static IServiceCollection AddHttpClientLatencyTelemetry (this IServiceCollection services );
91+ public static IServiceCollection AddHttpClientLatencyTelemetry (this IServiceCollection services , IConfigurationSection section );
92+ public static IServiceCollection AddHttpClientLatencyTelemetry (this IServiceCollection services , Action < HttpClientLatencyTelemetryOptions > configure );
93+ ```
94+
95+ For example:
96+
97+ ``` csharp
98+ var builder = Host .CreateApplicationBuilder (args );
99+
100+ // Register IHttpClientFactory:
101+ builder .Services .AddHttpClient ();
102+
103+ // Register redaction services:
104+ builder .Services .AddRedaction ();
105+
106+ // Register latency context services:
107+ builder .Services .AddLatencyContext ();
108+
109+ // Register HttpClient logging enrichment & redaction services:
110+ builder .Services .AddExtendedHttpClientLogging ();
111+
112+ // Register HttpClient latency telemetry services:
113+ builder .Services .AddHttpClientLatencyTelemetry ();
114+
115+ var host = builder .Build ();
116+ ```
21117
22118## Feedback & Contributing
23119
0 commit comments