Skip to content

Commit a72051d

Browse files
authored
Merge pull request #38 from serilog-contrib/feature/drop-dotnet-framework
Drop support for .NET Framework.
2 parents 0dda9a6 + c4f781f commit a72051d

10 files changed

+76
-208
lines changed

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ or in `appsettings.json` file:
4646
---
4747

4848
### ClientIp
49-
For `ClientIp` enricher you can configure the `x-forwarded-for` header if the proxy server uses a different header to forward the IP address.
49+
`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.
5050
```csharp
5151
Log.Logger = new LoggerConfiguration()
5252
.Enrich.WithClientIp(headerName: "CF-Connecting-IP")
@@ -60,10 +60,7 @@ or
6060
"Using": [ "Serilog.Enrichers.ClientInfo" ],
6161
"Enrich": [
6262
{
63-
"Name": "WithClientIp",
64-
"Args": {
65-
"headerName": "CF-Connecting-IP"
66-
}
63+
"Name": "WithClientIp"
6764
}
6865
],
6966
}

Serilog.Enrichers.ClientInfo.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ Global
5353
GlobalSection(ExtensibilityGlobals) = postSolution
5454
SolutionGuid = {785F0E90-8DC5-4003-AD7A-33DE806F3B3A}
5555
EndGlobalSection
56-
EndGlobal
56+
EndGlobal

src/Serilog.Enrichers.ClientInfo/Accessors/HttpContextAccessor.cs

-16
This file was deleted.

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using Serilog.Core;
1+
using Microsoft.AspNetCore.Http;
2+
using Serilog.Core;
23
using Serilog.Events;
34

4-
#if NETFULL
5-
6-
using Serilog.Enrichers.ClientInfo.Accessors;
7-
8-
#else
9-
using Microsoft.AspNetCore.Http;
10-
#endif
11-
125
namespace Serilog.Enrichers;
136

147
/// <inheritdoc/>
@@ -19,6 +12,11 @@ public class ClientHeaderEnricher : ILogEventEnricher
1912
private readonly string _headerKey;
2013
private readonly IHttpContextAccessor _contextAccessor;
2114

15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ClientHeaderEnricher"/> class.
17+
/// </summary>
18+
/// <param name="headerKey">The key of the header.</param>
19+
/// <param name="propertyName">The name of the property.</param>
2220
public ClientHeaderEnricher(string headerKey, string propertyName)
2321
: this(headerKey, propertyName, new HttpContextAccessor())
2422
{
@@ -27,7 +25,7 @@ public ClientHeaderEnricher(string headerKey, string propertyName)
2725
internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor)
2826
{
2927
_headerKey = headerKey;
30-
_propertyName = string.IsNullOrWhiteSpace(propertyName)
28+
_propertyName = string.IsNullOrWhiteSpace(propertyName)
3129
? headerKey.Replace("-", "")
3230
: propertyName;
3331
_clientHeaderItemKey = $"Serilog_{headerKey}";
@@ -43,7 +41,9 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
4341
{
4442
var httpContext = _contextAccessor.HttpContext;
4543
if (httpContext == null)
44+
{
4645
return;
46+
}
4747

4848
if (httpContext.Items[_clientHeaderItemKey] is LogEventProperty logEventProperty)
4949
{
@@ -59,4 +59,4 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
5959

6060
logEvent.AddPropertyIfAbsent(logProperty);
6161
}
62-
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,55 @@
1-
using Serilog.Core;
1+
using Microsoft.AspNetCore.Http;
2+
using Serilog.Core;
23
using Serilog.Events;
3-
using System.Linq;
44
using System.Runtime.CompilerServices;
55

6-
#if NETFULL
6+
[assembly: InternalsVisibleTo("Serilog.Enrichers.ClientInfo.Tests")]
77

8-
using Serilog.Enrichers.ClientInfo.Accessors;
8+
namespace Serilog.Enrichers;
99

10-
#else
11-
using Microsoft.AspNetCore.Http;
12-
#endif
10+
public class ClientIpEnricher : ILogEventEnricher
11+
{
12+
private const string IpAddressPropertyName = "ClientIp";
13+
private const string IpAddressItemKey = "Serilog_ClientIp";
1314

14-
[assembly: InternalsVisibleTo("Serilog.Enrichers.ClientInfo.Tests")]
15+
private readonly IHttpContextAccessor _contextAccessor;
1516

16-
namespace Serilog.Enrichers
17-
{
18-
public class ClientIpEnricher : ILogEventEnricher
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="ClientIpEnricher"/> class.
19+
/// </summary>
20+
public ClientIpEnricher() : this(new HttpContextAccessor())
1921
{
20-
private const string IpAddressPropertyName = "ClientIp";
21-
private const string IpAddressItemKey = "Serilog_ClientIp";
22-
private readonly string _forwardHeaderKey;
22+
}
2323

24-
private readonly IHttpContextAccessor _contextAccessor;
24+
internal ClientIpEnricher(IHttpContextAccessor contextAccessor)
25+
{
26+
_contextAccessor = contextAccessor;
27+
}
2528

26-
public ClientIpEnricher(string forwardHeaderKey)
27-
: this(forwardHeaderKey, new HttpContextAccessor())
29+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
30+
{
31+
var httpContext = _contextAccessor.HttpContext;
32+
if (httpContext == null)
2833
{
34+
return;
2935
}
3036

31-
internal ClientIpEnricher(string forwardHeaderKey, IHttpContextAccessor contextAccessor)
37+
if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty)
3238
{
33-
_forwardHeaderKey = forwardHeaderKey;
34-
_contextAccessor = contextAccessor;
39+
logEvent.AddPropertyIfAbsent(logEventProperty);
40+
return;
3541
}
3642

37-
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
38-
{
39-
var httpContext = _contextAccessor.HttpContext;
40-
if (httpContext == null)
41-
return;
42-
43-
if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty)
44-
{
45-
logEvent.AddPropertyIfAbsent(logEventProperty);
46-
return;
47-
}
48-
49-
var ipAddress = GetIpAddress();
50-
51-
if (string.IsNullOrWhiteSpace(ipAddress))
52-
ipAddress = "unknown";
43+
var ipAddress = _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
5344

54-
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress));
55-
httpContext.Items.Add(IpAddressItemKey, ipAddressProperty);
56-
57-
logEvent.AddPropertyIfAbsent(ipAddressProperty);
58-
}
59-
60-
#if NETFULL
61-
62-
private string GetIpAddress()
45+
if (string.IsNullOrWhiteSpace(ipAddress))
6346
{
64-
var ipAddress = _contextAccessor.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
65-
66-
return !string.IsNullOrEmpty(ipAddress)
67-
? GetIpAddressFromProxy(ipAddress)
68-
: _contextAccessor.HttpContext.Request.ServerVariables["REMOTE_ADDR"];
47+
ipAddress = "unknown";
6948
}
7049

71-
#else
72-
private string GetIpAddress()
73-
{
74-
var ipAddress = _contextAccessor.HttpContext?.Request?.Headers[_forwardHeaderKey].FirstOrDefault();
50+
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress));
51+
httpContext.Items.Add(IpAddressItemKey, ipAddressProperty);
7552

76-
return !string.IsNullOrEmpty(ipAddress)
77-
? GetIpAddressFromProxy(ipAddress)
78-
: _contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
79-
}
80-
#endif
81-
82-
private string GetIpAddressFromProxy(string proxifiedIpList)
83-
{
84-
var addresses = proxifiedIpList.Split(',');
85-
86-
return addresses.Length == 0 ? string.Empty : addresses[0].Trim();
87-
}
53+
logEvent.AddPropertyIfAbsent(ipAddressProperty);
8854
}
8955
}

src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
using Serilog.Core;
1+
using Microsoft.AspNetCore.Http;
2+
using Serilog.Core;
23
using Serilog.Events;
34
using System;
45

5-
#if NETFULL
6-
7-
using Serilog.Enrichers.ClientInfo.Accessors;
8-
9-
#else
10-
using Microsoft.AspNetCore.Http;
11-
#endif
12-
136
namespace Serilog.Enrichers;
147

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

17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="CorrelationIdEnricher"/> class.
19+
/// </summary>
20+
/// <param name="headerKey">
21+
/// The header key used to retrieve the correlation ID from the HTTP request or response headers.
22+
/// </param>
23+
/// <param name="addValueIfHeaderAbsence">
24+
/// Determines whether to add a new correlation ID value if the header is absent.
25+
/// </param>
2426
public CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence)
2527
: this(headerKey, addValueIfHeaderAbsence, new HttpContextAccessor())
2628
{
@@ -33,6 +35,7 @@ internal CorrelationIdEnricher(string headerKey, bool addValueIfHeaderAbsence, I
3335
_contextAccessor = contextAccessor;
3436
}
3537

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

5053
var requestHeader = httpContext.Request.Headers[_headerKey];
5154
var responseHeader = httpContext.Response.Headers[_headerKey];
52-
55+
5356
string correlationId;
54-
57+
5558
if (!string.IsNullOrWhiteSpace(requestHeader))
5659
{
5760
correlationId = requestHeader;

src/Serilog.Enrichers.ClientInfo/Extensions/ClientInfoLoggerConfigurationExtensions.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
using Serilog.Configuration;
1+
using Microsoft.AspNetCore.Http;
2+
using Serilog.Configuration;
23
using Serilog.Enrichers;
34
using System;
4-
using System.Web;
5-
6-
#if NETFULL
7-
8-
using Serilog.Enrichers.ClientInfo.Accessors;
9-
10-
#else
11-
using Microsoft.AspNetCore.Http;
12-
#endif
135

146
namespace Serilog;
157

@@ -38,7 +30,7 @@ public static LoggerConfiguration WithClientIp(
3830
throw new ArgumentNullException(nameof(enrichmentConfiguration));
3931
}
4032

41-
return enrichmentConfiguration.With(new ClientIpEnricher(headerName));
33+
return enrichmentConfiguration.With<ClientIpEnricher>();
4234
}
4335

4436
/// <summary>

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

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
55
<AssemblyName>Serilog.Enrichers.ClientInfo</AssemblyName>
66
<RootNamespace>Serilog</RootNamespace>
77
<LangVersion>latest</LangVersion>
@@ -12,30 +12,11 @@
1212
<Version>2.0.3</Version>
1313
</PropertyGroup>
1414

15-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
16-
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
17-
</PropertyGroup>
18-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
19-
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_1</DefineConstants>
20-
</PropertyGroup>
21-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net462'">
22-
<DefineConstants>NET45;NETFULL</DefineConstants>
23-
</PropertyGroup>
15+
<ItemGroup>
16+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
17+
</ItemGroup>
2418

25-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
26-
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.22" />
27-
<PackageReference Include="Serilog" Version="2.7.1" />
28-
</ItemGroup>
29-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
30-
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.22" />
31-
<PackageReference Include="Serilog" Version="2.9.0" />
32-
</ItemGroup>
33-
<ItemGroup Condition=" '$(TargetFramework)' == 'net462'">
34-
<Reference Include="System.Web" />
35-
<PackageReference Include="Serilog" Version="2.4.0" />
36-
</ItemGroup>
37-
<ItemGroup Condition=" '$(TargetFramework)' != 'net462' and '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netstandard2.1'">
38-
<FrameworkReference Include="Microsoft.AspNetCore.App" />
19+
<ItemGroup>
3920
<PackageReference Include="Serilog" Version="2.9.0" />
4021
</ItemGroup>
4122

0 commit comments

Comments
 (0)