-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from serilog-contrib/feature/drop-dotnet-frame…
…work Drop support for .NET Framework.
- Loading branch information
Showing
10 changed files
with
76 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/Serilog.Enrichers.ClientInfo/Accessors/HttpContextAccessor.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 32 additions & 66 deletions
98
src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,55 @@ | ||
using Serilog.Core; | ||
using Microsoft.AspNetCore.Http; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
#if NETFULL | ||
[assembly: InternalsVisibleTo("Serilog.Enrichers.ClientInfo.Tests")] | ||
|
||
using Serilog.Enrichers.ClientInfo.Accessors; | ||
namespace Serilog.Enrichers; | ||
|
||
#else | ||
using Microsoft.AspNetCore.Http; | ||
#endif | ||
public class ClientIpEnricher : ILogEventEnricher | ||
Check warning on line 10 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs GitHub Actions / build
Check warning on line 10 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs GitHub Actions / build
|
||
{ | ||
private const string IpAddressPropertyName = "ClientIp"; | ||
private const string IpAddressItemKey = "Serilog_ClientIp"; | ||
|
||
[assembly: InternalsVisibleTo("Serilog.Enrichers.ClientInfo.Tests")] | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
|
||
namespace Serilog.Enrichers | ||
{ | ||
public class ClientIpEnricher : ILogEventEnricher | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ClientIpEnricher"/> class. | ||
/// </summary> | ||
public ClientIpEnricher() : this(new HttpContextAccessor()) | ||
{ | ||
private const string IpAddressPropertyName = "ClientIp"; | ||
private const string IpAddressItemKey = "Serilog_ClientIp"; | ||
private readonly string _forwardHeaderKey; | ||
} | ||
|
||
private readonly IHttpContextAccessor _contextAccessor; | ||
internal ClientIpEnricher(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public ClientIpEnricher(string forwardHeaderKey) | ||
: this(forwardHeaderKey, new HttpContextAccessor()) | ||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
Check warning on line 29 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs GitHub Actions / build
Check warning on line 29 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs GitHub Actions / build
|
||
{ | ||
var httpContext = _contextAccessor.HttpContext; | ||
if (httpContext == null) | ||
{ | ||
return; | ||
} | ||
|
||
internal ClientIpEnricher(string forwardHeaderKey, IHttpContextAccessor contextAccessor) | ||
if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty) | ||
{ | ||
_forwardHeaderKey = forwardHeaderKey; | ||
_contextAccessor = contextAccessor; | ||
logEvent.AddPropertyIfAbsent(logEventProperty); | ||
return; | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
var httpContext = _contextAccessor.HttpContext; | ||
if (httpContext == null) | ||
return; | ||
|
||
if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty) | ||
{ | ||
logEvent.AddPropertyIfAbsent(logEventProperty); | ||
return; | ||
} | ||
|
||
var ipAddress = GetIpAddress(); | ||
|
||
if (string.IsNullOrWhiteSpace(ipAddress)) | ||
ipAddress = "unknown"; | ||
var ipAddress = _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(); | ||
|
||
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress)); | ||
httpContext.Items.Add(IpAddressItemKey, ipAddressProperty); | ||
|
||
logEvent.AddPropertyIfAbsent(ipAddressProperty); | ||
} | ||
|
||
#if NETFULL | ||
|
||
private string GetIpAddress() | ||
if (string.IsNullOrWhiteSpace(ipAddress)) | ||
{ | ||
var ipAddress = _contextAccessor.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; | ||
|
||
return !string.IsNullOrEmpty(ipAddress) | ||
? GetIpAddressFromProxy(ipAddress) | ||
: _contextAccessor.HttpContext.Request.ServerVariables["REMOTE_ADDR"]; | ||
ipAddress = "unknown"; | ||
} | ||
|
||
#else | ||
private string GetIpAddress() | ||
{ | ||
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[_forwardHeaderKey].FirstOrDefault(); | ||
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress)); | ||
httpContext.Items.Add(IpAddressItemKey, ipAddressProperty); | ||
|
||
return !string.IsNullOrEmpty(ipAddress) | ||
? GetIpAddressFromProxy(ipAddress) | ||
: _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(); | ||
} | ||
#endif | ||
|
||
private string GetIpAddressFromProxy(string proxifiedIpList) | ||
{ | ||
var addresses = proxifiedIpList.Split(','); | ||
|
||
return addresses.Length == 0 ? string.Empty : addresses[0].Trim(); | ||
} | ||
logEvent.AddPropertyIfAbsent(ipAddressProperty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.