ViThor.HttpTracing 0.6.0

dotnet add package ViThor.HttpTracing --version 0.6.0                
NuGet\Install-Package ViThor.HttpTracing -Version 0.6.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.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ViThor.HttpTracing --version 0.6.0                
#r "nuget: ViThor.HttpTracing, 0.6.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.6.0

// Install ViThor.HttpTracing as a Cake Tool
#tool nuget:?package=ViThor.HttpTracing&version=0.6.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 received in the HTTP header, this library will generate a random value to pass along to other services (if needed) and track the response with the same ID.

Installation

Install the package from NuGet:

dotnet add package ViThor.HttpTracing

Usage

Update line builder.Services.AddControllers(); in Program.cs to:

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

Add reference to ViThorTraceFilter:

using Vithor.HttpTracing.Filters;

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();
builder.Services.AddHttpClient();

And, your controller needs to extend from ViThorControllerBase:

public class TodoController : ViThorControllerBase
{
    public TodoController(IHttpContextAccessor httpContextAccessor, HttpClient httpClient) : base(httpContextAccessor, httpClient)
    {
    }
}

The ViThorControllerBase will retrieve the X-Correlation-ID and set it in the header of your HttpClient instance.

The CorrelationId property of ViThorControllerBase contains the value of X-Correlation-ID, and can be used to log the TraceID value in any point of your code.

Add Exception Handling Middleware for return the TraceID in the body of the response (optional)

Add the following line in Program.cs:

app.UseMiddleware<ViThorExceptionHandlingMiddleware>();

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 see, in the logs of both applications, the same identifier (Guid), as well as the content of the HTTP request and response.

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 313 1/2/2023
0.5.0 283 12/28/2022
0.4.0 284 12/19/2022
0.3.0 291 12/18/2022
0.2.0 282 12/18/2022
0.1.0 284 12/18/2022