From 056a4a335cedd7b3d967bede76b8907e474dbc33 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Fri, 20 Oct 2023 13:26:50 -0700 Subject: [PATCH] Allow suppressing specific fields Also default to omitting null values. A configuration hook (CustomNullValue) can be used to preserve the legacy behavior --- README.md | 2 +- .../BackwardsCompatabilityShims.cs | 2 +- src/Spiffy.Monitoring/Config/Configuration.cs | 2 ++ .../Config/InitializationApi.cs | 7 +++++- src/Spiffy.Monitoring/EventContext.cs | 18 ++++++++++++--- .../EventContext_ObjectMethods.cs | 4 ++-- .../Spiffy.Monitoring.csproj | 2 +- tests/TestConsoleApp/Program.cs | 11 +++++++--- tests/UnitTests/Publishing.cs | 22 +++++++++++++++++++ 9 files changed, 58 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e5c3ed1..b24a18d 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ Multiple providers can be provied, for example, this application uses both `Cons ### Exception Entry > [2014-06-13 00:12:52.038Z] Application=MyApplication **Level=Error** Component=Program Operation=Main TimeElapsed=1027.0 Key=Value **ErrorReason="An exception has ocurred"** **Exception_Type=ApplicationException Exception_Message="you were unlucky!"** Exception_StackTrace=" at TestConsoleApp.Program.DoSomethingDangerous() in c:\src\git\github\chris-peterson\Spiffy\src\Tests\TestConsoleApp\Program.cs:line 47 - at TestConsoleApp.Program.Main() in c:\src\git\github\chris-peterson\Spiffy\src\Tests\TestConsoleApp\Program.cs:line 29" InnermostException_Type=NullReferenceException **InnermostException_Message="Object reference not set to an instance of an object."** InnermostException_StackTrace={null} Exception="See Exception_* and InnermostException_* for more details" TimeElapsed_LongRunning=1000.0 + at TestConsoleApp.Program.Main() in c:\src\git\github\chris-peterson\Spiffy\src\Tests\TestConsoleApp\Program.cs:line 29" InnermostException_Type=NullReferenceException **InnermostException_Message="Object reference not set to an instance of an object."** Exception="See Exception_* and InnermostException_* for more details" TimeElapsed_LongRunning=1000.0 ## Hosting Frameworks diff --git a/src/Spiffy.Monitoring/BackwardsCompatabilityShims.cs b/src/Spiffy.Monitoring/BackwardsCompatabilityShims.cs index 64e0df1..43bf708 100644 --- a/src/Spiffy.Monitoring/BackwardsCompatabilityShims.cs +++ b/src/Spiffy.Monitoring/BackwardsCompatabilityShims.cs @@ -30,7 +30,7 @@ public static void IncludeInformationalException(this EventContext target, Excep public static class EventContextExtensions { [Obsolete("This extension should be avoided, instead preferring IncludeStructure on EventContext")] - public static EventContext IncludeStructure(this EventContext eventContext, object structure, string keyPrefix = null, bool includeNullValues = true) + public static EventContext IncludeStructure(this EventContext eventContext, object structure, string keyPrefix = null, bool includeNullValues = false) { return eventContext.IncludeStructure(structure, keyPrefix, includeNullValues); } diff --git a/src/Spiffy.Monitoring/Config/Configuration.cs b/src/Spiffy.Monitoring/Config/Configuration.cs index cc3e419..bb4bb07 100644 --- a/src/Spiffy.Monitoring/Config/Configuration.cs +++ b/src/Spiffy.Monitoring/Config/Configuration.cs @@ -20,6 +20,7 @@ public static void Initialize(Action customize) _beforeLoggingActions = api.GetBeforeLoggingActions(); _loggingActions = api.GetLoggingActions(); + CustomNullValue = api.CustomNullValue; RemoveNewLines = api.RemoveNewlines; DeprioritizedValueLength = api.DeprioritizedValueLength; } @@ -34,6 +35,7 @@ internal static Action [] GetLoggingActions() return _loggingActions; } + internal static string CustomNullValue { get; set; } internal static bool RemoveNewLines { get; private set; } internal static int DeprioritizedValueLength { get; private set; } = 1024; diff --git a/src/Spiffy.Monitoring/Config/InitializationApi.cs b/src/Spiffy.Monitoring/Config/InitializationApi.cs index af6e48b..2dee14f 100644 --- a/src/Spiffy.Monitoring/Config/InitializationApi.cs +++ b/src/Spiffy.Monitoring/Config/InitializationApi.cs @@ -48,7 +48,12 @@ public InitializationApi() Providers = new ProvidersApi(this); Callbacks = new CallbacksApi(this); } - + + /// + /// If set, this value is used for logging values that are null. + /// + public string CustomNullValue { get; set; } + /// /// Whether or not to remove newline characters from logged values. /// diff --git a/src/Spiffy.Monitoring/EventContext.cs b/src/Spiffy.Monitoring/EventContext.cs index 3d2bf58..da93ce5 100644 --- a/src/Spiffy.Monitoring/EventContext.cs +++ b/src/Spiffy.Monitoring/EventContext.cs @@ -177,6 +177,14 @@ public void Suppress() IsSuppressed = true; } + public void SuppressFields(params string [] fields) + { + foreach (var field in fields) + { + _values.TryRemove(field, out _); + } + } + // ReSharper disable once RedundantDefaultMemberInitializer volatile bool _disposed = false; @@ -221,13 +229,17 @@ public void Initialize(string component, string operation) internal LogEvent Render() { - var kvps = _values + IEnumerable> values = _values; + if (Configuration.CustomNullValue == null) + { + values = values.Where(kvp => kvp.Value.Value != null); + } + var kvps = values .OrderBy(x => x.Value.Order) .ToDictionary( kvp => kvp.Key, kvp => GetValue(kvp.Value.Value)); - foreach (var kvp in GetCountValues()) { kvps.Add(kvp.Key, kvp.Value); @@ -315,7 +327,7 @@ static string GetValue(object value) { if (value == null) { - return "{null}"; + return Configuration.CustomNullValue; } var str = value.ToString(); diff --git a/src/Spiffy.Monitoring/EventContext_ObjectMethods.cs b/src/Spiffy.Monitoring/EventContext_ObjectMethods.cs index b2cd7ce..dc3b34a 100644 --- a/src/Spiffy.Monitoring/EventContext_ObjectMethods.cs +++ b/src/Spiffy.Monitoring/EventContext_ObjectMethods.cs @@ -5,7 +5,7 @@ namespace Spiffy.Monitoring { public partial class EventContext { - public EventContext IncludeStructure(object structure, string keyPrefix = null, bool includeNullValues = true) + public EventContext IncludeStructure(object structure, string keyPrefix = null, bool includeNullValues = false) { if (structure != null) { @@ -13,7 +13,7 @@ public EventContext IncludeStructure(object structure, string keyPrefix = null, { try { - var val = property.GetValue(structure, null); + var val = property.GetValue(structure, null) ?? Configuration.CustomNullValue; if (val == null && !includeNullValues) { continue; diff --git a/src/Spiffy.Monitoring/Spiffy.Monitoring.csproj b/src/Spiffy.Monitoring/Spiffy.Monitoring.csproj index 06380f0..8ecd609 100644 --- a/src/Spiffy.Monitoring/Spiffy.Monitoring.csproj +++ b/src/Spiffy.Monitoring/Spiffy.Monitoring.csproj @@ -13,7 +13,7 @@ README.md MIT True - 6.2.6 + 6.3.0 Spiffy.Monitoring latest diff --git a/tests/TestConsoleApp/Program.cs b/tests/TestConsoleApp/Program.cs index 71af2ec..522f6a0 100755 --- a/tests/TestConsoleApp/Program.cs +++ b/tests/TestConsoleApp/Program.cs @@ -30,10 +30,11 @@ static void Main(string[] args) break; case "console": Configuration.Initialize(spiffy => { + spiffy.CustomNullValue = ""; spiffy.Callbacks.BeforeLogging(eventContext => { if (eventContext.Level == Level.Info) { - eventContext.Suppress(); + //eventContext.Suppress(); } }); spiffy.Providers.Console(); @@ -120,9 +121,13 @@ static void Main(string[] args) // info: using (var context = new EventContext("Greetings", "Start")) { - context["Greeting"] = "Hello world!"; + context["Greeting"] = "hi"; + context["MyField"] = null; + context.SuppressFields("Greeting"); } - + + //return; + while (true) { // warning: diff --git a/tests/UnitTests/Publishing.cs b/tests/UnitTests/Publishing.cs index a24f3bf..fe61606 100644 --- a/tests/UnitTests/Publishing.cs +++ b/tests/UnitTests/Publishing.cs @@ -18,6 +18,15 @@ public void Events_are_published_on_disposal() Then(It_should_publish_the_log_message); } + [Scenario] + public void Suppressed_fields_are_not_emitted() + { + Given(A_publishing_context) + .And(EventContext_fields_are_suppressed); + When(Disposing_an_event_context); + Then(Some_fields_are_omitted); + } + [Scenario] public void Suppressed_events_are_not_published() { @@ -127,6 +136,13 @@ void A_publishing_context() _context =new PublishingTestContext(); } + void EventContext_fields_are_suppressed() + { + var field = "Uninteresting"; + _context.EventContext[field] = "some value"; + _context.EventContext.SuppressFields(field); + } + void EventContext_is_suppressed() { _context.EventContext.Suppress(); @@ -144,6 +160,12 @@ void It_should_publish_the_log_message() logEvent.MessageWithTime.Length.Should().BeInRange(10, 200); } + void Some_fields_are_omitted() + { + var logEvent = _context.LogEvents.Single(); + logEvent.Message.Should().NotContain("Uninteresting"); + } + void It_should_not_publish_again() { _context.LogEvents.Count.Should().Be(1);