Elwark.CorrelationId 1.0.2

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

// Install Elwark.CorrelationId as a Cake Tool
#tool nuget:?package=Elwark.CorrelationId&version=1.0.2                

Elwark Correlation Id

A Correlation Id is a tool used in distributed systems to trace requests across multiple services. This specific library offers a lightweight approach to implementing Correlation Ids. When enabled, it checks for the presence of a Correlation Id in request headers and attaches it to the Correlation Context, which can then be used for logging and other purposes. Additionally, this Correlation Id can also be attached to downstream HTTP calls made through an IHttpClientFactory. However, it should be noted that this library may not be fully comprehensive and that built-in tracing options for .NET apps may be more suitable for more extensive tracing needs.

Installation

You should install Elwark.CorrelationId from NuGet:

Install-Package Elwark.CorrelationId

NuGet

This command from Package Manager Console will download and install CorrelationId and all required dependencies.

All stable and some pre-release packages are available on NuGet.

Quick Start

Register with DI

Inside ConfigureServices add the required correlation id services, with common defaults.

services.AddCorrelationId()

or you can define with options

services.AddCorrelationId(options =>
{ 
    options.AddToLoggingScope = true;
    options.EnforceHeader = true;
    options.IgnoreRequestHeader = false;
    options.IncludeInResponse = true;
    options.RequestHeader = "X-Correlation-Id";
    options.ResponseHeader = "X-Correlation-Id";
    options.UpdateTraceIdentifier = false;
});

This registers a correlation id provider which generates new ids based on a random GUID.

Minimal API / MVC

You should install Elwark.CorrelationId.Http from NuGet:

Install-Package Elwark.CorrelationId.Http

NuGet

Add the middleware

Register the middleware into the pipeline. This should occur before any downstream middleware which requires the correlation ID. Normally this will be registered very early in the middleware pipeline.

app.UseCorrelationId();

Also Elwark.CorrelationId.Http contains TraceIdentifier Correlation id provider. You can replace GUID provider by TraceId just following code:

services.AddCorrelationId()
   .WithTraceIdentifierProvider();

After your correlation id will be trace identifier from HttpContext.

Add HttpClient forwarding

Forwarding your correlation id to another service by http call available by adding:

builder.Services
    .AddHttpClient<ITestClient, TestClient>()
    .AddHttpCorrelationIdForwarding();

gRPC

You should install Elwark.CorrelationId.Grpc from NuGet:

Install-Package Elwark.CorrelationId.Grpc

NuGet

Add the interceptor

Register the interceptor into the pipeline.

builder.Services
    .AddGrpc(options => options.UseCorrelationId());

Add GrpcClient forwarding

Forwarding your correlation id to another service by gRPC call available by adding:

builder.Services
    .AddGrpcClient<Greeter.GreeterClient>()
    .AddGrpcCorrelationIdForwarding();

or

builder.Services
    .AddGrpcClient<Greeter.GreeterClient>(options =>
    {
        options.AddCorrelationIdForwarding();
    });

Where you need to access the correlation Id, you may request the ICorrelationContextAccessor from DI.

public class MyClass
{
   private readonly ICorrelationContextAccessor _accessor;

   public MyClass(ICorrelationContextAccessor accessor)
   {
	  _accessor = accessor;
   }

   ...
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Elwark.CorrelationId:

Package Downloads
Elwark.CorrelationId.Grpc

ASP.NET Core correlation Id for distributed microservices.

Elwark.CorrelationId.Http

ASP.NET Core correlation Id for distributed microservices.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 2,768 3/1/2024
1.0.5 4,800 9/29/2023
1.0.4 600 9/1/2023
1.0.3 2,390 7/15/2023
1.0.2 2,392 4/6/2023
1.0.1 2,169 2/3/2023
1.0.0 555 1/26/2023