ViThor.HttpTracing 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ViThor.HttpTracing --version 0.2.0                
NuGet\Install-Package ViThor.HttpTracing -Version 0.2.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="ViThor.HttpTracing" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ViThor.HttpTracing --version 0.2.0                
#r "nuget: ViThor.HttpTracing, 0.2.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 ViThor.HttpTracing as a Cake Addin
#addin nuget:?package=ViThor.HttpTracing&version=0.2.0

// Install ViThor.HttpTracing as a Cake Tool
#tool nuget:?package=ViThor.HttpTracing&version=0.2.0                

ViThor HTTP Tracing

A small package for tracing HTTP requests between dotnet microservices.

Sample of the log:

info: Vithor.Filters.LoggingFilter[0]
      [ced6398d-36a2-4f0c-80c2-c78e91274d0d] Request from [Username/Anonymous] to Controller/Action with query parameters: [] and arguments: {
        "id": 5
      }
info: Vithor.Filters.LoggingFilter[0]
      [ced6398d-36a2-4f0c-80c2-c78e91274d0d] Response from [Username/Anonymous] to Controller/Action with result: {
        "id": 5,
        "name": "Test",
        "isComplete": true
      }

[ced6398d-36a2-4f0c-80c2-c78e91274d0d] is a tracking code that can be received in the header with the term X-Correlation-ID.

If an X-Correlation-ID is not passed then this library will generate a random value to track request and response.

Installation

Install the package from NuGet:

dotnet add package ViThor.HttpTracing

Usage

Add in Program.cs:

using Vithor.HttpTracing.Filters;

builder.Services.AddControllers(options =>
{
    options.Filters.Add<ViThorTraceFilter>();
});

The above filter will retrieve the TraceID , log the request and the response.

If your API has to pass the TraceID to another service, you also need to add the following line in Program.cs:

builder.Services.AddHttpContextAccessor();

And, your controller needs to extend from ViThorControllerBase:

public class TodoController : ViThorControllerBase 
{
    private readonly HttpClient _httpClient;

    public TodoController(HttpClient httpClient, IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {
        _httpClient = httpClient;
        _httpClient.DefaultRequestHeaders.Add("X-Correlation-ID", this.CorrelationId);
    }
}

Sample

Running the sample

dotnet run --project Samples/ViThor.HttpTracing.Sample.AppOne/ViThor.HttpTracing.Sample.AppOne.csproj
dotnet run --project Samples/ViThor.HttpTracing.Sample.AppTwo/ViThor.HttpTracing.Sample.AppTwo.csproj

Testing the sample

Swagger

The sample project has Swagger configured. To access it, run the API and access the following URL:

http://localhost:5000/swagger/index.html
Curl

Request without passing the X-Correlation-ID:

curl --location --request GET 'http://localhost:5000/Todo'

Request passing the X-Correlation-ID:

curl --location --request GET 'http://localhost:5000/Todo' \
--header 'X-Correlation-ID: ced6398d-36a2-4f0c-80c2-c78e91274d0d'

You will be able to view the content of the HTTP request and response in both applications, with a unique TraceID.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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
0.6.0 317 1/2/2023
0.5.0 287 12/28/2022
0.4.0 290 12/19/2022
0.3.0 295 12/18/2022
0.2.0 286 12/18/2022
0.1.0 288 12/18/2022