Skip to content

Commit f3b48a1

Browse files
authored
Merge pull request #38 from serilog/dev
2.2.0 Release
2 parents 6fddfe0 + 3c4e78c commit f3b48a1

File tree

9 files changed

+123
-60
lines changed

9 files changed

+123
-60
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ The package includes:
2727

2828
* `WithMachineName()` - adds `MachineName` based on either `%COMPUTERNAME%` (Windows) or `$HOSTNAME` (macOS, Linux)
2929
* `WithEnvironmentUserName()` - adds `EnvironmentUserName` based on `USERNAME` and `USERDOMAIN` (if available)
30+
* `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')
3031

3132
Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).

Diff for: appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2017
3+
image: Visual Studio 2019
44
configuration: Release
55
install:
66
- ps: mkdir -Force ".\build\" | Out-Null
@@ -13,7 +13,7 @@ artifacts:
1313
deploy:
1414
- provider: NuGet
1515
api_key:
16-
secure: bd9z4P73oltOXudAjPehwp9iDKsPtC+HbgshOrSgoyQKr5xVK+bxJQngrDJkHdY8
16+
secure: j+PjfZXCmLnVDJJreZpM8MPgFEhbaOZ4iwjbLZpG4X+rtNjThwOvsgwXC5QSbUL1
1717
skip_symbols: true
1818
on:
1919
branch: /^(master|dev)$/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2013-2018 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 EnvironmentNameEnricher : ILogEventEnricher
26+
{
27+
LogEventProperty _cachedProperty;
28+
29+
/// <summary>
30+
/// The property name added to enriched log events.
31+
/// </summary>
32+
public const string EnvironmentNamePropertyName = "EnvironmentName";
33+
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+
54+
// Qualify as uncommon-path
55+
[MethodImpl(MethodImplOptions.NoInlining)]
56+
private static LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
57+
{
58+
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
59+
60+
if (string.IsNullOrWhiteSpace(environmentName))
61+
{
62+
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
63+
}
64+
65+
if (string.IsNullOrWhiteSpace(environmentName))
66+
{
67+
environmentName = "Production";
68+
}
69+
70+
return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName);
71+
}
72+
}
73+
}

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ namespace Serilog
2323
/// capabilities.
2424
/// </summary>
2525
public static class EnvironmentLoggerConfigurationExtensions
26-
{
26+
{
27+
/// <summary>
28+
/// Enrich log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
29+
/// </summary>
30+
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
31+
/// <returns>Configuration object allowing method chaining.</returns>
32+
public static LoggerConfiguration WithEnvironmentName(
33+
this LoggerEnrichmentConfiguration enrichmentConfiguration)
34+
{
35+
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
36+
return enrichmentConfiguration.With<EnvironmentNameEnricher>();
37+
}
38+
2739
/// <summary>
2840
/// Enrich log events with a MachineName property containing the current <see cref="Environment.MachineName"/>.
2941
/// </summary>

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<Description>Enrich Serilog log events with properties from System.Environment.</Description>
5-
<VersionPrefix>2.1.3</VersionPrefix>
5+
<VersionPrefix>2.2.0</VersionPrefix>
66
<Authors>Serilog Contributors</Authors>
7-
<TargetFrameworks>net45;netstandard1.3;netstandard1.5</TargetFrameworks>
7+
<TargetFrameworks>net45;netstandard1.3;netstandard1.5;netstandard2.0</TargetFrameworks>
88
<AssemblyName>Serilog.Enrichers.Environment</AssemblyName>
99
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
1010
<SignAssembly>true</SignAssembly>
@@ -13,7 +13,7 @@
1313
<PackageTags>serilog;machine;enricher</PackageTags>
1414
<PackageIconUrl>http://serilog.net/images/serilog-enricher-nuget.png</PackageIconUrl>
1515
<PackageProjectUrl>http://serilog.net</PackageProjectUrl>
16-
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
16+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1717
<RepositoryUrl>https://github.com/serilog/serilog-enrichers-environment</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
1919
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
@@ -32,7 +32,7 @@
3232
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
3333
</ItemGroup>
3434

35-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
35+
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' AND '$(TargetFramework)' != 'netstandard1.5' ">
3636
<DefineConstants>$(DefineConstants);ENV_USER_NAME</DefineConstants>
3737
</PropertyGroup>
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Serilog.Events;
2+
using Serilog.Tests.Support;
3+
using Xunit;
4+
5+
namespace Serilog.Tests.Enrichers
6+
{
7+
public class EnvironmentEnvironmenNameEnricherTests
8+
{
9+
[Fact]
10+
public void EnvironmentNameEnricherIsApplied()
11+
{
12+
LogEvent evt = null;
13+
var log = new LoggerConfiguration()
14+
.Enrich.WithEnvironmentName()
15+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
16+
.CreateLogger();
17+
18+
log.Information(@"Has an EnvironmenName property with the value of the DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT environment variable.");
19+
20+
Assert.NotNull(evt);
21+
Assert.NotEmpty((string)evt.Properties["EnvironmentName"].LiteralValue());
22+
}
23+
}
24+
}

Diff for: test/Serilog.Enrichers.Environment.Tests/Properties/launchSettings.json

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp1.0;net46</TargetFrameworks>
5-
<AssemblyName>Serilog.Enrichers.Environment.Tests</AssemblyName>
6-
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
7-
<SignAssembly>true</SignAssembly>
8-
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
9-
<PackageId>Serilog.Enrichers.Environment.Tests</PackageId>
10-
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
11-
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback>
12-
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.0.4</RuntimeFrameworkVersion>
4+
<TargetFrameworks>netcoreapp3.1;net46</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
136
</PropertyGroup>
147

15-
<ItemGroup>
16-
<None Include="App.config" />
17-
</ItemGroup>
18-
198
<ItemGroup>
209
<ProjectReference Include="..\..\src\Serilog.Enrichers.Environment\Serilog.Enrichers.Environment.csproj" />
2110
</ItemGroup>
2211

2312
<ItemGroup>
24-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
25-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" />
26-
<PackageReference Include="xunit" Version="2.2.0-beta5-build3474" />
27-
</ItemGroup>
28-
29-
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
30-
<Reference Include="System" />
31-
<Reference Include="Microsoft.CSharp" />
32-
</ItemGroup>
33-
34-
<ItemGroup>
35-
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
15+
<PackageReference Include="xunit" Version="2.4.1" />
3616
</ItemGroup>
3717

3818
</Project>

Diff for: test/Serilog.Enrichers.Environment.Tests/app.config

-16
This file was deleted.

0 commit comments

Comments
 (0)