Skip to content

Commit 644f6d4

Browse files
committed
Reuse LogEventProperty per request to reduce allocation
1 parent 89f53f5 commit 644f6d4

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientAgentEnricher.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace Serilog.Enrichers
1313
{
1414
public class ClientAgentEnricher : ILogEventEnricher
1515
{
16-
private const string IpAddressPropertyName = "ClientAgent";
16+
private const string ClientAgentPropertyName = "ClientAgent";
17+
private const string ClientAgentItemKey = "Serilog_ClientAgent";
18+
1719
private readonly IHttpContextAccessor _contextAccessor;
1820

1921
public ClientAgentEnricher() : this(new HttpContextAccessor())
@@ -27,17 +29,27 @@ internal ClientAgentEnricher(IHttpContextAccessor contextAccessor)
2729

2830
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
2931
{
30-
if (_contextAccessor.HttpContext == null)
32+
var httpContext = _contextAccessor.HttpContext;
33+
if (httpContext == null)
34+
return;
35+
36+
if (httpContext.Items[ClientAgentItemKey] is LogEventProperty logEventProperty)
37+
{
38+
logEvent.AddPropertyIfAbsent(logEventProperty);
3139
return;
40+
}
41+
3242
#if NETFULL
33-
var agentName = _contextAccessor.HttpContext.Request.Headers["User-Agent"];
43+
var agentName = httpContext.Request.Headers["User-Agent"];
3444
#else
35-
var agentName = _contextAccessor.HttpContext.Request.Headers["User-Agent"];
45+
var agentName = httpContext.Request.Headers["User-Agent"];
3646
#endif
3747

38-
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(agentName));
3948

40-
logEvent.AddPropertyIfAbsent(ipAddressProperty);
49+
var clientAgentProperty = new LogEventProperty(ClientAgentPropertyName, new ScalarValue(agentName));
50+
httpContext.Items.Add(ClientAgentItemKey, clientAgentProperty);
51+
52+
logEvent.AddPropertyIfAbsent(clientAgentProperty);
4153
}
4254
}
4355
}

src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Serilog.Enrichers
1616
public class ClientIpEnricher : ILogEventEnricher
1717
{
1818
private const string IpAddressPropertyName = "ClientIp";
19+
private const string IpAddresstItemKey = "Serilog_ClientIp";
20+
1921
private readonly IHttpContextAccessor _contextAccessor;
2022

2123
public ClientIpEnricher()
@@ -30,15 +32,23 @@ internal ClientIpEnricher(IHttpContextAccessor contextAccessor)
3032

3133
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
3234
{
33-
if (_contextAccessor.HttpContext == null)
35+
var httpContext = _contextAccessor.HttpContext;
36+
if (httpContext == null)
3437
return;
3538

39+
if (httpContext.Items[IpAddresstItemKey] is LogEventProperty logEventProperty)
40+
{
41+
logEvent.AddPropertyIfAbsent(logEventProperty);
42+
return;
43+
}
44+
3645
var ipAddress = GetIpAddress();
3746

3847
if (string.IsNullOrWhiteSpace(ipAddress))
3948
ipAddress = "unknown";
4049

4150
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress));
51+
httpContext.Items.Add(IpAddresstItemKey, ipAddressProperty);
4252

4353
logEvent.AddPropertyIfAbsent(ipAddressProperty);
4454
}

0 commit comments

Comments
 (0)