From 41d5cd442d2ad8ae7b35a5491dea6fdf66675a85 Mon Sep 17 00:00:00 2001 From: Jakob Rasmussen Date: Thu, 22 Dec 2022 12:01:27 +0100 Subject: [PATCH] Implemented CachedPropertyEnricher --- .../Enrichers/CachedPropertyEnricher.cs | 46 +++++++++++++++++++ .../Enrichers/EnvironmentNameEnricher.cs | 28 ++--------- .../Enrichers/EnvironmentUserNameEnricher.cs | 25 +++------- .../Enrichers/MachineNameEnricher.cs | 28 ++--------- 4 files changed, 59 insertions(+), 68 deletions(-) create mode 100644 src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs diff --git a/src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs b/src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs new file mode 100644 index 0000000..a8097e4 --- /dev/null +++ b/src/Serilog.Enrichers.Environment/Enrichers/CachedPropertyEnricher.cs @@ -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; } + + /// + /// Enrich the log event. + /// + /// The log event to enrich. + /// Factory for creating new properties to add to the event. + 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); + } +} diff --git a/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs index 160cdab..9bfd9de 100644 --- a/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs +++ b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentNameEnricher.cs @@ -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. @@ -22,38 +22,16 @@ namespace Serilog.Enrichers /// /// Enriches log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable. /// - public class EnvironmentNameEnricher : ILogEventEnricher + public class EnvironmentNameEnricher : CachedPropertyEnricher { - LogEventProperty _cachedProperty; - /// /// The property name added to enriched log events. /// public const string EnvironmentNamePropertyName = "EnvironmentName"; - /// - /// Enrich the log event. - /// - /// The log event to enrich. - /// Factory for creating new properties to add to the event. - 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"); diff --git a/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs index 98209d0..f8a90c7 100644 --- a/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs +++ b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs @@ -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. @@ -21,27 +21,14 @@ namespace Serilog.Enrichers /// /// Enriches log events with an EnvironmentUserName property containing [\]. /// - public class EnvironmentUserNameEnricher : ILogEventEnricher + public class EnvironmentUserNameEnricher : CachedPropertyEnricher { - LogEventProperty _cachedProperty; - /// /// The property name added to enriched log events. /// public const string EnvironmentUserNamePropertyName = "EnvironmentUserName"; - /// - /// Enrich the log event. - /// - /// The log event to enrich. - /// Factory for creating new properties to add to the event. - 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; @@ -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); } } -} \ No newline at end of file +} diff --git a/src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs b/src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs index 6fd304b..a580eda 100644 --- a/src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs +++ b/src/Serilog.Enrichers.Environment/Enrichers/MachineNameEnricher.cs @@ -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. @@ -26,38 +26,16 @@ namespace Serilog.Enrichers /// /// Enriches log events with a MachineName property containing . /// - public class MachineNameEnricher : ILogEventEnricher + public class MachineNameEnricher : CachedPropertyEnricher { - LogEventProperty _cachedProperty; - /// /// The property name added to enriched log events. /// public const string MachineNamePropertyName = "MachineName"; - /// - /// Enrich the log event. - /// - /// The log event to enrich. - /// Factory for creating new properties to add to the event. - 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();