Elwark.CorrelationId
2.0.0
dotnet add package Elwark.CorrelationId --version 2.0.0
NuGet\Install-Package Elwark.CorrelationId -Version 2.0.0
<PackageReference Include="Elwark.CorrelationId" Version="2.0.0" />
paket add Elwark.CorrelationId --version 2.0.0
#r "nuget: Elwark.CorrelationId, 2.0.0"
// Install Elwark.CorrelationId as a Cake Addin #addin nuget:?package=Elwark.CorrelationId&version=2.0.0 // Install Elwark.CorrelationId as a Cake Tool #tool nuget:?package=Elwark.CorrelationId&version=2.0.0
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
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
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
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 | Versions 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. |
-
net7.0
- Elwark.CorrelationId.Abstractions (>= 2.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
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.