-
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 #52 from serilog/dev
2.3.0 Release
- Loading branch information
Showing
11 changed files
with
215 additions
and
90 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
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
46 changes: 46 additions & 0 deletions
46
src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.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,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); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/Serilog.Enrichers.Environment/Enrichers/EnvironmentVariableEnricher.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,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); | ||
} | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
src/Serilog.Enrichers.Environment/Serilog.Enrichers.Environment.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
Oops, something went wrong.