Skip to content

Commit 0dda9a6

Browse files
authored
Merge pull request #36 from tobiashein/add-correlation-id-from-response
Add correlation ID from HTTP response
2 parents 4cf9f41 + 25961b7 commit 0dda9a6

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
4747
return;
4848
}
4949

50-
var header = httpContext.Request.Headers[_headerKey].ToString();
51-
var correlationId = !string.IsNullOrWhiteSpace(header)
52-
? header
53-
: (_addValueIfHeaderAbsence ? Guid.NewGuid().ToString() : null);
50+
var requestHeader = httpContext.Request.Headers[_headerKey];
51+
var responseHeader = httpContext.Response.Headers[_headerKey];
52+
53+
string correlationId;
54+
55+
if (!string.IsNullOrWhiteSpace(requestHeader))
56+
{
57+
correlationId = requestHeader;
58+
}
59+
else if (!string.IsNullOrWhiteSpace(responseHeader))
60+
{
61+
correlationId = responseHeader;
62+
}
63+
else if (_addValueIfHeaderAbsence)
64+
{
65+
correlationId = Guid.NewGuid().ToString();
66+
}
67+
else
68+
{
69+
correlationId = null;
70+
}
5471

5572
var correlationIdProperty = new LogEventProperty(PropertyName, new ScalarValue(correlationId));
5673
logEvent.AddOrUpdateProperty(correlationIdProperty);

test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,54 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade
108108
Assert.NotNull(evt.Properties[LogPropertyName].LiteralValue().ToString());
109109
}
110110

111+
[Fact]
112+
public void EnrichLogWithCorrelationId_WhenHttpResponseContainsCorrelationIdHeader_ShouldCreateCorrelationIdProperty()
113+
{
114+
// Arrange
115+
var correlationId = Guid.NewGuid().ToString();
116+
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, correlationId);
117+
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
118+
119+
LogEvent evt = null;
120+
var log = new LoggerConfiguration()
121+
.Enrich.With(correlationIdEnricher)
122+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
123+
.CreateLogger();
124+
125+
// Act
126+
log.Information(@"Has a correlation id.");
127+
128+
// Assert
129+
Assert.NotNull(evt);
130+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
131+
Assert.Equal(correlationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
132+
}
133+
134+
[Fact]
135+
public void EnrichLogWithCorrelationId_WhenHttpRequestAndResponseContainCorrelationIdHeader_ShouldCreateCorrelationIdPropertyFromHttpRequest()
136+
{
137+
// Arrange
138+
var requestCorrelationId = Guid.NewGuid().ToString();
139+
var responseCorrelationId = Guid.NewGuid().ToString();
140+
_contextAccessor.HttpContext.Request.Headers.Add(HeaderKey, requestCorrelationId);
141+
_contextAccessor.HttpContext.Response.Headers.Add(HeaderKey, responseCorrelationId);
142+
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
143+
144+
LogEvent evt = null;
145+
var log = new LoggerConfiguration()
146+
.Enrich.With(correlationIdEnricher)
147+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
148+
.CreateLogger();
149+
150+
// Act
151+
log.Information(@"Has a correlation id.");
152+
153+
// Assert
154+
Assert.NotNull(evt);
155+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
156+
Assert.Equal(requestCorrelationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
157+
}
158+
111159
[Fact]
112160
public void WithClientIp_ThenLoggerIsCalled_ShouldNotThrowException()
113161
{

0 commit comments

Comments
 (0)