/HttpTracing

Complete tracing of requests / responses for HttpClient

Primary LanguageC#Apache License 2.0Apache-2.0

HttpTracing

Complete tracing of requests / responses for HttpClient

License Build Status Nuget

Getting Started

Install the package:

Install-Package HttpTracing

Features

When configuring the HttpClient via the factory, add the tracing handler:

using Microsoft.Extensions.DependencyInjection;


services
    .AddHttpClient<MyNamedClient>() // Adds a named HttpClient
    .AddHttpTracing(); // Attaches a tracing handler.

The tracing handler (AddHttpTracing) should probably be the last handler in the chain in order to capture all modifications done by other handlers if they exist.

The logger category follows the conventions defined by the IHttpClientFactory by naming the category System.Net.Http.HttpClient.{HttpClient.Name}.TraceHandler (e.g. System.Net.Http.HttpClient.MyNamedClient.TraceHandler).

Event ids used for the various messages:

Event id Event Name Log level Description
200 RequestSuccessful Trace Request trace on successful calls
201 RequestError Warning Request trace on unsuccessful calls
210 ResponseSuccessful Trace Response on successful calls
211 ResponseError Warning Response on unsuccessful calls

A successful call is determined by default using HttpResponseMessage.IsSuccessStatusCode. This can be customized when adding the handler:

services
    .AddHttpClient<MyNamedClient>()
    .AddHttpTracing(
        isResponseSuccessful: response => response.StatusCode >= HttpStatusCode.InternalServerError);

The logger category name can also be customized if needed:

services
    .AddHttpClient<MyNamedClient>()
    .AddHttpTracing(categoryName: "Foo.Bar");

Using with Application Insights

By default, Application Insights captures only Warning and Error log levels. To enable tracing of successful requests and responses, configure the log level for Application Insights. Example within the appsettings.json file:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "System.Net.Http.HttpClient.MyNamedClient.TraceHandler": "Trace"
      }
    },
  }
}

Buffering the requests content

Some frameworks uses HttpContent streams on HttpRequestMessage that cannot be replayed, thus preventing the component to read the body of the request in such cases.

The default behavior is to write the following line in lieu of the body:

[InvalidOperationException: Request content is not buffered (...). Use the bufferRequests parameter to allow the reading.]

If you want to see the request content in such cases, use the bufferRequests parameter:

services
    .AddHttpClient<MyNamedClient>()
    .AddHttpTracing(bufferRequests: true);

Adding globally

It is possible to add tracing to all HttpClient registered through the IHttpClientFactory:

services.AddHttpTracingToAllHttpClients();

This way, the tracing handler is added to all instances globally. To customize the parameters, use the factory configuration method:

services.AddHttpTracingToAllHttpClients((sp, builder) =>
{
    return builder.Name switch
    {
        nameof(BufferedClient) => new HttpMessageHandlerTracingConfiguration { BufferRequests = true },
        nameof(DisabledClient) => new HttpMessageHandlerTracingConfiguration { Enabled = false },
        _ => null, // Default configuration
    };
})

Changelog

Please consult the CHANGELOG for more information about version history.

License

This project is licensed under the Apache 2.0 license - see the LICENSE file for details.

Contributing

Please read CONTRIBUTING.md for details on the process for contributing to this project.

Be mindful of our Code of Conduct.