Skip to content

Commit db174d1

Browse files
authored
Merge pull request #52 from serilog/dev
2.3.0 Release
2 parents 9ba7457 + 34d133c commit db174d1

11 files changed

+215
-90
lines changed

Diff for: Build.ps1

+24-15
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,56 @@ 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
1111

1212
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
1313
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
14-
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"]
15+
$commitHash = $(git rev-parse --short HEAD)
16+
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
1517

16-
echo "build: Version suffix is $suffix"
18+
echo "build: Package version suffix is $suffix"
19+
echo "build: Build version suffix is $buildSuffix"
1720

18-
foreach ($src in ls src/*) {
21+
foreach ($src in gci src/*) {
1922
Push-Location $src
2023

2124
echo "build: Packaging project in $src"
2225

23-
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
24-
if($LASTEXITCODE -ne 0) { exit 1 }
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
30+
} else {
31+
& dotnet pack -c Release --include-source --no-build -o ../../artifacts
32+
}
33+
if($LASTEXITCODE -ne 0) { exit 1 }
2534

2635
Pop-Location
2736
}
2837

29-
foreach ($test in ls test/*.PerformanceTests) {
38+
foreach ($test in gci test/*.Tests) {
3039
Push-Location $test
3140

32-
echo "build: Building performance test project in $test"
41+
echo "build: Testing project in $test"
3342

34-
& dotnet build -c Release
35-
if($LASTEXITCODE -ne 0) { exit 2 }
43+
& dotnet test -c Release
44+
if($LASTEXITCODE -ne 0) { exit 3 }
3645

3746
Pop-Location
3847
}
3948

40-
foreach ($test in ls test/*.Tests) {
49+
foreach ($test in ls test/*.PerformanceTests) {
4150
Push-Location $test
4251

43-
echo "build: Testing project in $test"
52+
echo "build: Building performance test project in $test"
4453

45-
& dotnet test -c Release
46-
if($LASTEXITCODE -ne 0) { exit 3 }
54+
& dotnet build -c Release
55+
if($LASTEXITCODE -ne 0) { exit 2 }
4756

4857
Pop-Location
4958
}

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +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.
3132

3233
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2019
3+
image: Visual Studio 2022
44
configuration: Release
55
install:
66
- ps: mkdir -Force ".\build\" | Out-Null
@@ -13,10 +13,10 @@ artifacts:
1313
deploy:
1414
- provider: NuGet
1515
api_key:
16-
secure: 9B24CFy1l5KYjxsp8AXPx6ANkDI3KKXSqi18nCUk1kyj0mClwfNbNj0Tna4+gUC5
16+
secure: dw2kT1B1gpFLOE7ZHhExKgcspS+tf5maUq0jEdH17YyHjUxQpemGcwvbe4nuf/Ic
1717
skip_symbols: true
1818
on:
19-
branch: /^(master|dev)$/
19+
branch: /^(main|dev)$/
2020
- provider: GitHub
2121
auth_token:
2222
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2013-2022 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Serilog.Core;
16+
using Serilog.Events;
17+
18+
namespace Serilog.Enrichers
19+
{
20+
public abstract class CachedPropertyEnricher: ILogEventEnricher
21+
{
22+
private LogEventProperty _cachedProperty { get; set; }
23+
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+
}
33+
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+
if (_cachedProperty == null)
39+
_cachedProperty = CreateProperty(propertyFactory);
40+
41+
return _cachedProperty;
42+
}
43+
44+
protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
45+
}
46+
}

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

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2018 Serilog Contributors
1+
// Copyright 2013-2022 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -22,38 +22,16 @@ namespace Serilog.Enrichers
2222
/// <summary>
2323
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
2424
/// </summary>
25-
public class EnvironmentNameEnricher : ILogEventEnricher
25+
public class EnvironmentNameEnricher : CachedPropertyEnricher
2626
{
27-
LogEventProperty _cachedProperty;
28-
2927
/// <summary>
3028
/// The property name added to enriched log events.
3129
/// </summary>
3230
public const string EnvironmentNamePropertyName = "EnvironmentName";
3331

34-
/// <summary>
35-
/// Enrich the log event.
36-
/// </summary>
37-
/// <param name="logEvent">The log event to enrich.</param>
38-
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
39-
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
40-
{
41-
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
42-
}
43-
44-
private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
45-
{
46-
// Don't care about thread-safety, in the worst case the field gets overwritten and one
47-
// property will be GCed
48-
if (_cachedProperty == null)
49-
_cachedProperty = CreateProperty(propertyFactory);
50-
51-
return _cachedProperty;
52-
}
53-
5432
// Qualify as uncommon-path
5533
[MethodImpl(MethodImplOptions.NoInlining)]
56-
private static LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
34+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
5735
{
5836
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
5937

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2016 Serilog Contributors
1+
// Copyright 2013-2022 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,27 +21,14 @@ namespace Serilog.Enrichers
2121
/// <summary>
2222
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
2323
/// </summary>
24-
public class EnvironmentUserNameEnricher : ILogEventEnricher
24+
public class EnvironmentUserNameEnricher : CachedPropertyEnricher
2525
{
26-
LogEventProperty _cachedProperty;
27-
2826
/// <summary>
2927
/// The property name added to enriched log events.
3028
/// </summary>
3129
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName";
3230

33-
/// <summary>
34-
/// Enrich the log event.
35-
/// </summary>
36-
/// <param name="logEvent">The log event to enrich.</param>
37-
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
38-
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
39-
{
40-
_cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, GetEnvironmentUserName());
41-
logEvent.AddPropertyIfAbsent(_cachedProperty);
42-
}
43-
44-
private static string GetEnvironmentUserName()
31+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
4532
{
4633
#if ENV_USER_NAME
4734
var userDomainName = Environment.UserDomainName;
@@ -50,7 +37,9 @@ private static string GetEnvironmentUserName()
5037
var userDomainName = Environment.GetEnvironmentVariable("USERDOMAIN");
5138
var userName = Environment.GetEnvironmentVariable("USERNAME");
5239
#endif
53-
return !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
40+
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
41+
42+
return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
5443
}
5544
}
56-
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2013-2022 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using Serilog.Core;
17+
using Serilog.Events;
18+
using System.Runtime.CompilerServices;
19+
20+
namespace Serilog.Enrichers
21+
{
22+
/// <summary>
23+
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
24+
/// </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; }
33+
34+
public EnvironmentVariableEnricher(string envVarName, string propertyName)
35+
{
36+
_envVarName = envVarName;
37+
EnvironmentVariablePropertyName = propertyName ?? envVarName;
38+
}
39+
40+
// Qualify as uncommon-path
41+
[MethodImpl(MethodImplOptions.NoInlining)]
42+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
43+
{
44+
var environmentVariableValue = Environment.GetEnvironmentVariable(_envVarName);
45+
46+
return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
47+
}
48+
}
49+
}

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

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013-2018 Serilog Contributors
1+
// Copyright 2013-2022 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -26,38 +26,16 @@ namespace Serilog.Enrichers
2626
/// <summary>
2727
/// Enriches log events with a MachineName property containing <see cref="Environment.MachineName"/>.
2828
/// </summary>
29-
public class MachineNameEnricher : ILogEventEnricher
29+
public class MachineNameEnricher : CachedPropertyEnricher
3030
{
31-
LogEventProperty _cachedProperty;
32-
3331
/// <summary>
3432
/// The property name added to enriched log events.
3533
/// </summary>
3634
public const string MachineNamePropertyName = "MachineName";
3735

38-
/// <summary>
39-
/// Enrich the log event.
40-
/// </summary>
41-
/// <param name="logEvent">The log event to enrich.</param>
42-
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
43-
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
44-
{
45-
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
46-
}
47-
48-
private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
49-
{
50-
// Don't care about thread-safety, in the worst case the field gets overwritten and one
51-
// property will be GCed
52-
if (_cachedProperty == null)
53-
_cachedProperty = CreateProperty(propertyFactory);
54-
55-
return _cachedProperty;
56-
}
57-
5836
// Qualify as uncommon-path
5937
[MethodImpl(MethodImplOptions.NoInlining)]
60-
private static LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
38+
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
6139
{
6240
#if NETSTANDARD1_3
6341
var machineName = Dns.GetHostName();

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,23 @@ public static LoggerConfiguration WithEnvironmentUserName(
5858
{
5959
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
6060
return enrichmentConfiguration.With<EnvironmentUserNameEnricher>();
61-
}
61+
}
62+
63+
/// <summary>
64+
/// Enriches log events with an property containing the value of the specified Environment Variable using
65+
/// [<see cref="Environment.GetEnvironmentVariable"/>\]<see cref="Environment.GetEnvironmentVariable"/>.
66+
/// </summary>
67+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
68+
/// <param name="environmentVariableName">The name of the Environment Variable</param>
69+
/// <param name="propertyName">The Optional name of the property. If empty <see cref="environmentVariableName"/> is used</param>
70+
/// <returns>Configuration object allowing method chaining.</returns>
71+
public static LoggerConfiguration WithEnvironmentVariable(
72+
this LoggerEnrichmentConfiguration enrichmentConfiguration, string environmentVariableName, string propertyName = null)
73+
{
74+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
75+
var environmentVariableEnricher = new EnvironmentVariableEnricher(environmentVariableName, propertyName);
76+
return enrichmentConfiguration.With(environmentVariableEnricher);
77+
}
6278

6379
}
64-
}
80+
}

Diff for: src/Serilog.Enrichers.Environment/Serilog.Enrichers.Environment.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Enrich Serilog log events with properties from System.Environment.</Description>
5-
<VersionPrefix>2.2.0</VersionPrefix>
5+
<VersionPrefix>2.3.0</VersionPrefix>
66
<Authors>Serilog Contributors</Authors>
77
<TargetFrameworks>net45;netstandard1.3;netstandard1.5;netstandard2.0</TargetFrameworks>
88
<AssemblyName>Serilog.Enrichers.Environment</AssemblyName>

0 commit comments

Comments
 (0)