Skip to content

Commit 0199aa5

Browse files
committed
Add sample project.
1 parent d9984e0 commit 0199aa5

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

Serilog.Enrichers.ClientInfo.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{931596
1919
Serilog.Enrichers.ClientInfo.nuspec = Serilog.Enrichers.ClientInfo.nuspec
2020
EndProjectSection
2121
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{A170BD74-8542-4880-BC6F-283D03C6AA6D}"
23+
EndProject
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApp", "sample\SampleWebApp\SampleWebApp.csproj", "{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455}"
25+
EndProject
2226
Global
2327
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2428
Debug|Any CPU = Debug|Any CPU
@@ -33,13 +37,18 @@ Global
3337
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
3438
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
3539
{A6AFC898-47F8-4F6D-9391-C79689885B7A}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455}.Release|Any CPU.Build.0 = Release|Any CPU
3644
EndGlobalSection
3745
GlobalSection(SolutionProperties) = preSolution
3846
HideSolutionNode = FALSE
3947
EndGlobalSection
4048
GlobalSection(NestedProjects) = preSolution
4149
{232EFC21-2E8D-44B0-8506-C0DCE122AAA2} = {84342DE9-9E61-4802-9E77-305CFFD0D2B5}
4250
{A6AFC898-47F8-4F6D-9391-C79689885B7A} = {F5A0A932-DD02-47C1-B81E-3F34F879FEEF}
51+
{3C3C0104-969D-4FA3-B5DE-8A8A0A91E455} = {A170BD74-8542-4880-BC6F-283D03C6AA6D}
4352
EndGlobalSection
4453
GlobalSection(ExtensibilityGlobals) = postSolution
4554
SolutionGuid = {785F0E90-8DC5-4003-AD7A-33DE806F3B3A}

sample/SampleWebApp/Program.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Serilog;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container. Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
6+
builder.Services.AddEndpointsApiExplorer();
7+
builder.Services.AddSwaggerGen();
8+
builder.Services.AddHttpContextAccessor();
9+
10+
builder.Host.UseSerilog((context, services, configuration) => configuration
11+
.ReadFrom.Configuration(context.Configuration)
12+
.ReadFrom.Services(services)
13+
.Enrich.FromLogContext());
14+
15+
var app = builder.Build();
16+
17+
// Configure the HTTP request pipeline.
18+
if (app.Environment.IsDevelopment())
19+
{
20+
app.UseSwagger();
21+
app.UseSwaggerUI();
22+
}
23+
24+
var summaries = new[]
25+
{
26+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
27+
};
28+
29+
app.MapGet("/weatherforecast", () =>
30+
{
31+
var forecast = Enumerable.Range(1, 5).Select(index =>
32+
new WeatherForecast
33+
(
34+
DateTime.Now.AddDays(index),
35+
Random.Shared.Next(-20, 55),
36+
summaries[Random.Shared.Next(summaries.Length)]
37+
))
38+
.ToArray();
39+
return forecast;
40+
})
41+
.WithName("GetWeatherForecast");
42+
43+
app.Run();
44+
45+
internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
46+
{
47+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
48+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
11+
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="2.0.0" />
12+
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
13+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
14+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"AllowedHosts": "*",
3+
"Serilog": {
4+
"Using": [ "Serilog.Enrichers.ClientInfo" ],
5+
"MinimumLevel": "Information",
6+
"WriteTo": [
7+
{
8+
"Name": "Console",
9+
"Args": {
10+
"outputTemplate": "[{Timestamp:HH:mm:ss}] {Level:u3} Clint IP:{ClientIp} CorrelationId:{CorrelationId} Client Agent:{UserAgent} {Message:lj}{NewLine}{Exception}"
11+
}
12+
},
13+
{
14+
"Name": "File",
15+
"Args": {
16+
"path": "log.txt",
17+
"rollingInterval": "Day",
18+
"outputTemplate": "[{Timestamp:HH:mm:ss}] {Level:u3} Clint IP:{ClientIp} CorrelationId:{CorrelationId} Client Agent:{UserAgent} {Message:lj}{NewLine}{Exception}"
19+
}
20+
}
21+
],
22+
"Enrich": [
23+
"FromLogContext",
24+
"WithClientIp",
25+
//{
26+
// "Name": "WithClientIp",
27+
// "Args": {
28+
// "xForwardHeaderName": "CF-Connecting-IP"
29+
// }
30+
//},
31+
{
32+
"Name": "WithCorrelationId",
33+
"Args": {
34+
"addValueIfHeaderAbsence": true
35+
}
36+
},
37+
{
38+
"Name": "WithRequestHeader",
39+
"Args": {
40+
"headerName": "User-Agent"
41+
}
42+
}
43+
]
44+
}
45+
}

test/Serilog.Enrichers.ClientInfo.Tests/ClientHeaderEnricherTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
4848
{
4949
// Arrange
5050
var headerKey1 = "Header1";
51-
var headerKey2 = "Header-2";
51+
var headerKey2 = "User-Agent";
5252
var headerValue1 = Guid.NewGuid().ToString();
5353
var headerValue2 = Guid.NewGuid().ToString();
5454
_contextAccessor.HttpContext.Request.Headers.Add(headerKey1, headerValue1);

0 commit comments

Comments
 (0)