Skip to content

Commit c1b40f0

Browse files
authored
Merge pull request #25 from pawan-regoti/added-propertyName-argument-to-WithRequestHeader
added optional `propertyName` argument to `WithRequestHeader`
2 parents 2db23c9 + 80d40e4 commit c1b40f0

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ or in `appsettings.json` file:
2828
"Serilog": {
2929
"MinimumLevel": "Debug",
3030
"Using": [ "Serilog.Enrichers.ClientInfo" ],
31-
"Enrich": [
32-
"WithClientIp",
31+
"Enrich": [
32+
"WithClientIp",
3333
"WithCorrelationId",
34-
{
35-
"Name": "WithRequestHeader",
34+
{
35+
"Name": "WithRequestHeader",
3636
"Args": { "headerName": "User-Agent"}
3737
}
3838
],
@@ -57,7 +57,7 @@ or
5757
"Serilog": {
5858
"MinimumLevel": "Debug",
5959
"Using": [ "Serilog.Enrichers.ClientInfo" ],
60-
"Enrich": [
60+
"Enrich": [
6161
{
6262
"Name": "WithClientIp",
6363
"Args": {
@@ -83,7 +83,7 @@ or
8383
"Serilog": {
8484
"MinimumLevel": "Debug",
8585
"Using": [ "Serilog.Enrichers.ClientInfo" ],
86-
"Enrich": [
86+
"Enrich": [
8787
{
8888
"Name": "WithCorrelationId",
8989
"Args": {
@@ -109,7 +109,7 @@ or
109109
"Serilog": {
110110
"MinimumLevel": "Debug",
111111
"Using": [ "Serilog.Enrichers.ClientInfo" ],
112-
"Enrich": [
112+
"Enrich": [
113113
{
114114
"Name": "WithRequestHeader",
115115
"Args": {
@@ -121,6 +121,13 @@ or
121121
"Args": {
122122
"headerName": "Connection"
123123
}
124+
},
125+
{
126+
"Name": "WithRequestHeader",
127+
"Args": {
128+
"headerName": "Content-Length",
129+
"propertyName": "RequestLength"
130+
}
124131
}
125132
],
126133
}
@@ -160,6 +167,7 @@ namespace MyWebApp
160167
.Enrich.WithClientIp()
161168
.Enrich.WithCorrelationId()
162169
.Enrich.WithRequestHeader("header-name")
170+
.Enrich.WithRequestHeader("another-header-name", "some-property-name")
163171
.CreateLogger();
164172
}
165173

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ public class ClientHeaderEnricher : ILogEventEnricher
1919
private readonly string _headerKey;
2020
private readonly IHttpContextAccessor _contextAccessor;
2121

22-
public ClientHeaderEnricher(string headerKey)
23-
: this(headerKey, new HttpContextAccessor())
22+
public ClientHeaderEnricher(string headerKey, string propertyName)
23+
: this(headerKey, propertyName, new HttpContextAccessor())
2424
{
2525
}
2626

27-
internal ClientHeaderEnricher(string headerKey, IHttpContextAccessor contextAccessor)
27+
internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor)
2828
{
2929
_headerKey = headerKey;
30-
_propertyName = headerKey.Replace("-", "");
30+
_propertyName = string.IsNullOrWhiteSpace(propertyName)
31+
? headerKey.Replace("-", "")
32+
: propertyName;
3133
_clientHeaderItemKey = $"Serilog_{headerKey}";
3234
_contextAccessor = contextAccessor;
3335
}
@@ -57,4 +59,4 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
5759

5860
logEvent.AddPropertyIfAbsent(logProperty);
5961
}
60-
}
62+
}

src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public static LoggerConfiguration WithCorrelationId(
7373
/// Registers the HTTP request header enricher to enrich logs with the header value.
7474
/// </summary>
7575
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
76+
/// <param name="propertyName">The property name of log</param>
7677
/// <param name="headerName">The header name to log its value</param>
7778
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
7879
/// <exception cref="ArgumentNullException">headerName</exception>
7980
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
80-
public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration, string headerName)
81+
public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration,
82+
string headerName, string propertyName = null)
8183
{
8284
if (enrichmentConfiguration == null)
8385
{
@@ -89,6 +91,6 @@ public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfigu
8991
throw new ArgumentNullException(nameof(headerName));
9092
}
9193

92-
return enrichmentConfiguration.With(new ClientHeaderEnricher(headerName));
94+
return enrichmentConfiguration.With(new ClientHeaderEnricher(propertyName, headerName));
9395
}
94-
}
96+
}

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ public ClientHeaderEnricherTests()
1616
_contextAccessor = Substitute.For<IHttpContextAccessor>();
1717
_contextAccessor.HttpContext.Returns(httpContext);
1818
}
19+
20+
[Fact]
21+
public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateNamedHeaderValueProperty()
22+
{
23+
// Arrange
24+
var headerKey = "RequestId";
25+
var propertyName = "HttpRequestId";
26+
var headerValue = Guid.NewGuid().ToString();
27+
_contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue);
28+
29+
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName, _contextAccessor);
30+
31+
LogEvent evt = null;
32+
var log = new LoggerConfiguration()
33+
.Enrich.With(clientHeaderEnricher)
34+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
35+
.CreateLogger();
36+
37+
// Act
38+
log.Information(@"First testing log enricher.");
39+
log.Information(@"Second testing log enricher.");
40+
41+
// Assert
42+
Assert.NotNull(evt);
43+
Assert.True(evt.Properties.ContainsKey(propertyName));
44+
Assert.Equal(headerValue, evt.Properties[propertyName].LiteralValue().ToString());
45+
}
1946

2047
[Fact]
2148
public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty()
@@ -25,7 +52,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateH
2552
var headerValue = Guid.NewGuid().ToString();
2653
_contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue);
2754

28-
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor);
55+
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor);
2956

3057
LogEvent evt = null;
3158
var log = new LoggerConfiguration()
@@ -54,8 +81,8 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
5481
_contextAccessor.HttpContext.Request.Headers.Add(headerKey1, headerValue1);
5582
_contextAccessor.HttpContext.Request.Headers.Add(headerKey2, headerValue2);
5683

57-
var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, _contextAccessor);
58-
var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, _contextAccessor);
84+
var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, propertyName:string.Empty, _contextAccessor);
85+
var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, propertyName:string.Empty, _contextAccessor);
5986

6087
LogEvent evt = null;
6188
var log = new LoggerConfiguration()
@@ -81,7 +108,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCrea
81108
{
82109
// Arrange
83110
var headerKey = "RequestId";
84-
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor);
111+
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor);
85112

86113
LogEvent evt = null;
87114
var log = new LoggerConfiguration()
@@ -114,4 +141,4 @@ public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException()
114141
// Assert
115142
Assert.Null(exception);
116143
}
117-
}
144+
}

0 commit comments

Comments
 (0)