DejvidSmth.EfficientHttpClientBodyLogging 0.1.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package DejvidSmth.EfficientHttpClientBodyLogging --version 0.1.0
                    
NuGet\Install-Package DejvidSmth.EfficientHttpClientBodyLogging -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DejvidSmth.EfficientHttpClientBodyLogging" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DejvidSmth.EfficientHttpClientBodyLogging" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DejvidSmth.EfficientHttpClientBodyLogging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DejvidSmth.EfficientHttpClientBodyLogging --version 0.1.0
                    
#r "nuget: DejvidSmth.EfficientHttpClientBodyLogging, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DejvidSmth.EfficientHttpClientBodyLogging@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DejvidSmth.EfficientHttpClientBodyLogging&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DejvidSmth.EfficientHttpClientBodyLogging&version=0.1.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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