File tree Expand file tree Collapse file tree 1 file changed +8
-10
lines changed
src/Serilog.Enrichers.ClientInfo/Enrichers Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Original file line number Diff line number Diff line change 22using Serilog . Events ;
33using System ;
44using System . Linq ;
5+ using System . Runtime . CompilerServices ;
56
67#if NETFULL
78
1112using Microsoft . AspNetCore . Http ;
1213#endif
1314
15+ [ assembly: InternalsVisibleTo ( "Serilog.Enrichers.ClientInfo.Tests" ) ]
16+
1417namespace Serilog . Enrichers
1518{
1619 public class ClientIpEnricher : ILogEventEnricher
@@ -59,25 +62,20 @@ private string GetIpAddress()
5962 {
6063 var ipAddress = _contextAccessor . HttpContext . Request . ServerVariables [ "HTTP_X_FORWARDED_FOR" ] ;
6164
62- if ( ! string . IsNullOrEmpty ( ipAddress ) )
63- {
64- return GetIpAddressFromProxy ( ipAddress ) ;
65- }
66-
67- return _contextAccessor . HttpContext . Request . ServerVariables [ "REMOTE_ADDR" ] ;
65+ return ! string . IsNullOrEmpty ( ipAddress )
66+ ? GetIpAddressFromProxy ( ipAddress )
67+ : _contextAccessor . HttpContext . Request . ServerVariables [ "REMOTE_ADDR" ] ;
6868 }
6969
7070#else
7171 private string GetIpAddress ( )
7272 {
73- var ipAddress = _contextAccessor . HttpContext . Request . Headers [ "X-forwarded-for" ] . FirstOrDefault ( ) ;
73+ var ipAddress = _contextAccessor . HttpContext ? . Request ? . Headers [ "X-forwarded-for" ] . FirstOrDefault ( ) ;
7474
7575 if ( ! string . IsNullOrEmpty ( ipAddress ) )
76- {
7776 return GetIpAddressFromProxy ( ipAddress ) ;
78- }
7977
80- return _contextAccessor . HttpContext . Connection . RemoteIpAddress . ToString ( ) ;
78+ return _contextAccessor . HttpContext ? . Connection ? . RemoteIpAddress ? . ToString ( ) ;
8179 }
8280#endif
8381
You can’t perform that action at this time.
0 commit comments