Skip to content

Commit 880a21a

Browse files
authored
Merge pull request #63 from nblumhardt/serilog-4
Serilog 4 updates; unpublish enricher types that are already accessible via extensions
2 parents 38e2792 + 2b97f21 commit 880a21a

19 files changed

+247
-267
lines changed

Diff for: .idea/.idea.serilog-enrichers-environment/.idea/.gitignore

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/.idea.serilog-enrichers-environment/.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/.idea.serilog-enrichers-environment/.idea/indexLayout.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/.idea.serilog-enrichers-environment/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Build.ps1

+12-24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ echo "build: Build started"
33
Push-Location $PSScriptRoot
44

55
if(Test-Path .\artifacts) {
6-
echo "build: Cleaning ./artifacts"
7-
Remove-Item ./artifacts -Force -Recurse
6+
echo "build: Cleaning .\artifacts"
7+
Remove-Item .\artifacts -Force -Recurse
88
}
99

1010
& dotnet restore --no-cache
@@ -16,45 +16,33 @@ $commitHash = $(git rev-parse --short HEAD)
1616
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
1717

1818
echo "build: Package version suffix is $suffix"
19-
echo "build: Build version suffix is $buildSuffix"
19+
echo "build: Build version suffix is $buildSuffix"
2020

21-
foreach ($src in gci src/*) {
21+
foreach ($src in ls src/*) {
2222
Push-Location $src
2323

2424
echo "build: Packaging project in $src"
2525

26-
& dotnet build -c Release --version-suffix=$buildSuffix
27-
28-
if($suffix) {
29-
& dotnet pack -c Release --include-source --no-build -o ../../artifacts --version-suffix=$suffix
26+
& dotnet build -c Release --version-suffix=$buildSuffix -p:ContinuousIntegrationBuild=true
27+
if ($suffix) {
28+
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix --no-build
3029
} else {
31-
& dotnet pack -c Release --include-source --no-build -o ../../artifacts
30+
& dotnet pack -c Release -o ..\..\artifacts --no-build
3231
}
33-
if($LASTEXITCODE -ne 0) { exit 1 }
32+
if($LASTEXITCODE -ne 0) { throw "build failed" }
3433

3534
Pop-Location
3635
}
3736

38-
foreach ($test in gci test/*.Tests) {
37+
foreach ($test in ls test/*.Tests) {
3938
Push-Location $test
4039

4140
echo "build: Testing project in $test"
4241

4342
& dotnet test -c Release
44-
if($LASTEXITCODE -ne 0) { exit 3 }
45-
46-
Pop-Location
47-
}
48-
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 }
43+
if($LASTEXITCODE -ne 0) { throw "tests failed" }
5644

5745
Pop-Location
5846
}
5947

60-
Pop-Location
48+
Pop-Location

Diff for: README.md

+3-3
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).

Diff for: appveyor.yml

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
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
9+
- path: artifacts/Serilog.*.snupkg
1310
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
11+
- provider: NuGet
12+
api_key:
13+
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
14+
skip_symbols: true
15+
on:
16+
branch: /^(main|dev)$/
17+
- provider: GitHub
18+
auth_token:
19+
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
20+
artifacts:
21+
/Serilog.*\.nupkg/
22+
/Serilog.*\.snupkg/
23+
tag: v$(appveyor_build_version)
24+
on:
25+
branch: main
26+

Diff for: assets/serilog-enricher-nuget.png

21.1 KB
Loading

Diff for: serilog-enrichers-environment.sln

+1-1
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}"

Diff for: src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs

+20-23
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
}

Diff for: src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs

+20-24
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+
sealed 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+
}

Diff for: src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs

+15-21
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+
sealed 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+
}

0 commit comments

Comments
 (0)