Skip to content

vamcan/HttpClientToCurlLogger

Repository files navigation

HttpClientToCurlLogger

NuGet License: MIT

A lightweight .NET library that automatically logs all HttpClient requests as cURL commands. Perfect for debugging, testing, and sharing API requests during development.

Features

Automatic Logging - Captures all HttpClient requests automatically
cURL Format - Generates ready-to-use cURL commands
Easy Integration - Single line of configuration
Toggle On/Off - Enable or disable logging via configuration
Multiple Clients - Works with all registered HttpClient instances
Headers & Body - Includes headers and request body in the cURL output
.NET Standard 2.0 - Compatible with .NET Core 2.0+ and .NET Framework 4.6.1+

Installation

Install via NuGet Package Manager:

dotnet add package HttpClientToCurlLogger

Or via Package Manager Console:

Install-Package HttpClientToCurlLogger

Usage

Basic Setup

Add the following to your Startup.cs or Program.cs:

using HttpClientToCurlLogger;

public void ConfigureServices(IServiceCollection services)
{
    // Add cURL logging
    services.AddCurlLogging(options =>
    {
        options.EnableLogging = true; // Set to false to disable
    });

    // Register your HttpClient(s)
    services.AddHttpClient("MyClient");
    services.AddHttpClient<IMyService, MyService>();
}

ASP.NET Core Minimal API (.NET 6+)

using HttpClientToCurlLogger;

var builder = WebApplication.CreateBuilder(args);

// Add cURL logging
builder.Services.AddCurlLogging(options =>
{
    options.EnableLogging = true;
});

// Register HttpClient
builder.Services.AddHttpClient("MyClient");

var app = builder.Build();
app.Run();

Example Output

When you make an HTTP request:

var client = httpClientFactory.CreateClient("MyClient");
await client.PostAsJsonAsync("https://api.example.com/users", new { name = "John" });

You'll see in your logs:

curl -X POST -H "Content-Type: application/json; charset=utf-8" -d "{\"name\":\"John\"}" "https://api.example.com/users"

You can copy this cURL command directly to your terminal or Postman!

The output will look like this:

================================================================================
cURL Command (copy-paste ready):
================================================================================
curl -X POST \
  -H "ApiKey: your-api-key" \
  -H "Content-Type: application/json; charset=utf-8" \
  -d "{\"name\":\"John\"}" \
  "https://api.example.com/users"
================================================================================

Configuration Options

Property Type Default Description
EnableLogging bool false Enable or disable cURL logging
UseMultiLineFormat bool true Format curl command with line breaks for better readability
UseFormattedOutput bool true Add separators and headers around the curl command
UseConsoleOutput bool false Write directly to Console instead of ILogger (recommended for structured logging)

Advanced Configuration

services.AddCurlLogging(options =>
{
    options.EnableLogging = true;
    options.UseMultiLineFormat = true;   // Multi-line format with \ continuation
    options.UseFormattedOutput = true;   // With separators and headers
    options.UseConsoleOutput = false;    // Use ILogger (default)
});

For Structured Logging (Serilog, NLog with JSON formatters):

If you're using structured logging that outputs JSON (like Serilog with JSON formatter or Elastic.CommonSchema.Serilog), enable UseConsoleOutput to bypass the logger and write directly to console for readable output:

services.AddCurlLogging(options =>
{
    options.EnableLogging = true;
    options.UseMultiLineFormat = true;   // Multi-line format
    options.UseFormattedOutput = true;   // With separators
    options.UseConsoleOutput = true;     // Direct console output (bypasses structured logging)
});

For compact single-line output (useful for simple logging):

services.AddCurlLogging(options =>
{
    options.EnableLogging = true;
    options.UseMultiLineFormat = false;  // Single line
    options.UseFormattedOutput = false;  // No separators
});

Environment-Based Configuration

You can control logging based on environment:

services.AddCurlLogging(options =>
{
    options.EnableLogging = builder.Environment.IsDevelopment();
});

Or use configuration files (appsettings.json):

{
  "CurlLogging": {
    "EnableLogging": true,
    "UseMultiLineFormat": true,
    "UseFormattedOutput": true,
    "UseConsoleOutput": true
  }
}
services.AddCurlLogging(options =>
{
    configuration.GetSection("CurlLogging").Bind(options);
});

How It Works

The library uses an IHttpMessageHandlerBuilderFilter to automatically inject a DelegatingHandler into all registered HttpClient instances. This handler intercepts requests and logs them as cURL commands using the configured logger (typically ILogger<T>).

Requirements

  • .NET Standard 2.0 or higher
  • Microsoft.Extensions.Http 2.1.0+
  • Microsoft.Extensions.Logging.Abstractions 2.1.0+

Supported Frameworks

  • .NET Core 2.0+
  • .NET 5, 6, 7, 8+
  • .NET Framework 4.6.1+

Use Cases

  • Debugging - Quickly see what requests your application is making
  • Testing - Copy cURL commands to test APIs manually
  • Documentation - Share API request examples with your team
  • Troubleshooting - Diagnose integration issues with external APIs

Best Practices

  1. Disable in Production - Only enable logging in development/staging environments
  2. Sensitive Data - Be aware that headers and body content are logged (including auth tokens)
  3. Performance - Minimal overhead, but consider disabling for high-throughput scenarios

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Reza Ghasemi

Repository

https://github.com/vamcan/HttpClientToCurlLogger

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages