DejvidSmth.EfficientHttpClientBodyLogging
0.1.0
dotnet add package DejvidSmth.EfficientHttpClientBodyLogging --version 0.1.0
NuGet\Install-Package DejvidSmth.EfficientHttpClientBodyLogging -Version 0.1.0
<PackageReference Include="DejvidSmth.EfficientHttpClientBodyLogging" Version="0.1.0" />
<PackageVersion Include="DejvidSmth.EfficientHttpClientBodyLogging" Version="0.1.0" />
<PackageReference Include="DejvidSmth.EfficientHttpClientBodyLogging" />
paket add DejvidSmth.EfficientHttpClientBodyLogging --version 0.1.0
#r "nuget: DejvidSmth.EfficientHttpClientBodyLogging, 0.1.0"
#:package DejvidSmth.EfficientHttpClientBodyLogging@0.1.0
#addin nuget:?package=DejvidSmth.EfficientHttpClientBodyLogging&version=0.1.0
#tool nuget:?package=DejvidSmth.EfficientHttpClientBodyLogging&version=0.1.0
Efficient Request/Response body logging with C#'s HttpClient
This is an example of memory-efficient implementation of req/res body logging for C#'s HttpClient.
- Configure max size of req/resp body that will be read as a string and logged
- Use constant-sized linked list of pooled array segments to store bytes while req/resp is being processed
This is heavily inspired by existing implementation of req/res body logging used in
Microsoft.AspNetCore.HttpLogging.
Implementation details
Compared to other examples on the internet this implementation doesn't try to read request or response
body outside of standard req/res processing pipeline. If we wanted to first read the body from arbitrary
HttpContent we would have to read the entire content into memory which is inefficient, especially if
we only need part of the content.
Instead this implementation provides the LoggingStream, and we stream req/res body through that. As
the request is processed LoggingStream captures slice of the processed bytes up to configured limit
and eventually converts that to a string efficiently. Only then we log the captured string.
Benchmarks
WIP
Logging disabled:
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|---|
| Baseline_5 | 52.58 ns | 0.851 ns | 0.755 ns | 0.0312 | - | 392 B |
| Baseline_1024 | 62.69 ns | 0.294 ns | 0.261 ns | 0.0312 | - | 392 B |
| Baseline_10000 | 113.90 ns | 0.372 ns | 0.330 ns | 0.0312 | - | 392 B |
| Wrapped_5 | 806.35 ns | 4.024 ns | 3.567 ns | 0.1926 | 0.0010 | 2424 B |
| Wrapped_1024 | 886.58 ns | 2.227 ns | 1.738 ns | 0.2537 | 0.0010 | 3192 B |
| Wrapped_10000 | 1,731.81 ns | 9.764 ns | 9.133 ns | 0.9842 | 0.0381 | 12344 B |
Logging enabled:
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|---|
| Baseline_5 | 53.76 ns | 1.014 ns | 0.899 ns | 0.0312 | - | 392 B |
| Baseline_1024 | 62.13 ns | 0.385 ns | 0.360 ns | 0.0312 | - | 392 B |
| Baseline_10000 | 111.99 ns | 0.747 ns | 0.698 ns | 0.0312 | - | 392 B |
| Wrapped_5 | 882.62 ns | 7.012 ns | 6.559 ns | 0.2451 | 0.0010 | 3080 B |
| Wrapped_1024 | 1,117.03 ns | 12.310 ns | 10.913 ns | 0.5913 | 0.0019 | 7424 B |
| Wrapped_10000 | 4,961.28 ns | 37.148 ns | 32.931 ns | 7.1411 | 1.1826 | 89872 B |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- Microsoft.Net.Http.Headers (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|