Skip to content

Commit

Permalink
Merge pull request #64 from serilog/dev
Browse files Browse the repository at this point in the history
3.0.0 Release
  • Loading branch information
nblumhardt committed Jun 9, 2024
2 parents db174d1 + 880a21a commit c7801e6
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 266 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.serilog-enrichers-environment/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.serilog-enrichers-environment/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.serilog-enrichers-environment/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 12 additions & 24 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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
Expand All @@ -16,45 +16,33 @@ $commitHash = $(git rev-parse --short HEAD)
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]

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

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

echo "build: Packaging project in $src"

& dotnet build -c Release --version-suffix=$buildSuffix

if($suffix) {
& dotnet pack -c Release --include-source --no-build -o ../../artifacts --version-suffix=$suffix
& dotnet build -c Release --version-suffix=$buildSuffix -p:ContinuousIntegrationBuild=true
if ($suffix) {
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix --no-build
} else {
& dotnet pack -c Release --include-source --no-build -o ../../artifacts
& dotnet pack -c Release -o ..\..\artifacts --no-build
}
if($LASTEXITCODE -ne 0) { exit 1 }
if($LASTEXITCODE -ne 0) { throw "build failed" }

Pop-Location
}

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

echo "build: Testing project in $test"

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

Pop-Location
}

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

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

& dotnet build -c Release
if($LASTEXITCODE -ne 0) { exit 2 }
if($LASTEXITCODE -ne 0) { throw "tests failed" }

Pop-Location
}

Pop-Location
Pop-Location
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Serilog.Enrichers.Environment

Enriches Serilog events with information from the process environment.
Enriches Serilog events with information from the execution environment.

[![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/)

To use the enricher, first install the NuGet package:

```powershell
Install-Package Serilog.Enrichers.Environment
dotnet add package Serilog.Enrichers.Environment
```

Then, apply the enricher to you `LoggerConfiguration`:
Expand All @@ -28,6 +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.
* `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.

Copyright &copy; 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).
38 changes: 19 additions & 19 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2022
configuration: Release
install:
- ps: mkdir -Force ".\build\" | Out-Null

build_script:
- ps: ./Build.ps1
- pwsh: ./Build.ps1
test: off
artifacts:
- path: artifacts/Serilog.*.nupkg
- path: artifacts/Serilog.*.nupkg
- path: artifacts/Serilog.*.snupkg
deploy:
- provider: NuGet
api_key:
secure: dw2kT1B1gpFLOE7ZHhExKgcspS+tf5maUq0jEdH17YyHjUxQpemGcwvbe4nuf/Ic
skip_symbols: true
on:
branch: /^(main|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
tag: v$(appveyor_build_version)
on:
branch: master
- provider: NuGet
api_key:
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
skip_symbols: true
on:
branch: /^(main|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifacts:
/Serilog.*\.nupkg/
/Serilog.*\.snupkg/
tag: v$(appveyor_build_version)
on:
branch: main

Binary file added assets/serilog-enricher-nuget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion serilog-enrichers-environment.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
ProjectSection(SolutionItems) = preProject
Build.ps1 = Build.ps1
NuGet.Config = NuGet.Config
README.md = README.md
assets\Serilog.snk = assets\Serilog.snk
appveyor.yml = appveyor.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{406006F7-12FA-4A8A-ADC8-80E5338F5275}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,28 @@
using Serilog.Core;
using Serilog.Events;

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

/// <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));
}
abstract class CachedPropertyEnricher: ILogEventEnricher
{
LogEventProperty? CachedProperty { get; set; }

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);
/// <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));
}

return _cachedProperty;
}

protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
LogEventProperty GetLogEventProperty(ILogEventPropertyFactory propertyFactory)
{
// Don't care about thread-safety, in the worst case the field gets overwritten and one
// property will be GCed
return CachedProperty ??= CreateProperty(propertyFactory);
}

protected abstract LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,33 @@
using System;
using Serilog.Core;
using Serilog.Events;
using System.Runtime.CompilerServices;

namespace Serilog.Enrichers
namespace Serilog.Enrichers;

/// <summary>
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
/// </summary>
sealed class EnvironmentNameEnricher : CachedPropertyEnricher
{
/// <summary>
/// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
/// The property name added to enriched log events.
/// </summary>
public class EnvironmentNameEnricher : CachedPropertyEnricher
const string EnvironmentNamePropertyName = "EnvironmentName";

protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string EnvironmentNamePropertyName = "EnvironmentName";
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

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

if (string.IsNullOrWhiteSpace(environmentName))
{
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
}

if (string.IsNullOrWhiteSpace(environmentName))
{
environmentName = "Production";
}
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
}

return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName);
if (string.IsNullOrWhiteSpace(environmentName))
{
environmentName = "Production";
}

return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,24 @@
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Enrichers
namespace Serilog.Enrichers;

/// <summary>
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
/// </summary>
sealed class EnvironmentUserNameEnricher : CachedPropertyEnricher
{
/// <summary>
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
/// The property name added to enriched log events.
/// </summary>
public class EnvironmentUserNameEnricher : CachedPropertyEnricher
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName";
const string EnvironmentUserNamePropertyName = "EnvironmentUserName";

protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
#if ENV_USER_NAME
var userDomainName = Environment.UserDomainName;
var userName = Environment.UserName;
#else
var userDomainName = Environment.GetEnvironmentVariable("USERDOMAIN");
var userName = Environment.GetEnvironmentVariable("USERNAME");
#endif
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
var userDomainName = Environment.UserDomainName;
var userName = Environment.UserName;
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;

return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
}
return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
}
}
}
Loading

0 comments on commit c7801e6

Please sign in to comment.