Skip to content

Commit c7f8654

Browse files
committed
Serilog 4 updates; unpublish enricher types accessible via extensions
1 parent 38e2792 commit c7f8654

18 files changed

+231
-254
lines changed

.idea/.idea.serilog-enrichers-environment/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.serilog-enrichers-environment/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.serilog-enrichers-environment/.idea/indexLayout.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.serilog-enrichers-environment/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Build.ps1

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,4 @@ foreach ($test in gci test/*.Tests) {
4646
Pop-Location
4747
}
4848

49-
foreach ($test in ls test/*.PerformanceTests) {
50-
Push-Location $test
51-
52-
echo "build: Building performance test project in $test"
53-
54-
& dotnet build -c Release
55-
if($LASTEXITCODE -ne 0) { exit 2 }
56-
57-
Pop-Location
58-
}
59-
6049
Pop-Location

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Serilog.Enrichers.Environment
22

3-
Enriches Serilog events with information from the process environment.
3+
Enriches Serilog events with information from the execution environment.
44

55
[![Build status](https://ci.appveyor.com/api/projects/status/yfbvbdxd5vwh6955?svg=true)](https://ci.appveyor.com/project/serilog/serilog-enrichers-environment) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Enrichers.Environment.svg?style=flat)](https://www.nuget.org/packages/Serilog.Enrichers.Environment/)
66

77
To use the enricher, first install the NuGet package:
88

99
```powershell
10-
Install-Package Serilog.Enrichers.Environment
10+
dotnet add package Serilog.Enrichers.Environment
1111
```
1212

1313
Then, apply the enricher to you `LoggerConfiguration`:
@@ -28,6 +28,6 @@ The package includes:
2828
* `WithMachineName()` - adds `MachineName` based on either `%COMPUTERNAME%` (Windows) or `$HOSTNAME` (macOS, Linux)
2929
* `WithEnvironmentUserName()` - adds `EnvironmentUserName` based on `USERNAME` and `USERDOMAIN` (if available)
3030
* `WithEnvironmentName()` - adds `EnvironmentName` based on `ASPNETCORE_ENVIRONMENT` or `DOTNET_ENVIRONMENT` (when both are available then 'ASPNETCORE_ENVIRONMENT' takes precedence, when none are available then the fallback value will be 'Production')
31-
* `WithEnvironmentVariable(string envVarName, string propertyName = null)` - adds an `<envVarName>` property to the log event with the value for the specified enviroment variable. Optional `<propertyName>` will override `<envVarName>` as propertyName.
31+
* `WithEnvironmentVariable(string envVarName, string propertyName = null)` - adds an `<envVarName>` property to the log event with the value for the specified environment variable. Optional `<propertyName>` will override `<envVarName>` as propertyName.
3232

3333
Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).

appveyor.yml

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
version: '{build}'
22
skip_tags: true
33
image: Visual Studio 2022
4-
configuration: Release
5-
install:
6-
- ps: mkdir -Force ".\build\" | Out-Null
7-
84
build_script:
9-
- ps: ./Build.ps1
5+
- pwsh: ./Build.ps1
106
test: off
117
artifacts:
12-
- path: artifacts/Serilog.*.nupkg
8+
- path: artifacts/Serilog.*.nupkg
139
deploy:
14-
- provider: NuGet
15-
api_key:
16-
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
17-
skip_symbols: true
18-
on:
19-
branch: /^(main|dev)$/
20-
- provider: GitHub
21-
auth_token:
22-
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
23-
artifact: /Serilog.*\.nupkg/
24-
tag: v$(appveyor_build_version)
25-
on:
26-
branch: main
10+
- provider: NuGet
11+
api_key:
12+
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
13+
skip_symbols: true
14+
on:
15+
branch: /^(main|dev)$/
16+
- provider: GitHub
17+
auth_token:
18+
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
19+
artifact: /Serilog.*\.nupkg/
20+
tag: v$(appveyor_build_version)
21+
on:
22+
branch: main
23+

serilog-enrichers-environment.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
99
ProjectSection(SolutionItems) = preProject
1010
Build.ps1 = Build.ps1
11-
NuGet.Config = NuGet.Config
1211
README.md = README.md
1312
assets\Serilog.snk = assets\Serilog.snk
13+
appveyor.yml = appveyor.yml
1414
EndProjectSection
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{406006F7-12FA-4A8A-ADC8-80E5338F5275}"

src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,28 @@
1515
using Serilog.Core;
1616
using Serilog.Events;
1717

18-
namespace Serilog.Enrichers
19-
{
20-
public abstract class CachedPropertyEnricher: ILogEventEnricher
21-
{
22-
private LogEventProperty? _cachedProperty { get; set; }
18+
namespace Serilog.Enrichers;
2319

24-
/// <summary>
25-
/// Enrich the log event.
26-
/// </summary>
27-
/// <param name="logEvent">The log event to enrich.</param>
28-
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
29-
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
30-
{
31-
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
32-
}
20+
abstract class CachedPropertyEnricher: ILogEventEnricher
21+
{
22+
LogEventProperty? CachedProperty { get; set; }
3323

34-
private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
35-
{
36-
// Don't care about thread-safety, in the worst case the field gets overwritten and one
37-
// property will be GCed
38-
_cachedProperty ??= CreateProperty(propertyFactory);
24+
/// <summary>
25+
/// Enrich the log event.
26+
/// </summary>
27+
/// <param name="logEvent">The log event to enrich.</param>
28+
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
29+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
30+
{
31+
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
32+
}
3933

40-
return _cachedProperty;
41-
}
42-
43-
protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
34+
LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
35+
{
36+
// Don't care about thread-safety, in the worst case the field gets overwritten and one
37+
// property will be GCed
38+
return CachedProperty ??= CreateProperty(propertyFactory);
4439
}
40+
41+
protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
4542
}

src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,33 @@
1515
using System;
1616
using Serilog.Core;
1717
using Serilog.Events;
18-
using System.Runtime.CompilerServices;
1918

20-
namespace Serilog.Enrichers
19+
namespace Serilog.Enrichers;
20+
21+
/// <summary>
22+
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
23+
/// </summary>
24+
class EnvironmentNameEnricher : CachedPropertyEnricher
2125
{
2226
/// <summary>
23-
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
27+
/// The property name added to enriched log events.
2428
/// </summary>
25-
public class EnvironmentNameEnricher : CachedPropertyEnricher
29+
const string EnvironmentNamePropertyName = "EnvironmentName";
30+
31+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
2632
{
27-
/// <summary>
28-
/// The property name added to enriched log events.
29-
/// </summary>
30-
public const string EnvironmentNamePropertyName = "EnvironmentName";
33+
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
3134

32-
// Qualify as uncommon-path
33-
[MethodImpl(MethodImplOptions.NoInlining)]
34-
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
35+
if (string.IsNullOrWhiteSpace(environmentName))
3536
{
36-
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
37-
38-
if (string.IsNullOrWhiteSpace(environmentName))
39-
{
40-
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
41-
}
42-
43-
if (string.IsNullOrWhiteSpace(environmentName))
44-
{
45-
environmentName = "Production";
46-
}
37+
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
38+
}
4739

48-
return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName);
40+
if (string.IsNullOrWhiteSpace(environmentName))
41+
{
42+
environmentName = "Production";
4943
}
44+
45+
return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName);
5046
}
51-
}
47+
}

src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,24 @@
1616
using Serilog.Core;
1717
using Serilog.Events;
1818

19-
namespace Serilog.Enrichers
19+
namespace Serilog.Enrichers;
20+
21+
/// <summary>
22+
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
23+
/// </summary>
24+
class EnvironmentUserNameEnricher : CachedPropertyEnricher
2025
{
2126
/// <summary>
22-
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
27+
/// The property name added to enriched log events.
2328
/// </summary>
24-
public class EnvironmentUserNameEnricher : CachedPropertyEnricher
25-
{
26-
/// <summary>
27-
/// The property name added to enriched log events.
28-
/// </summary>
29-
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName";
29+
const string EnvironmentUserNamePropertyName = "EnvironmentUserName";
3030

31-
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
32-
{
33-
#if ENV_USER_NAME
34-
var userDomainName = Environment.UserDomainName;
35-
var userName = Environment.UserName;
36-
#else
37-
var userDomainName = Environment.GetEnvironmentVariable("USERDOMAIN");
38-
var userName = Environment.GetEnvironmentVariable("USERNAME");
39-
#endif
40-
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
31+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
32+
{
33+
var userDomainName = Environment.UserDomainName;
34+
var userName = Environment.UserName;
35+
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
4136

42-
return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
43-
}
37+
return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
4438
}
45-
}
39+
}

src/Serilog.Enrichers.Environment/Enrichers/EnvironmentVariableEnricher.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,31 @@
1515
using System;
1616
using Serilog.Core;
1717
using Serilog.Events;
18-
using System.Runtime.CompilerServices;
1918

20-
namespace Serilog.Enrichers
19+
namespace Serilog.Enrichers;
20+
21+
/// <summary>
22+
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
23+
/// </summary>
24+
class EnvironmentVariableEnricher : CachedPropertyEnricher
2125
{
26+
readonly string _envVarName;
27+
2228
/// <summary>
23-
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
29+
/// The property name added to enriched log events.
2430
/// </summary>
25-
public class EnvironmentVariableEnricher : CachedPropertyEnricher
26-
{
27-
private readonly string _envVarName;
28-
29-
/// <summary>
30-
/// The property name added to enriched log events.
31-
/// </summary>
32-
public string EnvironmentVariablePropertyName { get; }
31+
string EnvironmentVariablePropertyName { get; }
3332

34-
public EnvironmentVariableEnricher(string envVarName, string? propertyName)
35-
{
36-
_envVarName = envVarName;
37-
EnvironmentVariablePropertyName = propertyName ?? envVarName;
38-
}
33+
public EnvironmentVariableEnricher(string envVarName, string? propertyName)
34+
{
35+
_envVarName = envVarName;
36+
EnvironmentVariablePropertyName = propertyName ?? envVarName;
37+
}
3938

40-
// Qualify as uncommon-path
41-
[MethodImpl(MethodImplOptions.NoInlining)]
42-
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
43-
{
44-
var environmentVariableValue = Environment.GetEnvironmentVariable(_envVarName);
39+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
40+
{
41+
var environmentVariableValue = Environment.GetEnvironmentVariable(_envVarName);
4542

46-
return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
47-
}
43+
return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
4844
}
49-
}
45+
}

src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,22 @@
1515
using System;
1616
using Serilog.Core;
1717
using Serilog.Events;
18-
using System.Runtime.CompilerServices;
1918

20-
#if NETSTANDARD1_3
21-
using System.Net;
22-
#endif
19+
namespace Serilog.Enrichers;
2320

24-
namespace Serilog.Enrichers
21+
/// <summary>
22+
/// Enriches log events with a MachineName property containing <see cref="Environment.MachineName"/>.
23+
/// </summary>
24+
class MachineNameEnricher : CachedPropertyEnricher
2525
{
2626
/// <summary>
27-
/// Enriches log events with a MachineName property containing <see cref="Environment.MachineName"/>.
27+
/// The property name added to enriched log events.
2828
/// </summary>
29-
public class MachineNameEnricher : CachedPropertyEnricher
30-
{
31-
/// <summary>
32-
/// The property name added to enriched log events.
33-
/// </summary>
34-
public const string MachineNamePropertyName = "MachineName";
29+
const string MachineNamePropertyName = "MachineName";
3530

36-
// Qualify as uncommon-path
37-
[MethodImpl(MethodImplOptions.NoInlining)]
38-
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
39-
{
40-
#if NETSTANDARD1_3
41-
var machineName = Dns.GetHostName();
42-
#else
43-
var machineName = Environment.MachineName;
44-
#endif
45-
return propertyFactory.CreateProperty(MachineNamePropertyName, machineName);
46-
}
31+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
32+
{
33+
var machineName = Environment.MachineName;
34+
return propertyFactory.CreateProperty(MachineNamePropertyName, machineName);
4735
}
48-
}
36+
}

0 commit comments

Comments
 (0)