https://github.com/nventive/httptracing
Complete tracing of requests / responses for HttpClient
https://github.com/nventive/httptracing
application-insights csharp httpclient httpclientfactory logging netstandard netstandard20
Last synced: 4 months ago
JSON representation
Complete tracing of requests / responses for HttpClient
- Host: GitHub
- URL: https://github.com/nventive/httptracing
- Owner: nventive
- License: apache-2.0
- Created: 2019-09-24T13:07:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-06T13:32:40.000Z (almost 4 years ago)
- Last Synced: 2025-01-06T13:45:41.093Z (about 1 year ago)
- Topics: application-insights, csharp, httpclient, httpclientfactory, logging, netstandard, netstandard20
- Language: C#
- Size: 40 KB
- Stars: 2
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# HttpTracing
Complete tracing of requests / responses for `HttpClient`
[](LICENSE)
[](https://dev.azure.com/nventive-public/nventive/_build/latest?definitionId=9&branchName=master)

## Getting Started
Install the package:
```
Install-Package HttpTracing
```
## Features
When configuring the `HttpClient` via the factory, add the tracing handler:
```csharp
using Microsoft.Extensions.DependencyInjection;
services
.AddHttpClient() // Adds a named HttpClient
.AddHttpTracing(); // Attaches a tracing handler.
```
The tracing handler (`AddHttpTracing`) should probably be the last handler in the
chain in order to capture all modifications done by other handlers if they exist.
The logger category [follows the conventions defined by the `IHttpClientFactory`](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#logging)
by naming the category `System.Net.Http.HttpClient.{HttpClient.Name}.TraceHandler`
(e.g. `System.Net.Http.HttpClient.MyNamedClient.TraceHandler`).
Event ids used for the various messages:
| Event id | Event Name | Log level | Description |
|----------|--------------------|-----------|-------------------------------------|
| 200 | RequestSuccessful | Trace | Request trace on successful calls |
| 201 | RequestError | Warning | Request trace on unsuccessful calls |
| 210 | ResponseSuccessful | Trace | Response on successful calls |
| 211 | ResponseError | Warning | Response on unsuccessful calls |
A successful call is determined by default using `HttpResponseMessage.IsSuccessStatusCode`.
This can be customized when adding the handler:
```csharp
services
.AddHttpClient()
.AddHttpTracing(
isResponseSuccessful: response => response.StatusCode >= HttpStatusCode.InternalServerError);
```
The logger category name can also be customized if needed:
```csharp
services
.AddHttpClient()
.AddHttpTracing(categoryName: "Foo.Bar");
```
### Using with Application Insights
By default, Application Insights captures only `Warning` and `Error` log levels.
To enable tracing of successful requests and responses, [configure the log level for Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger).
Example within the `appsettings.json` file:
```json
{
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"System.Net.Http.HttpClient.MyNamedClient.TraceHandler": "Trace"
}
},
}
}
```
### Buffering the requests content
Some frameworks uses `HttpContent` streams on `HttpRequestMessage` that cannot be replayed, thus preventing the component
to read the body of the request in such cases.
The default behavior is to write the following line in lieu of the body:
```
[InvalidOperationException: Request content is not buffered (...). Use the bufferRequests parameter to allow the reading.]
```
If you want to see the request content in such cases, use the `bufferRequests` parameter:
```csharp
services
.AddHttpClient()
.AddHttpTracing(bufferRequests: true);
```
### Adding globally
It is possible to add tracing to **all** `HttpClient` registered through the `IHttpClientFactory`:
```csharp
services.AddHttpTracingToAllHttpClients();
```
This way, the tracing handler is added to all instances globally.
To customize the parameters, use the factory configuration method:
```csharp
services.AddHttpTracingToAllHttpClients((sp, builder) =>
{
return builder.Name switch
{
nameof(BufferedClient) => new HttpMessageHandlerTracingConfiguration { BufferRequests = true },
nameof(DisabledClient) => new HttpMessageHandlerTracingConfiguration { Enabled = false },
_ => null, // Default configuration
};
})
```
## Changelog
Please consult the [CHANGELOG](CHANGELOG.md) for more information about version
history.
## License
This project is licensed under the Apache 2.0 license - see the
[LICENSE](LICENSE) file for details.
## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for
contributing to this project.
Be mindful of our [Code of Conduct](CODE_OF_CONDUCT.md).