Skip to content

Commit eb643ec

Browse files
committed
Initial commit.
1 parent 1814c21 commit eb643ec

File tree

7 files changed

+213
-0
lines changed

7 files changed

+213
-0
lines changed

Serilog.Enrichers.ClientInfo.sln

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29411.108
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{84342DE9-9E61-4802-9E77-305CFFD0D2B5}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F5A0A932-DD02-47C1-B81E-3F34F879FEEF}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClientInfo", "src\Serilog.Enrichers.ClientInfo\Serilog.Enrichers.ClientInfo.csproj", "{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
GlobalSection(SolutionProperties) = preSolution
24+
HideSolutionNode = FALSE
25+
EndGlobalSection
26+
GlobalSection(NestedProjects) = preSolution
27+
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2} = {84342DE9-9E61-4802-9E77-305CFFD0D2B5}
28+
EndGlobalSection
29+
GlobalSection(ExtensibilityGlobals) = postSolution
30+
SolutionGuid = {785F0E90-8DC5-4003-AD7A-33DE806F3B3A}
31+
EndGlobalSection
32+
EndGlobal
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Enricher/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if NETFULL
2+
using System.Web;
3+
4+
namespace Serilog.Enrichers.ClientInfo.Accessors
5+
{
6+
public interface IHttpContextAccessor
7+
{
8+
HttpContext HttpContext { get; }
9+
}
10+
11+
internal class HttpContextAccessor : IHttpContextAccessor
12+
{
13+
public HttpContext HttpContext => HttpContext.Current;
14+
}
15+
}
16+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Serilog.Core;
2+
using Serilog.Events;
3+
4+
#if NETFULL
5+
6+
using Serilog.Enrichers.ClientInfo.Accessors;
7+
8+
#else
9+
using Microsoft.AspNetCore.Http;
10+
#endif
11+
12+
namespace Serilog.Enrichers
13+
{
14+
public class ClientAgentEnricher : ILogEventEnricher
15+
{
16+
private const string IpAddressPropertyName = "ClientAgent";
17+
private readonly IHttpContextAccessor _contextAccessor;
18+
19+
public ClientAgentEnricher() : this(new HttpContextAccessor())
20+
{
21+
}
22+
23+
internal ClientAgentEnricher(IHttpContextAccessor contextAccessor)
24+
{
25+
_contextAccessor = contextAccessor;
26+
}
27+
28+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
29+
{
30+
if (_contextAccessor.HttpContext == null)
31+
return;
32+
33+
var agentName = _contextAccessor.HttpContext.Request.Headers["User-Agent"];
34+
35+
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(agentName ?? "unknown"));
36+
37+
logEvent.AddPropertyIfAbsent(ipAddressProperty);
38+
}
39+
}
40+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Serilog.Core;
2+
using Serilog.Events;
3+
4+
#if NETFULL
5+
6+
using Serilog.Enrichers.ClientInfo.Accessors;
7+
8+
#else
9+
using Microsoft.AspNetCore.Http;
10+
#endif
11+
12+
namespace Serilog.Enrichers
13+
{
14+
public class ClientIpEnricher : ILogEventEnricher
15+
{
16+
private const string IpAddressPropertyName = "ClientIp";
17+
private readonly IHttpContextAccessor _contextAccessor;
18+
19+
public ClientIpEnricher()
20+
{
21+
_contextAccessor = new HttpContextAccessor();
22+
}
23+
24+
internal ClientIpEnricher(IHttpContextAccessor contextAccessor)
25+
{
26+
_contextAccessor = contextAccessor;
27+
}
28+
29+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
30+
{
31+
if (_contextAccessor.HttpContext == null)
32+
return;
33+
34+
#if NETFULL
35+
36+
var ipAddress = GetIpAddress();
37+
38+
#else
39+
var ipAddress = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
40+
#endif
41+
42+
if (string.IsNullOrWhiteSpace(ipAddress))
43+
ipAddress = "unknown";
44+
45+
var ipAddressProperty = new LogEventProperty(IpAddressPropertyName, new ScalarValue(ipAddress));
46+
47+
logEvent.AddPropertyIfAbsent(ipAddressProperty);
48+
}
49+
50+
private string GetIpAddress()
51+
{
52+
var ipAddress = _contextAccessor.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
53+
54+
if (!string.IsNullOrEmpty(ipAddress))
55+
{
56+
var addresses = ipAddress.Split(',');
57+
if (addresses.Length != 0)
58+
return addresses[0];
59+
}
60+
61+
return _contextAccessor.HttpContext.Request.ServerVariables["REMOTE_ADDR"];
62+
}
63+
}
64+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Serilog.Configuration;
2+
using Serilog.Enrichers;
3+
using System;
4+
5+
namespace Serilog
6+
{
7+
public static class ClientInfoLoggerConfigurationExtensions
8+
{
9+
public static LoggerConfiguration WithClientIp(this LoggerEnrichmentConfiguration enrichmentConfiguration)
10+
{
11+
if (enrichmentConfiguration == null)
12+
throw new ArgumentNullException(nameof(enrichmentConfiguration));
13+
14+
return enrichmentConfiguration.With<ClientIpEnricher>();
15+
}
16+
17+
public static LoggerConfiguration WithClientAgent(this LoggerEnrichmentConfiguration enrichmentConfiguration)
18+
{
19+
if (enrichmentConfiguration == null)
20+
throw new ArgumentNullException(nameof(enrichmentConfiguration));
21+
22+
return enrichmentConfiguration.With<ClientAgentEnricher>();
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<AssemblyName>Serilog.Enrichers.CorrelationId</AssemblyName>
5+
<RootNamespace>Serilog</RootNamespace>
6+
<TargetFrameworks>net452;netstandard2.0;netstandard2.1</TargetFrameworks>
7+
<LangVersion>7.3</LangVersion>
8+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
12+
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
15+
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_1</DefineConstants>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net452'">
18+
<DefineConstants>NET45;NETFULL</DefineConstants>
19+
</PropertyGroup>
20+
21+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
22+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
23+
<PackageReference Include="Serilog" Version="2.9.0" />
24+
</ItemGroup>
25+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
26+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
27+
<PackageReference Include="Serilog" Version="2.9.0" />
28+
</ItemGroup>
29+
<ItemGroup Condition=" '$(TargetFramework)' == 'net452'">
30+
<Reference Include="System.Web" />
31+
<PackageReference Include="Serilog" Version="2.9.0" />
32+
</ItemGroup>
33+
</Project>

0 commit comments

Comments
 (0)