-
Notifications
You must be signed in to change notification settings - Fork 25
Adding generated correlation id to response #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
0583198
0f7948d
d9244d6
e24fc82
2dedc05
6967e10
c2b6c9d
8232311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.AspNetCore.Http; | ||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.Primitives; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #nullable enable | ||||||||||||||||||||||||||||||||||||||||||
| namespace Serilog.Preparers.CorrelationIds | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| internal class CorrelationIdPreparer : ICorrelationIdPreparer | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| protected string? CorrelationId { get; set; } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||||||||||||
| public string? PrepareCorrelationId( | ||||||||||||||||||||||||||||||||||||||||||
| HttpContext httpContext, | ||||||||||||||||||||||||||||||||||||||||||
| CorrelationIdPreparerOptions correlationIdPreparerOptions) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| if (string.IsNullOrEmpty(CorrelationId)) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| CorrelationId = PrepareValueForCorrelationId(httpContext, correlationIdPreparerOptions); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return CorrelationId; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| protected string? CorrelationId { get; set; } | |
| /// <inheritdoc/> | |
| public string? PrepareCorrelationId( | |
| HttpContext httpContext, | |
| CorrelationIdPreparerOptions correlationIdPreparerOptions) | |
| { | |
| if (string.IsNullOrEmpty(CorrelationId)) | |
| { | |
| CorrelationId = PrepareValueForCorrelationId(httpContext, correlationIdPreparerOptions); | |
| } | |
| return CorrelationId; | |
| /// <inheritdoc/> | |
| public string? PrepareCorrelationId( | |
| HttpContext httpContext, | |
| CorrelationIdPreparerOptions correlationIdPreparerOptions) | |
| { | |
| return PrepareValueForCorrelationId(httpContext, correlationIdPreparerOptions); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| namespace Serilog.Preparers.CorrelationIds | ||
| { | ||
| /// <summary> | ||
| /// Settings for <see cref="ICorrelationIdPreparer"/>. | ||
| /// </summary> | ||
| public class CorrelationIdPreparerOptions | ||
| { | ||
| /// <summary> | ||
| /// Determines whether to add a new correlation ID value if the header is absent. | ||
| /// </summary> | ||
| public bool AddValueIfHeaderAbsence { get; } | ||
|
|
||
| /// <summary> | ||
| /// The header key used to retrieve the correlation ID from the HTTP request or response headers. | ||
| /// </summary> | ||
| public string HeaderKey { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CorrelationIdPreparerOptions" /> class. | ||
| /// </summary> | ||
| /// <param name="addValueIfHeaderAbsence">Determines whether to add a new correlation ID value if the header is absent.</param> | ||
| /// <param name="headerKey">The header key used to retrieve the correlation ID from the HTTP request or response headers.</param> | ||
| public CorrelationIdPreparerOptions( | ||
| bool addValueIfHeaderAbsence, | ||
| string headerKey) | ||
| { | ||
| AddValueIfHeaderAbsence = addValueIfHeaderAbsence; | ||
| HeaderKey = headerKey; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| #nullable enable | ||
| namespace Serilog.Preparers.CorrelationIds | ||
| { | ||
| /// <summary> | ||
| /// Preparer for correlation ID. | ||
| /// </summary> | ||
| public interface ICorrelationIdPreparer | ||
| { | ||
| /// <summary> | ||
| /// Prepares the correlation ID. | ||
| /// </summary> | ||
| /// <param name="httpContext">The HTTP context.</param> | ||
| /// <param name="correlationIdPreparerOptions">Options for preparation.</param> | ||
| /// <returns>The correlation ID.</returns> | ||
| string? PrepareCorrelationId( | ||
| HttpContext httpContext, | ||
| CorrelationIdPreparerOptions correlationIdPreparerOptions); | ||
| } | ||
| } | ||
| #nullable disable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
CorrelationIdproperty is not thread-safe and could lead to race conditions when multiple threads access the sameCorrelationIdPreparerinstance. Since this class may be registered as a singleton through DI, concurrent requests could overwrite each other's correlation IDs. Consider usingAsyncLocal<string>or making this class scoped/transient, or remove the caching altogether since the preparer is called once per log event.