Skip to content

Commit

Permalink
Merge pull request #52 from serilog/dev
Browse files Browse the repository at this point in the history
2.3.0 Release
  • Loading branch information
nblumhardt authored Oct 3, 2023
2 parents 9ba7457 + 34d133c commit db174d1
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 90 deletions.
39 changes: 24 additions & 15 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,56 @@ echo "build: Build started"
Push-Location $PSScriptRoot

if(Test-Path .\artifacts) {
echo "build: Cleaning .\artifacts"
Remove-Item .\artifacts -Force -Recurse
echo "build: Cleaning ./artifacts"
Remove-Item ./artifacts -Force -Recurse
}

& dotnet restore --no-cache

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

echo "build: Version suffix is $suffix"
echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"

foreach ($src in ls src/*) {
foreach ($src in gci src/*) {
Push-Location $src

echo "build: Packaging project in $src"

& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix
if($LASTEXITCODE -ne 0) { exit 1 }
& dotnet build -c Release --version-suffix=$buildSuffix

if($suffix) {
& dotnet pack -c Release --include-source --no-build -o ../../artifacts --version-suffix=$suffix
} else {
& dotnet pack -c Release --include-source --no-build -o ../../artifacts
}
if($LASTEXITCODE -ne 0) { exit 1 }

Pop-Location
}

foreach ($test in ls test/*.PerformanceTests) {
foreach ($test in gci test/*.Tests) {
Push-Location $test

echo "build: Building performance test project in $test"
echo "build: Testing project in $test"

& dotnet build -c Release
if($LASTEXITCODE -ne 0) { exit 2 }
& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 3 }

Pop-Location
}

foreach ($test in ls test/*.Tests) {
foreach ($test in ls test/*.PerformanceTests) {
Push-Location $test

echo "build: Testing project in $test"
echo "build: Building performance test project in $test"

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 3 }
& dotnet build -c Release
if($LASTEXITCODE -ne 0) { exit 2 }

Pop-Location
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ The package includes:
* `WithMachineName()` - adds `MachineName` based on either `%COMPUTERNAME%` (Windows) or `$HOSTNAME` (macOS, Linux)
* `WithEnvironmentUserName()` - adds `EnvironmentUserName` based on `USERNAME` and `USERDOMAIN` (if available)
* `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')
* `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.

Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2019
image: Visual Studio 2022
configuration: Release
install:
- ps: mkdir -Force ".\build\" | Out-Null
Expand All @@ -13,10 +13,10 @@ artifacts:
deploy:
- provider: NuGet
api_key:
secure: 9B24CFy1l5KYjxsp8AXPx6ANkDI3KKXSqi18nCUk1kyj0mClwfNbNj0Tna4+gUC5
secure: dw2kT1B1gpFLOE7ZHhExKgcspS+tf5maUq0jEdH17YyHjUxQpemGcwvbe4nuf/Ic
skip_symbols: true
on:
branch: /^(master|dev)$/
branch: /^(main|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2013-2022 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Serilog.Core;
using Serilog.Events;

namespace Serilog.Enrichers
{
public abstract class CachedPropertyEnricher: ILogEventEnricher
{
private LogEventProperty _cachedProperty { get; set; }

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
}

private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
{
// Don't care about thread-safety, in the worst case the field gets overwritten and one
// property will be GCed
if (_cachedProperty == null)
_cachedProperty = CreateProperty(propertyFactory);

return _cachedProperty;
}

protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2018 Serilog Contributors
// Copyright 2013-2022 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -22,38 +22,16 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
/// </summary>
public class EnvironmentNameEnricher : ILogEventEnricher
public class EnvironmentNameEnricher : CachedPropertyEnricher
{
LogEventProperty _cachedProperty;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string EnvironmentNamePropertyName = "EnvironmentName";

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
}

private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
{
// Don't care about thread-safety, in the worst case the field gets overwritten and one
// property will be GCed
if (_cachedProperty == null)
_cachedProperty = CreateProperty(propertyFactory);

return _cachedProperty;
}

// Qualify as uncommon-path
[MethodImpl(MethodImplOptions.NoInlining)]
private static LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2016 Serilog Contributors
// Copyright 2013-2022 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,27 +21,14 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
/// </summary>
public class EnvironmentUserNameEnricher : ILogEventEnricher
public class EnvironmentUserNameEnricher : CachedPropertyEnricher
{
LogEventProperty _cachedProperty;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName";

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
_cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, GetEnvironmentUserName());
logEvent.AddPropertyIfAbsent(_cachedProperty);
}

private static string GetEnvironmentUserName()
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
#if ENV_USER_NAME
var userDomainName = Environment.UserDomainName;
Expand All @@ -50,7 +37,9 @@ private static string GetEnvironmentUserName()
var userDomainName = Environment.GetEnvironmentVariable("USERDOMAIN");
var userName = Environment.GetEnvironmentVariable("USERNAME");
#endif
return !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;

return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2013-2022 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Core;
using Serilog.Events;
using System.Runtime.CompilerServices;

namespace Serilog.Enrichers
{
/// <summary>
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
/// </summary>
public class EnvironmentVariableEnricher : CachedPropertyEnricher
{
private readonly string _envVarName;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public string EnvironmentVariablePropertyName { get; }

public EnvironmentVariableEnricher(string envVarName, string propertyName)
{
_envVarName = envVarName;
EnvironmentVariablePropertyName = propertyName ?? envVarName;
}

// Qualify as uncommon-path
[MethodImpl(MethodImplOptions.NoInlining)]
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
var environmentVariableValue = Environment.GetEnvironmentVariable(_envVarName);

return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
}
}
}
28 changes: 3 additions & 25 deletions src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2018 Serilog Contributors
// Copyright 2013-2022 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -26,38 +26,16 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with a MachineName property containing <see cref="Environment.MachineName"/>.
/// </summary>
public class MachineNameEnricher : ILogEventEnricher
public class MachineNameEnricher : CachedPropertyEnricher
{
LogEventProperty _cachedProperty;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string MachineNamePropertyName = "MachineName";

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(GetLogEventProperty(propertyFactory));
}

private LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
{
// Don't care about thread-safety, in the worst case the field gets overwritten and one
// property will be GCed
if (_cachedProperty == null)
_cachedProperty = CreateProperty(propertyFactory);

return _cachedProperty;
}

// Qualify as uncommon-path
[MethodImpl(MethodImplOptions.NoInlining)]
private static LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
#if NETSTANDARD1_3
var machineName = Dns.GetHostName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,23 @@ public static LoggerConfiguration WithEnvironmentUserName(
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<EnvironmentUserNameEnricher>();
}
}

/// <summary>
/// Enriches log events with an property containing the value of the specified Environment Variable using
/// [<see cref="Environment.GetEnvironmentVariable"/>\]<see cref="Environment.GetEnvironmentVariable"/>.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <param name="environmentVariableName">The name of the Environment Variable</param>
/// <param name="propertyName">The Optional name of the property. If empty <see cref="environmentVariableName"/> is used</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithEnvironmentVariable(
this LoggerEnrichmentConfiguration enrichmentConfiguration, string environmentVariableName, string propertyName = null)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
var environmentVariableEnricher = new EnvironmentVariableEnricher(environmentVariableName, propertyName);
return enrichmentConfiguration.With(environmentVariableEnricher);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Enrich Serilog log events with properties from System.Environment.</Description>
<VersionPrefix>2.2.0</VersionPrefix>
<VersionPrefix>2.3.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.3;netstandard1.5;netstandard2.0</TargetFrameworks>
<AssemblyName>Serilog.Enrichers.Environment</AssemblyName>
Expand Down
Loading

0 comments on commit db174d1

Please sign in to comment.