Skip to content

Commit

Permalink
Implemented CachedPropertyEnricher
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobra committed Dec 22, 2022
1 parent 77ea4c4 commit 41d5cd4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 68 deletions.
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2018 Serilog Contributors
// 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.
Expand All @@ -22,38 +22,16 @@ 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
public class EnvironmentNameEnricher : CachedPropertyEnricher
{
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)
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2016 Serilog Contributors
// 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.
Expand All @@ -21,27 +21,14 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
/// </summary>
public class EnvironmentUserNameEnricher : ILogEventEnricher
public class EnvironmentUserNameEnricher : CachedPropertyEnricher
{
LogEventProperty _cachedProperty;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName";

/// <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)
{
_cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, GetEnvironmentUserName());
logEvent.AddPropertyIfAbsent(_cachedProperty);
}

private static string GetEnvironmentUserName()
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
#if ENV_USER_NAME
var userDomainName = Environment.UserDomainName;
Expand All @@ -50,7 +37,9 @@ private static string GetEnvironmentUserName()
var userDomainName = Environment.GetEnvironmentVariable("USERDOMAIN");
var userName = Environment.GetEnvironmentVariable("USERNAME");
#endif
return !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;
var environmentUserName = !string.IsNullOrWhiteSpace(userDomainName) ? $@"{userDomainName}\{userName}" : userName;

return propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, environmentUserName);
}
}
}
}
28 changes: 3 additions & 25 deletions src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2018 Serilog Contributors
// 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.
Expand Down Expand Up @@ -26,38 +26,16 @@ namespace Serilog.Enrichers
/// <summary>
/// Enriches log events with a MachineName property containing <see cref="Environment.MachineName"/>.
/// </summary>
public class MachineNameEnricher : ILogEventEnricher
public class MachineNameEnricher : CachedPropertyEnricher
{
LogEventProperty _cachedProperty;

/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string MachineNamePropertyName = "MachineName";

/// <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)
protected override LogEventProperty CreateProperty(ILogEventPropertyFactory propertyFactory)
{
#if NETSTANDARD1_3
var machineName = Dns.GetHostName();
Expand Down

0 comments on commit 41d5cd4

Please sign in to comment.