Skip to content

Commit a80bb53

Browse files
Fix merge conflicts.
2 parents 3d959a2 + d1fa0e5 commit a80bb53

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Serilog.Core;
22
using Serilog.Events;
3-
using System;
43
using System.Linq;
54
using System.Runtime.CompilerServices;
65

@@ -74,27 +73,17 @@ private string GetIpAddress()
7473
{
7574
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[_forwardHeaderKey].FirstOrDefault();
7675

77-
if (!string.IsNullOrEmpty(ipAddress))
78-
return GetIpAddressFromProxy(ipAddress);
79-
80-
return _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
76+
return !string.IsNullOrEmpty(ipAddress)
77+
? GetIpAddressFromProxy(ipAddress)
78+
: _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
8179
}
8280
#endif
8381

8482
private string GetIpAddressFromProxy(string proxifiedIpList)
8583
{
8684
var addresses = proxifiedIpList.Split(',');
8785

88-
if (addresses.Length != 0)
89-
{
90-
// If IP contains port, it will be after the last : (IPv6 uses : as delimiter and
91-
// could have more of them)
92-
return addresses[0].Contains(":")
93-
? addresses[0].Substring(0, addresses[0].LastIndexOf(":", StringComparison.Ordinal))
94-
: addresses[0];
95-
}
96-
97-
return string.Empty;
86+
return addresses.Length == 0 ? string.Empty : addresses[0].Trim();
9887
}
9988
}
10089
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ public ClientIpEnricherTests()
1818
_contextAccessor.HttpContext.Returns(httpContext);
1919
}
2020

21-
[Fact]
22-
public void EnrichLogWithClientIp_ShouldCreateClientIpPropertyWithValue()
21+
[Theory]
22+
[InlineData("::1")]
23+
[InlineData("192.168.1.1")]
24+
[InlineData("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
25+
[InlineData("2001:db8:85a3:8d3:1319:8a2e:370:7348")]
26+
public void EnrichLogWithClientIp_ShouldCreateClientIpPropertyWithValue(string ip)
2327
{
2428
// Arrange
25-
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Parse("::1");
29+
var ipAddress = IPAddress.Parse(ip);
30+
_contextAccessor.HttpContext.Connection.RemoteIpAddress = ipAddress;
2631

2732
var ipEnricher = new ClientIpEnricher(ForwardHeaderKey, _contextAccessor);
2833

@@ -38,7 +43,7 @@ public void EnrichLogWithClientIp_ShouldCreateClientIpPropertyWithValue()
3843
// Assert
3944
Assert.NotNull(evt);
4045
Assert.True(evt.Properties.ContainsKey("ClientIp"));
41-
Assert.Equal("::1", evt.Properties["ClientIp"].LiteralValue());
46+
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
4247
}
4348

4449
[Fact]
@@ -64,12 +69,17 @@ public void EnrichLogWithClientIp_WhenLogMoreThanOnce_ShouldReadClientIpValueFro
6469
Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue());
6570
}
6671

67-
[Fact]
68-
public void EnrichLogWithClientIp_WhenRequestContainForwardHeader_ShouldCreateClientIpPropertyWithValue()
72+
[Theory]
73+
[InlineData("::1")]
74+
[InlineData("192.168.1.1")]
75+
[InlineData("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
76+
[InlineData("2001:db8:85a3:8d3:1319:8a2e:370:7348")]
77+
public void EnrichLogWithClientIp_WhenRequestContainForwardHeader_ShouldCreateClientIpPropertyWithValue(string ip)
6978
{
7079
//Arrange
80+
var ipAddress = IPAddress.Parse(ip);
7181
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
72-
_contextAccessor.HttpContext.Request.Headers.Add(ForwardHeaderKey, IPAddress.Broadcast.ToString());
82+
_contextAccessor.HttpContext.Request.Headers.Add(ForwardHeaderKey, ipAddress.ToString());
7383

7484
var ipEnricher = new ClientIpEnricher(ForwardHeaderKey, _contextAccessor);
7585

@@ -85,7 +95,7 @@ public void EnrichLogWithClientIp_WhenRequestContainForwardHeader_ShouldCreateCl
8595
// Assert
8696
Assert.NotNull(evt);
8797
Assert.True(evt.Properties.ContainsKey("ClientIp"));
88-
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
98+
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
8999
}
90100

91101
[Fact]

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Serilog.Enrichers.ClientInfo.Tests;
99
public class CorrelationIdEnricherTests
1010
{
1111
private const string HeaderKey = "x-correlation-id";
12+
private const string LogPropertyName = "CorrelationId";
1213
private readonly IHttpContextAccessor _contextAccessor;
1314

1415
public CorrelationIdEnricherTests()
@@ -24,7 +25,6 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_S
2425
// Arrange
2526
var correlationId = Guid.NewGuid().ToString();
2627
_contextAccessor.HttpContext.Request.Headers.Add(HeaderKey, correlationId);
27-
2828
var correlationIdEnricher = new CorrelationIdEnricher(HeaderKey, false, _contextAccessor);
2929

3030
LogEvent evt = null;
@@ -38,8 +38,8 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_S
3838

3939
// Assert
4040
Assert.NotNull(evt);
41-
Assert.True(evt.Properties.ContainsKey(HeaderKey));
42-
Assert.Equal(correlationId, evt.Properties[HeaderKey].LiteralValue().ToString());
41+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
42+
Assert.Equal(correlationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
4343
}
4444

4545
[Fact]
@@ -62,8 +62,8 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestContainCorrelationHeader_S
6262

6363
// Assert
6464
Assert.NotNull(evt);
65-
Assert.True(evt.Properties.ContainsKey(HeaderKey));
66-
Assert.Equal(correlationId, evt.Properties[HeaderKey].LiteralValue().ToString());
65+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
66+
Assert.Equal(correlationId, evt.Properties[LogPropertyName].LiteralValue().ToString());
6767
}
6868

6969
[Fact]
@@ -83,8 +83,8 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade
8383

8484
// Assert
8585
Assert.NotNull(evt);
86-
Assert.True(evt.Properties.ContainsKey(HeaderKey));
87-
Assert.Null(evt.Properties[HeaderKey].LiteralValue());
86+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
87+
Assert.Null(evt.Properties[LogPropertyName].LiteralValue());
8888
}
8989

9090
[Fact]
@@ -104,8 +104,8 @@ public void EnrichLogWithCorrelationId_WhenHttpRequestNotContainCorrelationHeade
104104

105105
// Assert
106106
Assert.NotNull(evt);
107-
Assert.True(evt.Properties.ContainsKey(HeaderKey));
108-
Assert.NotNull(evt.Properties[HeaderKey].LiteralValue().ToString());
107+
Assert.True(evt.Properties.ContainsKey(LogPropertyName));
108+
Assert.NotNull(evt.Properties[LogPropertyName].LiteralValue().ToString());
109109
}
110110

111111
[Fact]

0 commit comments

Comments
 (0)