A collection of zero-boilerplate correlation ID tracking libraries for .NET applications with automatic integration and configurable additional headers for comprehensive distributed tracing.
| Package | Description | NuGet | README |
|---|---|---|---|
| CorrelationId.AspNetCore | For ASP.NET Core applications | π Documentation | |
| CorrelationId.AzureFunctions | For Azure Functions | π Documentation |
- π Zero Boilerplate: No wrapper methods needed - correlation ID available everywhere automatically
- π Automatic Flow: Correlation ID flows seamlessly through all async operations via
AsyncLocal<T> - π¨ Automatic Header Tracking: Tracks
X-Correlation-Idand configurable additional headers - π― Auto-Generation: Generates new correlation ID if header is missing
- π Automatic Logging: Adds all captured headers to log entries
- π Structured Logging: Adds all headers as custom properties for searchable metadata
- π HTTP Client Integration: Automatically propagates all headers to outgoing HTTP requests
- β‘ Thread-Safe: Uses
AsyncLocal<T>for thread-safe header storage - ποΈ Configurable Headers: Support for additional custom headers like X-Event-Id, X-User-Id, etc.
- Middleware Integration: Seamless integration with ASP.NET Core pipeline
- Named HTTP Clients: Support for multiple configured HTTP clients with correlation
- Response Headers: Optionally add correlation headers to responses
- Multi-Trigger Support: HTTP, Queue, Service Bus, Event Hub, Timer, and Blob triggers
- Message-Based Correlation: Extracts correlation IDs from queue messages and events
- Base Class Support: Provides base classes for different trigger types
- Activity Integration: Works with distributed tracing and Application Insights
dotnet add package CorrelationId.AspNetCore// Program.cs
builder.Services.AddCorrelationId();
app.UseCorrelationId();
// That's it! Correlation ID now available everywhere automaticallydotnet add package CorrelationId.AzureFunctions// Program.cs
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddCorrelationId(options =>
{
options.AdditionalHeaders.AddRange(new[]
{
"X-Event-Id",
"X-User-Id",
"X-Tenant-Id"
});
});
})
.Build();public class MyService
{
private readonly ICorrelationIdService _correlationService;
private readonly ILogger<MyService> _logger;
public MyService(ICorrelationIdService correlationService, ILogger<MyService> logger)
{
_correlationService = correlationService;
_logger = logger;
}
public async Task DoSomethingAsync()
{
// Get correlation ID anywhere in your application
var correlationId = _correlationService.CorrelationId;
// Get additional headers
var userId = _correlationService.GetHeader("X-User-Id");
var eventId = _correlationService.GetHeader("X-Event-Id");
// All headers automatically included in logs
_logger.LogInformation("Processing request for user {UserId}", userId);
// Correlation automatically flows to async operations
await SomeOtherMethodAsync();
}
}// All HTTP clients automatically get correlation headers
public class ApiService
{
private readonly HttpClient _httpClient;
public ApiService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<string> CallExternalApiAsync()
{
// Correlation ID and additional headers automatically added
var response = await _httpClient.GetAsync("https://api.example.com/data");
return await response.Content.ReadAsStringAsync();
}
}Both libraries support extensive configuration:
services.AddCorrelationId(options =>
{
// Configure additional headers to track
options.AdditionalHeaders.AddRange(new[]
{
"X-Event-Id",
"X-User-Id",
"X-Tenant-Id",
"X-Request-Source",
"X-API-Version"
});
// Add headers to response (ASP.NET Core only)
options.AddAdditionalHeadersToResponse = true;
// Custom correlation ID header name
options.CorrelationIdHeaderName = "X-Custom-Correlation-Id";
// Configure HTTP client integration
options.EnableHttpClientIntegration = true;
});- Incoming Request: Middleware/Function intercepts incoming requests
- Header Extraction: Extracts correlation ID and configured additional headers
- Context Storage: Stores headers in
AsyncLocal<T>for thread-safe access - Automatic Logging: All log entries include captured headers
- HTTP Client Integration: Outgoing requests automatically include headers
- Response Headers: Optionally adds headers to response for client tracking
Both libraries use AsyncLocal<T> to ensure correlation context flows correctly through:
- Async/await operations
- Task continuations
- Thread pool operations
- Background services
- Headers not flowing: Ensure middleware is registered before other middleware
- HTTP client not including headers: Verify HTTP client integration is enabled
- Headers missing in logs: Check that structured logging is configured
Headers are automatically added to:
- Message Prefix:
[CorrelationId: abc123] [X-User-Id: user456] Your log message - Structured Properties: Available as searchable metadata in log aggregation systems
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.