Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

WithCorrelationIdHeader seems to keep creating new Guid #31

Closed
30akuma41 opened this issue Nov 9, 2019 · 4 comments
Closed

WithCorrelationIdHeader seems to keep creating new Guid #31

30akuma41 opened this issue Nov 9, 2019 · 4 comments
Assignees
Labels

Comments

@30akuma41
Copy link

Hi, it seems that if no header is found in WithCorrelationIdHeader() then a new Guid is being created (which is correct), but for each log event it seems that it creates a new one every time, resulting in a no correlation between the event logs.

Unless I misunderstood something in the documentation, shouldn't the fallback functionality work as WithCorrelationId() method if no header is found? That is to create a new correlation id and keep that id the same for all event logs?

@30akuma41
Copy link
Author

Not 100% if this fixes the issue without breaking other logic within the extension but I added the following code in the CorrelationIdHeaderEnricher and it seems to work.

var correlationId = string.IsNullOrEmpty(header) ? GetOrCreateCorrelationIdByItemName() <-- replaced Guid.NewGuid().ToString() : header;

The method used is similar to GetCorrelationId from CorrelationIdEnricher class

private string GetOrCreateCorrelationIdByItemName() { return (string)(_contextAccessor.HttpContext.Items[CorrelationIdItemName] ?? (_contextAccessor.HttpContext.Items[CorrelationIdItemName] = Guid.NewGuid().ToString())); }

and of course private static readonly string CorrelationIdItemName = $"{typeof(CorrelationIdHeaderEnricher).Name}+CorrelationId"; was added as well.

Please let me know if this is acceptable, or if other problems could arise if this change (or something similar) takes place.

lkurzyniec added a commit to lkurzyniec/serilog-enrichers-correlation-id that referenced this issue Nov 12, 2019
Getting correlationId from response header to avoid creating it again in CorrelationIdHeaderEnricher
@lkurzyniec
Copy link
Contributor

https://github.com/ekmsystems/serilog-enrichers-correlation-id/pull/33/files#diff-e4f3e565f0c3341e91e28b282cd1925eR51
Here I introduce a much simpler way to retrieve already created correlationId when it was not supplied.

lkurzyniec added a commit to lkurzyniec/serilog-enrichers-correlation-id that referenced this issue Nov 12, 2019
added unit test for reusing the same correlationId for multiple log entries
lkurzyniec added a commit to lkurzyniec/serilog-enrichers-correlation-id that referenced this issue Nov 12, 2019
added unit test for reusing the same correlationId for multiple log entries for second enricher
@ejcoyle88
Copy link
Contributor

Merged #33 which should fix this. Thanks @lkurzyniec. 👍
I'll push a new release tonight.

@ejcoyle88 ejcoyle88 self-assigned this Nov 13, 2019
@ejcoyle88 ejcoyle88 added the bug label Nov 13, 2019
@ejcoyle88
Copy link
Contributor

Released as v3.0.1 so I'm gonna close this one off. Feel free to reopen if there are any issues. 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants