DMNSN.AspNetCore.Middlewares.CorrelationId
8.0.3
dotnet add package DMNSN.AspNetCore.Middlewares.CorrelationId --version 8.0.3
NuGet\Install-Package DMNSN.AspNetCore.Middlewares.CorrelationId -Version 8.0.3
<PackageReference Include="DMNSN.AspNetCore.Middlewares.CorrelationId" Version="8.0.3" />
paket add DMNSN.AspNetCore.Middlewares.CorrelationId --version 8.0.3
#r "nuget: DMNSN.AspNetCore.Middlewares.CorrelationId, 8.0.3"
// Install DMNSN.AspNetCore.Middlewares.CorrelationId as a Cake Addin #addin nuget:?package=DMNSN.AspNetCore.Middlewares.CorrelationId&version=8.0.3 // Install DMNSN.AspNetCore.Middlewares.CorrelationId as a Cake Tool #tool nuget:?package=DMNSN.AspNetCore.Middlewares.CorrelationId&version=8.0.3
Correlation ID Middleware for ASP.NET Core
This middleware is designed to monitor and manage correlation IDs across incoming requests and outgoing responses in ASP.NET Core applications. It ensures that every operation within your application can be traced with a unique identifier, making debugging and monitoring significantly easier.
Features
- Automatic Correlation ID Management: Automatically assigns a correlation ID to incoming requests if one is not present.
- Customizable Correlation ID Key: Allows customization of the HTTP header key used for the correlation ID.
- Configurable through appsettings.json: Easily configure the middleware's behavior through the application's configuration file.
Solution Structure
The solution is structured into two main projects:
DMNSN.AspNetCore.Middlewares.CorrelationId
: The core middleware project.DMNSN.AspNetCore.Middlewares.CorrelationId.xUnit
: The test project for the middleware.
DMNSN.AspNetCore.Middlewares.CorrelationId
This project contains the implementation of the Correlation ID middleware. It is responsible for adding a unique correlation ID to the HTTP context of each incoming request. This can then be used for logging and tracing purposes.
DMNSN.AspNetCore.Middlewares.CorrelationId.xUnit
This project contains the unit tests for the Correlation ID middleware, ensuring its functionality works as expected. It uses xUnit for testing and includes tests for both enabling and disabling the middleware.
Getting Started
To get started with using the Correlation ID middleware in your project, please see detail in this section.
Prerequisites
- .NET 8.0 SDK or later
- An existing ASP.NET Core project
Installation
Add the middleware to your project by including it in your
csproj
file or using NuGet Package Manager.Ensure your project file (
DMNSN.AspNetCore.Middlewares.CorrelationId.csproj
) includes the necessary dependencies:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
</ItemGroup>
Usage
- In your
Startup.cs
or wherever you configure your HTTP request pipeline, add the middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other configurations...
app.UseCorrelationIdMiddleware(Configuration);
// Continue with the rest of the pipeline...
}
- Optionally, you can customize the middleware options by adding a section in your
appsettings.json
:
"DMNSN": {
"Middlewares": {
"CorrelationId": {
"EnableFeature": true,
"CorrelationKey": "X-Correlation-ID"
}
}
}
- If you prefer to pass options programmatically, you can use the
UseCorrelationIdMiddleware
extension method:
app.UseCorrelationIdMiddleware(new CorrelationIdMiddlewareOptions
{
EnableFeature = true,
CorrelationKey = "X-Correlation-ID"
});
Running the Tests
To run the tests for the Correlation ID middleware, navigate to the DMNSN.AspNetCore.Middlewares.CorrelationId.xUnit project directory and execute the following command:
This will run all the unit tests in the project and output the results.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues to discuss potential improvements or fixes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release. Starting base version with version of dotnet