!!Obsolete!! Please use Crip.Extensions.Correlation instead
The correlation ID is meant to be used to help Admin trace what was happening at the time of an error/request. It is only valuable as an interactive tool to help the Admin track down your error. An Admin uses the correlation ID as "breadcrumbs" to retrace a requests or processes in the Logging System to find what leads up to and causes a problem.
Install Crip.AspNetCore.Correlation NuGet package, or GitHub package
Configure services:
services.AddHttpContextAccessor(); // package requires that HTTP context accessor is available
services.AddCorrelation();
Configure pipeline:
// Add middlewares as early as possible
app.UseCorrelation();
// or
app.UseMiddleware<CorrelationIdMiddleware>();
app.UseMiddleware<CorrelationIdLoggingMiddleware>();
Correlation makes sense only when entire system is using same Correlation ID. To do so, you can use "traced" HTTP client witch will automatically add header to all outgoing requests.
services.AddTracedHttpClient<TClient, TImplementation>(((provider, client) =>
{
var config = provider.GetRequiredService<IOptions<TClientOptions>>().Value;
client.BaseAddress = new Uri(config.BaseUrl);
}));
You can customize correlation by providing custom options:
services.Configure<CorrelationIdOptions>(options =>
{
options.Cookie = "X-Correlation-Id"; // name of the cookie key to use as correlation identifier
options.Header = "X-Correlation-Id"; // name of the HTTP request/response header
options.PropertyName = "CorrelationId"; // name of the loggable property
options.IncludeInResponse = true; // include correlation identifier header in responses
});