-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from serilog/dev
2.2.0 Release
- Loading branch information
Showing
9 changed files
with
123 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2013-2018 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 EnvironmentNameEnricher : ILogEventEnricher | ||
{ | ||
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) | ||
{ | ||
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||
|
||
if (string.IsNullOrWhiteSpace(environmentName)) | ||
{ | ||
environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(environmentName)) | ||
{ | ||
environmentName = "Production"; | ||
} | ||
|
||
return propertyFactory.CreateProperty(EnvironmentNamePropertyName, environmentName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
test/Serilog.Enrichers.Environment.Tests/Enrichers/EnvironmentEnvironmenNameEnricherTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Serilog.Events; | ||
using Serilog.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Tests.Enrichers | ||
{ | ||
public class EnvironmentEnvironmenNameEnricherTests | ||
{ | ||
[Fact] | ||
public void EnvironmentNameEnricherIsApplied() | ||
{ | ||
LogEvent evt = null; | ||
var log = new LoggerConfiguration() | ||
.Enrich.WithEnvironmentName() | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information(@"Has an EnvironmenName property with the value of the DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT environment variable."); | ||
|
||
Assert.NotNull(evt); | ||
Assert.NotEmpty((string)evt.Properties["EnvironmentName"].LiteralValue()); | ||
} | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
test/Serilog.Enrichers.Environment.Tests/Properties/launchSettings.json
This file was deleted.
Oops, something went wrong.
32 changes: 6 additions & 26 deletions
32
test/Serilog.Enrichers.Environment.Tests/Serilog.Enrichers.Environment.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp1.0;net46</TargetFrameworks> | ||
<AssemblyName>Serilog.Enrichers.Environment.Tests</AssemblyName> | ||
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile> | ||
<SignAssembly>true</SignAssembly> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<PackageId>Serilog.Enrichers.Environment.Tests</PackageId> | ||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback> | ||
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.0.4</RuntimeFrameworkVersion> | ||
<TargetFrameworks>netcoreapp3.1;net46</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Serilog.Enrichers.Environment\Serilog.Enrichers.Environment.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" /> | ||
<PackageReference Include="xunit" Version="2.2.0-beta5-build3474" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' "> | ||
<Reference Include="System" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file was deleted.
Oops, something went wrong.