Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serilog 4 updates; unpublish enricher types that are already accessible via extensions #63

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:EnableSourceLink=true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we turn on ContinuousIntegrationBuild whilst making changes?
(e.g. https://github.com/serilog/serilog/blob/fdf4a4872b4effd02dcd7d788e45b6b8ffdfa2fc/Build.ps1#L20)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general question about SourceLink support in the enrichers -
As it stands, this package appears to have neither embedded PDBs nor a .snupkg file.
Compare that with the Process enricher, which has embedded PDBs inside the .nupkg.

Should they all be changed to do one or the other?

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).
35 changes: 16 additions & 19 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
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
deploy:
- provider: NuGet
api_key:
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
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: main
- provider: NuGet
api_key:
secure: Fh92tRIFbe1FAiyD8lTThWgAorQ1vV+eFYMlUK0iLHBBenJcy/UYc1qj6kgHvUcO
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: 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,31 +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
_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>
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>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,31 @@
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>
class EnvironmentVariableEnricher : CachedPropertyEnricher
{
readonly string _envVarName;

/// <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 EnvironmentVariableEnricher : CachedPropertyEnricher
{
private readonly string _envVarName;

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

public EnvironmentVariableEnricher(string envVarName, string? propertyName)
{
_envVarName = envVarName;
EnvironmentVariablePropertyName = propertyName ?? envVarName;
}
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);
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
var environmentVariableValue = Environment.GetEnvironmentVariable(_envVarName);

return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
}
return propertyFactory.CreateProperty(EnvironmentVariablePropertyName, environmentVariableValue);
}
}
}
Loading