HttpClientLogging 1.0.0

dotnet add package HttpClientLogging --version 1.0.0
NuGet\Install-Package HttpClientLogging -Version 1.0.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="HttpClientLogging" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpClientLogging --version 1.0.0
#r "nuget: HttpClientLogging, 1.0.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.
// Install HttpClientLogging as a Cake Addin
#addin nuget:?package=HttpClientLogging&version=1.0.0

// Install HttpClientLogging as a Cake Tool
#tool nuget:?package=HttpClientLogging&version=1.0.0

HttpClientLogging

Provides LoggingHandler as an implemetation of HttpMessageHandler that logs requests and responses into an ILogger instance.

.NET Core

Nuget Package

.Net Standard 2.1

https://www.nuget.org/packages/HttpClientLogging/

Why

The main motivation behind this package is to provide an easy way to inspect HttpClient traffic. It is especially helpful in the development stage.

Usage

Register with DI

A sample registration with a named HttpClient

var services = new ServiceCollection();
 // register the handler
services.AddTransient<LoggingHandler>();

// affect the handler to the client
services.AddHttpClient("MyClient").AddHttpMessageHandler<LoggingHandler>();

That's all

Customize log level and logger instance

By default, LoggingHandler logs with level LogLevel.Debug.

It is possible to change this behavior using the constructor.

services.AddTransient<LoggingHandler>(srv => new LoggingHandler(ILogger, LogLevel.Information));

Where ILogger is an instance of Microsoft.Extensions.Logging.ILogger

Demo sample

A sample demo project is also available in the sources at : https://github.com/akarzazi/HttpClientLogging/tree/master/demo

using System;
using System.Net.Http;
using System.Threading.Tasks;

using HttpClientLogging;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Demo
{
    public class Program
    {
        async static Task Main(string[] args)
        {
            ServiceProvider serviceProvider = BuildServices();
            var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();

            // named http client
            var namedClient = httpClientFactory.CreateClient("MyGithubClient");
            await namedClient.GetAsync("https://api.github.com/zen");
        }

        private static ServiceProvider BuildServices()
        {
            var services = new ServiceCollection();

            // 1 - customize injected instance
            services.AddTransient<LoggingHandler>(srv => new LoggingHandler(new ConsoleLogger(), LogLevel.Information));

            // 2 - Or use DI with defaults
            // services.AddTransient<LoggingHandler>();

            services.AddHttpClient("MyGithubClient", c =>
            {
                c.DefaultRequestHeaders.Add("User-Agent", "Anonymous-Guy");
            })
            .AddHttpMessageHandler<LoggingHandler>();

            var serviceProvider = services.BuildServiceProvider();
            return serviceProvider;
        }

        public class ConsoleLogger : ILogger
        {
            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                Console.WriteLine(formatter(state, exception));
            }

            public IDisposable BeginScope<TState>(TState state) => throw new NotImplementedException();
            public bool IsEnabled(LogLevel logLevel) => true;
        }
    }
}

Output

{
  "Request": {
    "RequestDateUtc": "2020-04-28T14:12:39.3076773Z",
    "Url": "https://api.github.com/zen",
    "Headers": {
      "User-Agent": [
        "Debug-Sample"
      ]
    },
    "Method": "GET",
    "Body": null
  },
  "Response": {
    "StatusCode": 200,
    "ReasonPhrase": "OK",
    "Body": "Design for failure.",
    "Headers": {
      "Date": [
        "Tue, 28 Apr 2020 14:12:40 GMT"
      ],
      ...
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.0.0 81,410 4/28/2020