@@ -16,7 +16,7 @@ namespace Serilog.Enrichers
1616 public class ClientIpEnricher : ILogEventEnricher
1717 {
1818 private const string IpAddressPropertyName = "ClientIp" ;
19- private const string IpAddresstItemKey = "Serilog_ClientIp" ;
19+ private const string IpAddressItemKey = "Serilog_ClientIp" ;
2020
2121 private readonly IHttpContextAccessor _contextAccessor ;
2222
@@ -36,7 +36,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
3636 if ( httpContext == null )
3737 return ;
3838
39- if ( httpContext . Items [ IpAddresstItemKey ] is LogEventProperty logEventProperty )
39+ if ( httpContext . Items [ IpAddressItemKey ] is LogEventProperty logEventProperty )
4040 {
4141 logEvent . AddPropertyIfAbsent ( logEventProperty ) ;
4242 return ;
@@ -48,7 +48,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
4848 ipAddress = "unknown" ;
4949
5050 var ipAddressProperty = new LogEventProperty ( IpAddressPropertyName , new ScalarValue ( ipAddress ) ) ;
51- httpContext . Items . Add ( IpAddresstItemKey , ipAddressProperty ) ;
51+ httpContext . Items . Add ( IpAddressItemKey , ipAddressProperty ) ;
5252
5353 logEvent . AddPropertyIfAbsent ( ipAddressProperty ) ;
5454 }
@@ -76,25 +76,24 @@ private string GetIpAddress()
7676 {
7777 return GetIpAddressFromProxy ( ipAddress ) ;
7878 }
79-
79+
8080 return _contextAccessor . HttpContext . Connection . RemoteIpAddress . ToString ( ) ;
8181 }
8282#endif
83-
84- private string GetIpAddressFromProxy ( string proxiedIpList )
85- {
86- var addresses = proxiedIpList . Split ( ',' ) ;
8783
88- if ( addresses . Length != 0 )
84+ private string GetIpAddressFromProxy ( string proxifiedIpList )
8985 {
90- // If IP contains port, it will be after the last : (IPv6 uses : as delimiter and could have more of them)
91- return addresses [ 0 ] . Contains ( ":" )
92- ? addresses [ 0 ] . Substring ( 0 , addresses [ 0 ] . LastIndexOf ( ":" , StringComparison . Ordinal ) )
93- : addresses [ 0 ] ;
94- }
86+ var addresses = proxifiedIpList . Split ( ',' ) ;
9587
96- return string . Empty ;
97- }
98-
88+ if ( addresses . Length != 0 )
89+ {
90+ // If IP contains port, it will be after the last : (IPv6 uses : as delimiter and could have more of them)
91+ return addresses [ 0 ] . Contains ( ":" )
92+ ? addresses [ 0 ] . Substring ( 0 , addresses [ 0 ] . LastIndexOf ( ":" , StringComparison . Ordinal ) )
93+ : addresses [ 0 ] ;
94+ }
95+
96+ return string . Empty ;
97+ }
9998 }
100- }
99+ }
0 commit comments