TraceLink.AspNetCore
7.0.0.4
dotnet add package TraceLink.AspNetCore --version 7.0.0.4
NuGet\Install-Package TraceLink.AspNetCore -Version 7.0.0.4
<PackageReference Include="TraceLink.AspNetCore" Version="7.0.0.4" />
paket add TraceLink.AspNetCore --version 7.0.0.4
#r "nuget: TraceLink.AspNetCore, 7.0.0.4"
// Install TraceLink.AspNetCore as a Cake Addin #addin nuget:?package=TraceLink.AspNetCore&version=7.0.0.4 // Install TraceLink.AspNetCore as a Cake Tool #tool nuget:?package=TraceLink.AspNetCore&version=7.0.0.4
Getting Started
Install Nuget Package TraceLink.AspNetCore
Correlation ID
The Correlation ID will remaing static for the lifetime of a process. This enables correlation of a process over multiple services.
Example: New ID → ServiceA → Same ID → ServiceB → Same ID → ServiceC
public void ConfigureServices(IServiceCollection services)
{
// Adds Required Sercies and Configuration
services.AddCorrelation();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Enables Correlation Middleware
app.UseCorrelation();
}
Configuration
services.AddCorrelation(o =>
{
// The default value is x-correlation-id
o.Key = "my-custom-key";
o.AttachToResponse = true;
o.IsRequired = true;
o.AttachToLoggingScope = false;
// The default value is correlation-id
o.LoggingScopeKey = "my-custom-scope"
o.UseIdProvider<MyCustomIdProvider>();
o.UseIdForwarder<MyCustomIdForwarder>();
});
Accessing Correlation
public class MyClass
{
private readonly IContextAccessor<CorrelationContext> _contextAccessor;
public MyClass(IContextAccessor<CorrelationContext> contextAccessor)
{
_contextAccessor = contextAccessor
}
public void MyMethod()
{
string correlationId = _contextAccessor.Context.CorrelationId;
}
}
Trace ID
A unique Trace ID is generated every time a message transits from service to another, unlike the Correlation ID which remains static for the lifetime of the process. This is helpful for tracing how the current process moves between services.
Example: ServiceA → New ID → ServiceB → New ID → ServiceC
public void ConfigureServices(IServiceCollection services)
{
// Adds Required Sercies and Configuration
services.AddTracing();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Enables Correlation Middleware
app.UseTracing();
}
Configuration
services.AddTracing(o =>
{
// The default value is x-tracing-id
o.Key = "my-custom-key";
o.AttachToResponse = true;
o.IsRequired = true;
o.AttachToLoggingScope = false;
// The default value is tracing-id
o.LoggingScopeKey = "my-custom-scope"
o.UseIdProvider<MyCustomIdProvider>();
o.UseIdForwarder<MyCustomIdForwarder>();
});
Accessing Correlation
public class MyClass
{
private readonly IContextAccessor<TraceContext> _contextAccessor;
public MyClass(IContextAccessor<TraceContext> contextAccessor)
{
_contextAccessor = contextAccessor
}
public void MyMethod()
{
string traceId = _contextAccessor.Context.TraceId;
}
}
Applying Attributes to Controller or Methods
It is also possible to change how a Controller or Method will handle an incoming request by using the following Attributes
Attach ID to the response headers.
When these attributes are present the Correlation or Trace ID will be attached to the response headers.
- AttachCorrelationIdToResponseHeader
- AttachTraceIdToResponseHeaderAttribute
Enforce an incoming request to have the ID header
When these attributes are present if an incoming request does not contain the Correlation or Trace ID headers it will respond with 400 (BadRequest). This can be enabled API wide by settings the IsRequired
property to true when adding Correlation or Trace ID.
- CorrelationIdHeaderRequiredAttribute
- TraceIdHeaderRequiredAttribute
Make an incoming requests ID header optional.
When these attributes are present the IsRequiredAttribute
or IsRequired
option are no longer enforced.
- CorrelationIdHeaderNotRequiredAttribute
- TraceIdHeaderNotRequiredAttribute
Example
Below is an example of using the IsRequied and IsNotRequired attributes for the Correlation ID.
// All calls to this controller must have the Correlation ID header present in the incoming request.
[ApiController]
[CorrelationIdHeaderRequired]
public FooController: ControllerBase
{
// When this endpoint is called, the Correlation ID does not need to be present in the headers of the incoming request as we've used the Not Required attribute.
[HttpGet("{fooId}")]
[CorrelationIdHeaderNotRequired]
public IActionResult Get(string fooId)
{
}
// When this endpoint is called, the Correlation ID must be present in the headers of the incoming request as the controller has the Is Required attribute.
// It will also attach the Correlation ID to the headers of the Http Response.
[HttpPost]
[AttachCorrelationIdToResponseHeader]
public IActionResult Get(FooDto newFoo)
{
}
}
NOTE: This can also be done with the Trace ID.
Releases
Package | Downloads | NuGet |
---|---|---|
TraceLink.Abstractions | ||
TraceLink.AspNetCore | ||
TraceLink.NServiceBus |
Contributors
Nuget Icon by Bernd Lakenbrink from Noun Project
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Http (>= 2.2.0)
- TraceLink.Abstractions (>= 7.0.0.4)
-
net5.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Http (>= 5.0.0)
- TraceLink.Abstractions (>= 7.0.0.4)
-
net6.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Http (>= 6.0.0)
- TraceLink.Abstractions (>= 7.0.0.4)
-
net7.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Http (>= 7.0.0)
- TraceLink.Abstractions (>= 7.0.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
* Added Support for NetStandard2.1