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

Added WithEnvironmentName enricher #42

Merged
merged 3 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,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 faalback value will be 'Production')

Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ namespace Serilog
/// capabilities.
/// </summary>
public static class EnvironmentLoggerConfigurationExtensions
{
{
/// <summary>
/// Enrich log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithEnvironmentName(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<EnvironmentNameEnricher>();
}

/// <summary>
/// Enrich log events with a MachineName property containing the current <see cref="Environment.MachineName"/>.
/// </summary>
Expand Down
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());
}
}
}