From 6b32f84ebe4d4e5f61c0be64e7b906f98ded062a Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Thu, 9 Oct 2025 10:04:31 +1000 Subject: [PATCH] A quick run over/general tidy-up --- Directory.Build.props | 3 ++ .../Extensions/Logging/SeqLoggerExtensions.cs | 16 ++++---- .../Seq.Extensions.Logging.csproj | 3 -- .../Parameters/MessageTemplateProcessor.cs | 2 - .../Seq.Extensions.Logging.Tests.csproj | 1 - .../Seq/Extensions/Logging/EnricherTests.cs | 40 ++++++------------- .../Extensions/Logging/EnrichingEventTests.cs | 26 +++--------- .../Logging/ExceptionDataEnricherTests.cs | 4 +- .../Extensions/Logging/SerilogLoggerTests.cs | 24 +++++------ .../Extensions/Logging/Support/SerilogSink.cs | 1 - .../Sinks/BatchedConnectionStatusTests.cs | 5 +-- .../Sinks/Seq/ControlledLevelSwitchTests.cs | 11 +++-- .../Sinks/Seq/SeqPayloadFormatterTests.cs | 5 ++- .../Support/Some.cs | 29 ++++++++------ 14 files changed, 65 insertions(+), 105 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index d2a7029..b6d027b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,8 @@ 9.0.0 + latest + enable + enable diff --git a/src/Seq.Extensions.Logging/Microsoft/Extensions/Logging/SeqLoggerExtensions.cs b/src/Seq.Extensions.Logging/Microsoft/Extensions/Logging/SeqLoggerExtensions.cs index d94dd2a..afdbca0 100644 --- a/src/Seq.Extensions.Logging/Microsoft/Extensions/Logging/SeqLoggerExtensions.cs +++ b/src/Seq.Extensions.Logging/Microsoft/Extensions/Logging/SeqLoggerExtensions.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Configuration; using Serilog.Sinks.PeriodicBatching; +// ReSharper disable UnusedMember.Global namespace Microsoft.Extensions.Logging; @@ -29,7 +30,7 @@ public static ILoggerFactory AddSeq(this ILoggerFactory loggerFactory, IConfigur if (loggerFactory == null) throw new ArgumentNullException(nameof(loggerFactory)); if (configuration == null) throw new ArgumentNullException(nameof(configuration)); - if (TryCreateProvider(configuration, LogLevel.Information, Array.Empty>(), out var provider)) + if (TryCreateProvider(configuration, LogLevel.Information, [], out var provider)) loggerFactory.AddProvider(provider); return loggerFactory; @@ -137,16 +138,15 @@ static bool TryCreateProvider( } var levelOverrides = new Dictionary(); - foreach (var overr in configuration.GetSection("LevelOverride").GetChildren()) + foreach (var levelOverride in configuration.GetSection("LevelOverride").GetChildren()) { - LogLevel value; - if (!Enum.TryParse(overr.Value, out value)) + if (!Enum.TryParse(levelOverride.Value, out LogLevel value)) { - SelfLog.WriteLine("The level override setting `{0}` for `{1}` is invalid", overr.Value, overr.Key); + SelfLog.WriteLine("The level override setting `{0}` for `{1}` is invalid", levelOverride.Value, levelOverride.Key); continue; } - levelOverrides[overr.Key] = value; + levelOverrides[levelOverride.Key] = value; } provider = CreateProvider(serverUrl, apiKey, minimumLevel, levelOverrides, enrichers); @@ -197,7 +197,7 @@ static SerilogLoggerProvider CreateProvider( var overrides = new Dictionary(); foreach (var levelOverride in levelOverrides) { - overrides.Add(levelOverride.Key, new LoggingLevelSwitch(levelOverride.Value)); + overrides[levelOverride.Key] = new LoggingLevelSwitch(levelOverride.Value); } overrideMap = new LevelOverrideMap(overrides, levelSwitch); @@ -209,7 +209,7 @@ static SerilogLoggerProvider CreateProvider( Period = TimeSpan.FromSeconds(2), }); - var logger = new Logger(batchingSink, new Enricher(enrichers ?? Array.Empty>()), batchingSink.Dispose, levelSwitch, overrideMap); + var logger = new Logger(batchingSink, new Enricher(enrichers ?? []), batchingSink.Dispose, levelSwitch, overrideMap); var provider = new SerilogLoggerProvider(logger); return provider; } diff --git a/src/Seq.Extensions.Logging/Seq.Extensions.Logging.csproj b/src/Seq.Extensions.Logging/Seq.Extensions.Logging.csproj index baaa76e..bdf66ca 100644 --- a/src/Seq.Extensions.Logging/Seq.Extensions.Logging.csproj +++ b/src/Seq.Extensions.Logging/Seq.Extensions.Logging.csproj @@ -13,11 +13,8 @@ icon.png https://github.com/datalust/seq-extensions-logging Apache-2.0 - latest README.md - enable - enable diff --git a/src/Seq.Extensions.Logging/Serilog/Parameters/MessageTemplateProcessor.cs b/src/Seq.Extensions.Logging/Serilog/Parameters/MessageTemplateProcessor.cs index a9d04b1..f982ebd 100644 --- a/src/Seq.Extensions.Logging/Serilog/Parameters/MessageTemplateProcessor.cs +++ b/src/Seq.Extensions.Logging/Serilog/Parameters/MessageTemplateProcessor.cs @@ -12,11 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System.Diagnostics.CodeAnalysis; using Serilog.Core; using Serilog.Core.Pipeline; using Serilog.Events; -using Serilog.Parsing; namespace Serilog.Parameters; diff --git a/test/Seq.Extensions.Logging.Tests/Seq.Extensions.Logging.Tests.csproj b/test/Seq.Extensions.Logging.Tests/Seq.Extensions.Logging.Tests.csproj index 13bd80f..267380a 100644 --- a/test/Seq.Extensions.Logging.Tests/Seq.Extensions.Logging.Tests.csproj +++ b/test/Seq.Extensions.Logging.Tests/Seq.Extensions.Logging.Tests.csproj @@ -6,7 +6,6 @@ true true Tests - latest diff --git a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnricherTests.cs b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnricherTests.cs index dbef5ae..18e64fd 100644 --- a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnricherTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnricherTests.cs @@ -1,7 +1,7 @@ -using System; using Serilog.Parameters; using Serilog.Events; using Seq.Extensions.Logging; +using Tests.Support; using Xunit; namespace Tests.Seq.Extensions.Logging; @@ -11,54 +11,38 @@ public class EnricherTests [Fact] public void EnrichersAreAppliedInOrder() { - var evt = new LogEvent( - default, - default, - default, - default, - new(), - default, - default - ); + var evt = Some.EmptyLogEvent(); new Enricher([ - (evt) => evt.AddPropertyIfAbsent("A", 1), - (evt) => evt.AddPropertyIfAbsent("A", 2), - (evt) => evt.AddOrUpdateProperty("B", 1), - (evt) => evt.AddOrUpdateProperty("B", 2), + enrichingEvent => enrichingEvent.AddPropertyIfAbsent("A", 1), + enrichingEvent => enrichingEvent.AddPropertyIfAbsent("A", 2), + enrichingEvent => enrichingEvent.AddOrUpdateProperty("B", 1), + enrichingEvent => enrichingEvent.AddOrUpdateProperty("B", 2), ]) .Enrich( evt, new PropertyValueConverter(int.MaxValue, int.MaxValue) ); - Assert.Equal(1, (evt.Properties["A"] as ScalarValue).Value); - Assert.Equal(2, (evt.Properties["B"] as ScalarValue).Value); + Assert.Equal(1, ((ScalarValue)evt.Properties["A"]).Value); + Assert.Equal(2, ((ScalarValue)evt.Properties["B"]).Value); } [Fact] public void FailingEnricherIsHandled() { - var evt = new LogEvent( - default, - default, - default, - default, - new(), - default, - default - ); + var evt = Some.EmptyLogEvent(); new Enricher([ - (evt) => evt.AddOrUpdateProperty("A", 1), + enrichingEvent => enrichingEvent.AddOrUpdateProperty("A", 1), _ => throw new Exception("Enricher Failed"), - (evt) => evt.AddOrUpdateProperty("A", 2), + enrichingEvent => enrichingEvent.AddOrUpdateProperty("A", 2), ]) .Enrich( evt, new PropertyValueConverter(int.MaxValue, int.MaxValue) ); - Assert.Equal(2, (evt.Properties["A"] as ScalarValue).Value); + Assert.Equal(2, ((ScalarValue)evt.Properties["A"]).Value); } } \ No newline at end of file diff --git a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnrichingEventTests.cs b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnrichingEventTests.cs index 640ee0f..5e05746 100644 --- a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnrichingEventTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/EnrichingEventTests.cs @@ -1,8 +1,8 @@ -using System; using Xunit; using Serilog.Parameters; using Serilog.Events; using Seq.Extensions.Logging; +using Tests.Support; namespace Tests.Seq.Extensions.Logging; @@ -12,43 +12,27 @@ public class EnrichingEventTests public void AddPropertyIfAbsentAddsProperties() { var enriching = new EnrichingEvent( - new LogEvent( - default, - default, - default, - default, - new(), - default, - default - ), + Some.EmptyLogEvent(), new PropertyValueConverter(int.MaxValue, int.MaxValue) ); enriching.AddPropertyIfAbsent("A", false); enriching.AddPropertyIfAbsent("A", true); - Assert.Equal(false, (enriching.LogEvent.Properties["A"] as ScalarValue).Value); + Assert.Equal(false, ((ScalarValue)enriching.LogEvent.Properties["A"]).Value); } [Fact] public void AddOrUpdatePropertyAddsProperties() { var enriching = new EnrichingEvent( - new LogEvent( - default, - default, - default, - default, - new(), - default, - default - ), + Some.EmptyLogEvent(), new PropertyValueConverter(int.MaxValue, int.MaxValue) ); enriching.AddOrUpdateProperty("A", false); enriching.AddOrUpdateProperty("A", true); - Assert.Equal(true, (enriching.LogEvent.Properties["A"] as ScalarValue).Value); + Assert.Equal(true, ((ScalarValue)enriching.LogEvent.Properties["A"]).Value); } } diff --git a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/ExceptionDataEnricherTests.cs b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/ExceptionDataEnricherTests.cs index 74dcf50..e598ca9 100644 --- a/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/ExceptionDataEnricherTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Seq/Extensions/Logging/ExceptionDataEnricherTests.cs @@ -1,7 +1,5 @@ using Seq.Extensions.Logging; using Serilog.Events; -using System; -using System.Linq; using Tests.Support; using Xunit; @@ -25,7 +23,7 @@ public void WhenNoDataIsPresentNoPropertyIsAdded() public void WhenDataIsPresentThePropertyIsAdded() { var enricher = new ExceptionDataEnricher(); - var exception = new Exception() + var exception = new Exception { Data = { diff --git a/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/SerilogLoggerTests.cs b/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/SerilogLoggerTests.cs index de3a925..fe7ac69 100644 --- a/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/SerilogLoggerTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/SerilogLoggerTests.cs @@ -1,14 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections; using Serilog.Events; using Microsoft.Extensions.Logging; -using System.Collections.Generic; using System.Diagnostics; -using System.IO; -using System.Linq; using Xunit; using Serilog.Extensions.Logging; using Seq.Extensions.Logging; @@ -34,7 +30,7 @@ static SerilogLoggerTests() { var sink = new SerilogSink(); - var l = new global::Serilog.Core.Logger(sink, new Enricher(enrichers), null, new global::Serilog.Core.LoggingLevelSwitch(logLevel), null); + var l = new global::Serilog.Core.Logger(sink, new Enricher(enrichers), null, new global::Serilog.Core.LoggingLevelSwitch(logLevel)); var provider = new SerilogLoggerProvider(l); provider.SetScopeProvider(new LoggerExternalScopeProvider()); @@ -138,9 +134,9 @@ public void LogsCorrectMessage() { var (logger, sink) = SetUp(LogLevel.Trace); - logger.Log(LogLevel.Information, 0, null, null, null!); + logger.Log(LogLevel.Information, 0, null, null, null!); logger.Log(LogLevel.Information, 0, TestMessage, null, null!); - logger.Log(LogLevel.Information, 0, null, null, (_, _) => TestMessage); + logger.Log(LogLevel.Information, 0, null, null, (_, _) => TestMessage); Assert.Equal(3, sink.Writes.Count); @@ -270,7 +266,7 @@ public void OverridesStateEventIdIfSpecified() const int expected = 3; - logger.Log[]>(LogLevel.Information, expected, state: [new("EventId", "Something")], exception: null, formatter: (s, e) => ""); + logger.Log[]>(LogLevel.Information, expected, state: [new("EventId", "Something")], exception: null, formatter: (_, _) => ""); Assert.Single(sink.Writes); @@ -397,14 +393,14 @@ public void EnrichersAreApplied() { var (logger, sink) = SetUp( LogLevel.Trace, - (evt) => evt.AddPropertyIfAbsent("EnrichedScalar", true), - (evt) => evt.AddPropertyIfAbsent("EnrichedObject", new { a = 1 }, true) + evt => evt.AddPropertyIfAbsent("EnrichedScalar", true), + evt => evt.AddPropertyIfAbsent("EnrichedObject", new { a = 1 }, true) ); logger.Log(LogLevel.Information, 0, TestMessage, null, null!); - Assert.Equal(true, (sink.Writes[0].Properties["EnrichedScalar"] as ScalarValue).Value); - Assert.Equal(1, ((sink.Writes[0].Properties["EnrichedObject"] as StructureValue).Properties[0].Value as ScalarValue).Value); + Assert.Equal(true, ((ScalarValue)sink.Writes[0].Properties["EnrichedScalar"]).Value); + Assert.Equal(1, ((ScalarValue)((StructureValue)sink.Writes[0].Properties["EnrichedObject"]).Properties[0].Value).Value); } class FoodScope : IEnumerable> @@ -450,8 +446,8 @@ IEnumerator IEnumerable.GetEnumerator() class Person { // ReSharper disable once UnusedAutoPropertyAccessor.Local - public string FirstName { get; set; } + public string? FirstName { get; set; } // ReSharper disable once UnusedAutoPropertyAccessor.Local - public string LastName { get; set; } + public string? LastName { get; set; } } } \ No newline at end of file diff --git a/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/Support/SerilogSink.cs b/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/Support/SerilogSink.cs index baa8cb8..3205cc7 100644 --- a/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/Support/SerilogSink.cs +++ b/test/Seq.Extensions.Logging.Tests/Serilog/Extensions/Logging/Support/SerilogSink.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Collections.Generic; using Serilog.Core; using Serilog.Events; using Xunit; diff --git a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/BatchedConnectionStatusTests.cs b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/BatchedConnectionStatusTests.cs index 366dd4d..29fe89d 100644 --- a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/BatchedConnectionStatusTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/BatchedConnectionStatusTests.cs @@ -12,12 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; using System.Globalization; -using Xunit; using Serilog.Sinks.PeriodicBatching; +using Xunit; -namespace Seq.Extensions.Logging.Tests.Serilog.Sinks; +namespace Tests.Serilog.Sinks; public class BatchedConnectionStatusTests { diff --git a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/ControlledLevelSwitchTests.cs b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/ControlledLevelSwitchTests.cs index ace60a5..4243d88 100644 --- a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/ControlledLevelSwitchTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/ControlledLevelSwitchTests.cs @@ -20,8 +20,7 @@ public void WhenTheServerSendsALevelTheSwitchIsAdjusted() [Fact] public void WhenTheServerSendsNoLevelTheSwitchIsNotInitiallyAdjusted() { - var lls = new LoggingLevelSwitch(LogLevel.Warning); - lls.MinimumLevel = LogLevel.Critical; + var lls = new LoggingLevelSwitch(LogLevel.Critical); var cls = new ControlledLevelSwitch(lls); cls.Update(null); Assert.Equal(LogLevel.Critical, lls.MinimumLevel); @@ -40,14 +39,14 @@ public void WhenTheServerSendsNoLevelTheSwitchIsResetIfPreviouslyAdjusted() [Fact] public void WithNoSwitchToControlAllEventsAreIncluded() { - var cls = new ControlledLevelSwitch(null); + var cls = new ControlledLevelSwitch(); Assert.True(cls.IsIncluded(Some.DebugEvent())); } [Fact] public void WithNoSwitchToControlEventsAreStillFiltered() { - var cls = new ControlledLevelSwitch(null); + var cls = new ControlledLevelSwitch(); cls.Update(LogLevel.Warning); Assert.True(cls.IsIncluded(Some.ErrorEvent())); Assert.False(cls.IsIncluded(Some.InformationEvent())); @@ -56,7 +55,7 @@ public void WithNoSwitchToControlEventsAreStillFiltered() [Fact] public void WithNoSwitchToControlAllEventsAreIncludedAfterReset() { - var cls = new ControlledLevelSwitch(null); + var cls = new ControlledLevelSwitch(); cls.Update(LogLevel.Warning); cls.Update(null); Assert.True(cls.IsIncluded(Some.DebugEvent())); @@ -77,7 +76,7 @@ public void WhenNotControllingASwitchTheControllerIsNotActive() } [Fact] - public void AfterServerControlhTheControllerIsAlwaysActive() + public void AfterServerControlTheControllerIsAlwaysActive() { var cls = new ControlledLevelSwitch(); diff --git a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/SeqPayloadFormatterTests.cs b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/SeqPayloadFormatterTests.cs index c780d67..85f521d 100644 --- a/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/SeqPayloadFormatterTests.cs +++ b/test/Seq.Extensions.Logging.Tests/Serilog/Sinks/Seq/SeqPayloadFormatterTests.cs @@ -1,8 +1,9 @@ -using Serilog.Sinks.Seq; +using Seq.Extensions.Logging; +using Serilog.Sinks.Seq; using Tests.Support; using Xunit; -namespace Seq.Extensions.Logging.Tests.Serilog.Sinks.Seq; +namespace Tests.Serilog.Sinks.Seq; public class SeqPayloadFormatterTests { diff --git a/test/Seq.Extensions.Logging.Tests/Support/Some.cs b/test/Seq.Extensions.Logging.Tests/Support/Some.cs index 2afae8d..5f6147a 100644 --- a/test/Seq.Extensions.Logging.Tests/Support/Some.cs +++ b/test/Seq.Extensions.Logging.Tests/Support/Some.cs @@ -1,20 +1,27 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Serilog.Events; +using Serilog.Events; using Xunit.Sdk; using Serilog.Core; using Microsoft.Extensions.Logging; using Serilog.Parameters; // ReSharper disable MemberCanBePrivate.Global -#nullable enable - namespace Tests.Support; static class Some { + public static LogEvent EmptyLogEvent() + { + return new LogEvent( + default, + default, + null, + new MessageTemplate("", []), + new(), + default, + default + ); + } + public static LogEvent LogEvent(string messageTemplate, params object?[] propertyValues) { return LogEvent(null, messageTemplate, propertyValues); @@ -25,11 +32,6 @@ public static LogEvent LogEvent(Exception? exception, string messageTemplate, pa return LogEvent(LogLevel.Information, exception, messageTemplate, propertyValues); } - public static ILogEventPropertyFactory PropertyFactory() - { - return new PropertyValueConverter(10, 1024); - } - public static ILogEventPropertyValueFactory PropertyValueFactory() { return new PropertyValueConverter(10, 1024); @@ -37,8 +39,9 @@ public static ILogEventPropertyValueFactory PropertyValueFactory() public static LogEvent LogEvent(LogLevel level, Exception? exception, string messageTemplate, params object?[] propertyValues) { - var log = new Logger(null!, null!, null); + var log = new Logger(null!, null!); #pragma warning disable Serilog004 // Constant MessageTemplate verifier + // ReSharper disable once ConvertIfStatementToReturnStatement if (!log.BindMessageTemplate(messageTemplate, propertyValues, out var template, out var properties)) #pragma warning restore Serilog004 // Constant MessageTemplate verifier {