ekmsystems / serilog-enrichers-correlation-id

Enrich logs with a unique ID so you can track logs for specific requests.
MIT License
93 stars 37 forks source link

WithCorrelationIdHeader seems to keep creating new Guid #31

Closed 30akuma41 closed 4 years ago

30akuma41 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

ejcoyle88 commented 4 years ago

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

ejcoyle88 commented 4 years ago

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