Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for .NET Framework. #38

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ or in `appsettings.json` file:
---

### ClientIp
For `ClientIp` enricher you can configure the `x-forwarded-for` header if the proxy server uses a different header to forward the IP address.
`ClientIp` enricher reads client IP from `HttpContext.Connection.RemoteIpAddress`. Since version 2.1, for [security reasons](https://nvd.nist.gov/vuln/detail/CVE-2023-22474), it no longer reads the `x-forwarded-for` header. To handle forwarded headers, configure [ForwardedHeadersOptions](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-7.0#forwarded-headers-middleware-order). If you still want to log `x-forwarded-header`, you can use the `RequestHeader`` enricher.
```csharp
Log.Logger = new LoggerConfiguration()
.Enrich.WithClientIp(headerName: "CF-Connecting-IP")
Expand All @@ -60,10 +60,7 @@ or
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
{
"Name": "WithClientIp",
"Args": {
"headerName": "CF-Connecting-IP"
}
"Name": "WithClientIp"
}
],
}
Expand Down
2 changes: 1 addition & 1 deletion Serilog.Enrichers.ClientInfo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {785F0E90-8DC5-4003-AD7A-33DE806F3B3A}
EndGlobalSection
EndGlobal
EndGlobal
16 changes: 0 additions & 16 deletions src/Serilog.Enrichers.ClientInfo/Accessors/HttpContextAccessor.cs

This file was deleted.

22 changes: 11 additions & 11 deletions src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using Serilog.Core;
using Microsoft.AspNetCore.Http;
using Serilog.Core;
using Serilog.Events;

#if NETFULL

using Serilog.Enrichers.ClientInfo.Accessors;

#else
using Microsoft.AspNetCore.Http;
#endif

namespace Serilog.Enrichers;

/// <inheritdoc/>
Expand All @@ -19,6 +12,11 @@
private readonly string _headerKey;
private readonly IHttpContextAccessor _contextAccessor;

/// <summary>
/// Initializes a new instance of the <see cref="ClientHeaderEnricher"/> class.
/// </summary>
/// <param name="headerKey">The key of the header.</param>
/// <param name="propertyName">The name of the property.</param>
public ClientHeaderEnricher(string headerKey, string propertyName)
: this(headerKey, propertyName, new HttpContextAccessor())
{
Expand All @@ -27,7 +25,7 @@
internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor)
{
_headerKey = headerKey;
_propertyName = string.IsNullOrWhiteSpace(propertyName)
_propertyName = string.IsNullOrWhiteSpace(propertyName)
? headerKey.Replace("-", "")
: propertyName;
_clientHeaderItemKey = $"Serilog_{headerKey}";
Expand All @@ -39,11 +37,13 @@
_contextAccessor = contextAccessor;
}

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)

Check warning on line 40 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'

Check warning on line 40 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'

Check warning on line 40 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'
{
var httpContext = _contextAccessor.HttpContext;
if (httpContext == null)
{
return;
}

if (httpContext.Items[_clientHeaderItemKey] is LogEventProperty logEventProperty)
{
Expand All @@ -59,4 +59,4 @@

logEvent.AddPropertyIfAbsent(logProperty);
}
}
}
98 changes: 32 additions & 66 deletions src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs
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

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher'

Check warning on line 10 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher'

Check warning on line 10 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher'
{
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

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'

Check warning on line 29 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'

Check warning on line 29 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientIpEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'
{
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);
}
}
25 changes: 14 additions & 11 deletions src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
using Serilog.Core;
using Microsoft.AspNetCore.Http;
using Serilog.Core;
using Serilog.Events;
using System;

#if NETFULL

using Serilog.Enrichers.ClientInfo.Accessors;

#else
using Microsoft.AspNetCore.Http;
#endif

namespace Serilog.Enrichers;

/// <inheritdoc/>
Expand All @@ -21,6 +14,15 @@ public class CorrelationIdEnricher : ILogEventEnricher
private readonly bool _addValueIfHeaderAbsence;
private readonly IHttpContextAccessor _contextAccessor;

/// <summary>
/// Initializes a new instance of the <see cref="CorrelationIdEnricher"/> class.
/// </summary>
/// <param name="headerKey">
/// The header key used to retrieve the correlation ID from the HTTP request or response headers.
/// </param>
/// <param name="addValueIfHeaderAbsence">
/// Determines whether to add a new correlation ID value if the header is absent.
/// </param>
public CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence)
: this(headerKey, addValueIfHeaderAbsence, new HttpContextAccessor())
{
Expand All @@ -33,6 +35,7 @@ internal CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence, I
_contextAccessor = contextAccessor;
}

/// <inheritdoc/>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var httpContext = _contextAccessor.HttpContext;
Expand All @@ -49,9 +52,9 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)

var requestHeader = httpContext.Request.Headers[_headerKey];
var responseHeader = httpContext.Response.Headers[_headerKey];

string correlationId;

if (!string.IsNullOrWhiteSpace(requestHeader))
{
correlationId = requestHeader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using Serilog.Configuration;
using Microsoft.AspNetCore.Http;
using Serilog.Configuration;
using Serilog.Enrichers;
using System;
using System.Web;

#if NETFULL

using Serilog.Enrichers.ClientInfo.Accessors;

#else
using Microsoft.AspNetCore.Http;
#endif

namespace Serilog;

Expand Down Expand Up @@ -38,7 +30,7 @@ public static LoggerConfiguration WithClientIp(
throw new ArgumentNullException(nameof(enrichmentConfiguration));
}

return enrichmentConfiguration.With(new ClientIpEnricher(headerName));
return enrichmentConfiguration.With<ClientIpEnricher>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<AssemblyName>Serilog.Enrichers.ClientInfo</AssemblyName>
<RootNamespace>Serilog</RootNamespace>
<LangVersion>latest</LangVersion>
Expand All @@ -12,30 +12,11 @@
<Version>2.0.3</Version>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_1</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net462'">
<DefineConstants>NET45;NETFULL</DefineConstants>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.22" />
<PackageReference Include="Serilog" Version="2.7.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.22" />
<PackageReference Include="Serilog" Version="2.9.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462'">
<Reference Include="System.Web" />
<PackageReference Include="Serilog" Version="2.4.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'net462' and '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
</ItemGroup>

Expand Down
Loading
Loading