This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DFE-Digital/feature/correlation-id-logging
Feature/correlation id logging
- Loading branch information
Showing
6 changed files
with
110 additions
and
33 deletions.
There are no files selected for viewing
22 changes: 13 additions & 9 deletions
22
correlationIdMiddleware/correlationIdMiddleware/CorrelationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
using Ardalis.GuardClauses; | ||
namespace Dfe.Academisation.CorrelationIdMiddleware; | ||
|
||
namespace Dfe.Academisation.CorrelationIdMiddleware; | ||
|
||
public record CorrelationContext() : ICorrelationContext | ||
/// <inheritdoc /> | ||
public class CorrelationContext : ICorrelationContext | ||
{ | ||
public string? CorrelationId { get; private set; } | ||
public void SetContext(string correlationId) | ||
/// <inheritdoc /> | ||
public Guid CorrelationId { get; private set; } | ||
|
||
/// <inheritdoc /> | ||
public void SetContext(Guid correlationId) | ||
{ | ||
this.CorrelationId = Guard.Against.NullOrWhiteSpace(correlationId); | ||
if (correlationId == Guid.Empty) | ||
{ | ||
throw new ArgumentException("Guid cannot be empty", nameof(correlationId)); | ||
} | ||
this.CorrelationId = correlationId; | ||
} | ||
|
||
public string HeaderKey { get => "x-correlation-id"; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 14 additions & 3 deletions
17
correlationIdMiddleware/correlationIdMiddleware/ICorrelationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
namespace Dfe.Academisation.CorrelationIdMiddleware; | ||
|
||
/// <summary> | ||
/// Provides access to the current correlation id. You should register this as a scoped / per web request | ||
/// dependency in your IoC/DI container. | ||
/// </summary> | ||
public interface ICorrelationContext | ||
{ | ||
public string? CorrelationId { get; } | ||
public void SetContext(string correlationId); | ||
public string HeaderKey { get; } | ||
/// <summary> | ||
/// Returns the current correlation id if it has been set | ||
/// </summary> | ||
public Guid CorrelationId { get; } | ||
|
||
/// <summary> | ||
/// Used by the middleware to store the current correlation id. Do not call this method yourself. | ||
/// </summary> | ||
/// <param name="correlationId"></param> | ||
public void SetContext(Guid correlationId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Dfe.Academisation.CorrelationIdMiddleware; | ||
/// <summary> | ||
/// The keys used by the correlation id middleware. | ||
/// </summary> | ||
|
||
public class Keys | ||
{ | ||
/// <summary> | ||
/// The header key use to detect incoming correlation ids, and to send them in responses. | ||
/// Use this key if you are making subsequent requests so that correlation flows between services | ||
/// </summary> | ||
public const string HeaderKey = "x-correlationId"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters