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.
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.
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.
You should install Elwark.CorrelationId.Http from NuGet:
Install-Package Elwark.CorrelationId.Http
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.
Forwarding your correlation id to another service by http call available by adding:
builder.Services
.AddHttpClient<ITestClient, TestClient>()
.AddHttpCorrelationIdForwarding();
You should install Elwark.CorrelationId.Grpc from NuGet:
Install-Package Elwark.CorrelationId.Grpc
Register the interceptor into the pipeline.
builder.Services
.AddGrpc(options => options.UseCorrelationId());
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;
}
...
}