Skip to content

Commit d1fa0e5

Browse files
authored
Merge pull request #20 from serilog-contrib/19-ipv6-address
Fixed bug #19.
2 parents 3f2d61a + b640a5a commit d1fa0e5

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

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

Lines changed: 4 additions & 14 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

@@ -72,26 +71,17 @@ private string GetIpAddress()
7271
{
7372
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[ClinetIpConfiguration.XForwardHeaderName].FirstOrDefault();
7473

75-
if (!string.IsNullOrEmpty(ipAddress))
76-
return GetIpAddressFromProxy(ipAddress);
77-
78-
return _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
74+
return !string.IsNullOrEmpty(ipAddress)
75+
? GetIpAddressFromProxy(ipAddress)
76+
: _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
7977
}
8078
#endif
8179

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

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

src/Serilog.Enrichers.ClientInfo/Serilog.Enrichers.ClientInfo.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
<AssemblyName>Serilog.Enrichers.ClientInfo</AssemblyName>
55
<RootNamespace>Serilog</RootNamespace>
66
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
7-
<LangVersion>7.3</LangVersion>
7+
<Version>1.3.0</Version>
8+
<LangVersion>latest</LangVersion>
89
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
10+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
911
</PropertyGroup>
1012

1113
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">

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

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

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

2631
var ipEnricher = new ClientIpEnricher(_contextAccessor);
2732

@@ -37,7 +42,7 @@ public void When_Enrich_Log_Event_With_IpEnricher_Should_Contain_ClientIp_Proper
3742
// Assert
3843
Assert.NotNull(evt);
3944
Assert.True(evt.Properties.ContainsKey("ClientIp"));
40-
Assert.Equal("::1", evt.Properties["ClientIp"].LiteralValue());
45+
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
4146
}
4247

4348
[Fact]
@@ -63,12 +68,17 @@ public void When_Enrich_Log_Event_With_IpEnricher_And_Log_More_Than_Once_Should_
6368
Assert.Equal(IPAddress.Loopback.ToString(), evt.Properties["ClientIp"].LiteralValue());
6469
}
6570

66-
[Fact]
67-
public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value()
71+
[Theory]
72+
[InlineData("::1")]
73+
[InlineData("192.168.1.1")]
74+
[InlineData("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
75+
[InlineData("2001:db8:85a3:8d3:1319:8a2e:370:7348")]
76+
public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHeader_Should_Read_ClientIp_Value_From_Header_Value(string ip)
6877
{
6978
//Arrange
79+
var ipAddress = IPAddress.Parse(ip);
7080
_contextAccessor.HttpContext.Connection.RemoteIpAddress = IPAddress.Loopback;
71-
_contextAccessor.HttpContext.Request.Headers.Add(ClinetIpConfiguration.XForwardHeaderName, IPAddress.Broadcast.ToString());
81+
_contextAccessor.HttpContext.Request.Headers.Add(ClinetIpConfiguration.XForwardHeaderName, ipAddress.ToString());
7282

7383
var ipEnricher = new ClientIpEnricher(_contextAccessor);
7484

@@ -84,7 +94,7 @@ public void When_Enrich_Log_Event_With_IpEnricher_AndRequest_Contain_ForwardHead
8494
// Assert
8595
Assert.NotNull(evt);
8696
Assert.True(evt.Properties.ContainsKey("ClientIp"));
87-
Assert.Equal(IPAddress.Broadcast.ToString(), evt.Properties["ClientIp"].LiteralValue());
97+
Assert.Equal(ipAddress.ToString(), evt.Properties["ClientIp"].LiteralValue());
8898
}
8999

90100
[Fact]

0 commit comments

Comments
 (0)