HttpLoggingHandler Class

Represents an HTTP handler that logs HTTP request and response messages.

Syntax

public class HttpLoggingHandler : DelegatingHandler

Remarks

The HttpLoggingHandler class is a type of DelegatingHandler that intercepts outgoing HTTP requests and incoming HTTP responses. It allows you to log the details of these messages, which can be invaluable for debugging, monitoring, and understanding network traffic.

This handler is particularly useful for scenarios where you need to inspect the headers, content, and other properties of HTTP messages without modifying the core request/response processing logic. You can configure the logging behavior by overriding the LogRequest and LogResponse methods.

To use HttpLoggingHandler, you typically chain it with other HttpClientHandler instances within an HttpClient's message handler pipeline.

Constructors

Methods

Example

Here's a basic example of how to use HttpLoggingHandler:


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

public class CustomLoggingHandler : HttpLoggingHandler
{
    protected override void LogRequest(HttpRequestMessage request)
    {
        Console.WriteLine($"--- Request ---");
        Console.WriteLine($"Method: {request.Method}");
        Console.WriteLine($"URI: {request.RequestUri}");
        if (request.Headers.Count > 0)
        {
            Console.WriteLine($"Headers:");
            foreach (var header in request.Headers)
            {
                Console.WriteLine($"  {header.Key}: {string.Join(", ", header.Value)}");
            }
        }
        Console.WriteLine("---------------");
    }

    protected override void LogResponse(HttpResponseMessage response)
    {
        Console.WriteLine($"--- Response ---");
        Console.WriteLine($"Status Code: {(int)response.StatusCode} {response.ReasonPhrase}");
        if (response.Headers.Count > 0)
        {
            Console.WriteLine($"Headers:");
            foreach (var header in response.Headers)
            {
                Console.WriteLine($"  {header.Key}: {string.Join(", ", header.Value)}");
            }
        }
        Console.WriteLine("----------------");
    }
}

public class Example
{
    public static async Task Main(string[] args)
    {
        var loggingHandler = new CustomLoggingHandler();
        var httpClientHandler = new HttpClientHandler();
        loggingHandler.InnerHandler = httpClientHandler; // Chain the handlers

        using (var client = new HttpClient(loggingHandler))
        {
            try
            {
                var response = await client.GetAsync("https://www.example.com");
                response.EnsureSuccessStatusCode(); // Throw if HTTP status is an error
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Successfully retrieved content.");
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }
        }
    }
}
        

See Also