From 7b10cbfb9f79460a0863e641f2b4e611dc158bfd Mon Sep 17 00:00:00 2001 From: Jaben Cargman Date: Fri, 20 Sep 2024 00:45:22 -0400 Subject: [PATCH] Fixup for format bug --- .../Common.Logging.Serilog.Tests.csproj | 23 +- .../SerilogCommonLoggerTests.cs | 184 ++++++------ .../SerilogPreformatterTests.cs | 269 +++++++++--------- .../SerilogVariableEnricherTests.cs | 152 +++++----- Common.Logging.Serilog.Tests/packages.config | 9 - Common.Logging.Serilog.sln | 4 +- src/Common.Logging.Serilog.csproj | 54 ++-- src/CommonLoggingSerilogHelpers.cs | 2 +- src/SerilogCommonLogger.cs | 33 ++- src/SerilogFactoryAdapter.cs | 2 +- src/SerilogInstanceWrapper.cs | 2 +- src/SerilogPreformatter.cs | 2 +- src/SerilogVariableContextEnricher.cs | 52 ++-- 13 files changed, 392 insertions(+), 396 deletions(-) delete mode 100644 Common.Logging.Serilog.Tests/packages.config diff --git a/Common.Logging.Serilog.Tests/Common.Logging.Serilog.Tests.csproj b/Common.Logging.Serilog.Tests/Common.Logging.Serilog.Tests.csproj index c7b6d1c..49ca5f1 100644 --- a/Common.Logging.Serilog.Tests/Common.Logging.Serilog.Tests.csproj +++ b/Common.Logging.Serilog.Tests/Common.Logging.Serilog.Tests.csproj @@ -1,24 +1,19 @@ - - netcoreapp2.2 - + net8.0 false - - - - - + + + + + - - - + - + - - + \ No newline at end of file diff --git a/Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs b/Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs index 2cc7b61..14d0017 100644 --- a/Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs +++ b/Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs @@ -6,96 +6,100 @@ using Serilog.Core; using Serilog.Events; -namespace Common.Logging.Serilog.Tests +namespace Common.Logging.Serilog.Tests; + +[TestFixture] +public class SerilogCommonLoggerTests { - [TestFixture] - public class SerilogCommonLoggerTests + [SetUp] + public void Setup() + { + _seriLogger = new Mock(); + + _seriLogger.Setup(s => s.ForContext(It.IsAny())) + .Returns(_seriLogger.Object); + _seriLogger.Setup(l => l.IsEnabled(It.IsAny())).Returns(true); + + _commonLogger = new SerilogCommonLogger(_seriLogger.Object); + } + + private Mock _seriLogger; + private SerilogCommonLogger _commonLogger; + + [Test] + public void Should_Leave_String_As_Is_If_Formatted_With_Serilog_Syntax() { - private Mock _seriLogger; - private SerilogCommonLogger _commonLogger; - - [SetUp] - public void Setup() - { - _seriLogger = new Mock(); - - this._seriLogger.Setup(s => s.ForContext(It.IsAny())).Returns(this._seriLogger.Object); - this._seriLogger.Setup(l => l.IsEnabled(It.IsAny())).Returns(true); - - _commonLogger = new SerilogCommonLogger(_seriLogger.Object); - } - - [Test] - public void Should_Leave_String_As_Is_If_Formatted_With_Serilog_Syntax() - { - /* Setup */ - const string templateString = "This is a {@serilog} formatted string"; - const string expectedString = templateString; - - var arg = new { Type = "Serilog"}; - var expectedArgs = new object[] {arg}; - _seriLogger - .Setup(l => l.Write( - It.IsAny(), - It.IsAny(), - expectedString, - expectedArgs - )) - .Verifiable(); - - /* Test */ - _commonLogger.DebugFormat(templateString, arg); - - /* Assert */ - _seriLogger.VerifyAll(); - } - - [Test] - public void Should_Preformat_String_If_Numerical_Formatted() - { - /* Setup */ - const string templateString = "This is a {0} formatted string"; - var arg = "nummeric"; - var expectedString = string.Format(templateString, arg); - - _seriLogger - .Setup(l => l.Write( - It.IsAny(), - It.IsAny(), - expectedString, - It.Is(s => !s.Any()) - )) - .Verifiable(); - - /* Test */ - _commonLogger.DebugFormat(templateString, arg); - - /* Assert */ - _seriLogger.VerifyAll(); - } - - [Test] - public void Should_Preformat_Numeric_Formatting_But_Leave_Serilog_Formating() - { - /* Setup */ - const string templateString = "This is a {0} formatted string with {@serilog} args, too"; - var args = new object[] { "nummeric", new { type = "Serilog"} }; - const string expectedString = "This is a nummeric formatted string with {@serilog} args, too"; - - _seriLogger - .Setup(l => l.Write( - It.IsAny(), - It.IsAny(), - expectedString, - It.Is(s => s.Count() == 1 && s[0] == args[1]) - )) - .Verifiable(); - - /* Test */ - _commonLogger.DebugFormat(templateString, args); - - /* Assert */ - _seriLogger.VerifyAll(); - } + /* Setup */ + const string templateString = "This is a {@serilog} formatted string"; + const string expectedString = templateString; + + var arg = new { Type = "Serilog" }; + var expectedArgs = new object[] { arg }; + _seriLogger + .Setup(l => l.Write( + It.IsAny(), + It.IsAny(), + expectedString, + expectedArgs + )) + .Verifiable(); + + /* Test */ + _commonLogger.DebugFormat(templateString, arg); + + /* Should be same result */ + _commonLogger.Debug(m => m(templateString, arg)); + + /* Assert */ + _seriLogger.VerifyAll(); + } + + [Test] + public void Should_Preformat_String_If_Numerical_Formatted() + { + /* Setup */ + const string templateString = "This is a {0} formatted string"; + var arg = "nummeric"; + var expectedString = string.Format(templateString, arg); + + _seriLogger + .Setup(l => l.Write( + It.IsAny(), + It.IsAny(), + expectedString, + It.Is(s => !s.Any()) + )) + .Verifiable(); + + /* Test */ + _commonLogger.DebugFormat(templateString, arg); + + /* Assert */ + _seriLogger.VerifyAll(); + } + + [Test] + public void Should_Preformat_Numeric_Formatting_But_Leave_Serilog_Formating() + { + /* Setup */ + const string templateString = "This is a {0} formatted string with {@serilog} args, too"; + var args = new object[] { "nummeric", new { type = "Serilog" } }; + const string expectedString = + "This is a nummeric formatted string with {@serilog} args, too"; + + _seriLogger + .Setup(l => l.Write( + It.IsAny(), + It.IsAny(), + expectedString, + It.Is(s => s.Count() == 1 && s[0] == args[1]) + )) + .Verifiable(); + + /* Test */ + _commonLogger.DebugFormat(templateString, args); + + /* Assert */ + _seriLogger.VerifyAll(); } -} +} \ No newline at end of file diff --git a/Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs b/Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs index 0471a8c..4c44939 100644 --- a/Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs +++ b/Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs @@ -1,135 +1,144 @@ using NUnit.Framework; -namespace Common.Logging.Serilog.Tests +namespace Common.Logging.Serilog.Tests; + +[TestFixture] +public class SerilogPreformatterTests { - [TestFixture] - public class SerilogPreformatterTests + [SetUp] + public void Setup() + { + _preformatter = new SerilogPreformatter(); + _resultTemplate = string.Empty; + _resultArgs = null; + } + + private SerilogPreformatter _preformatter; + private string _resultTemplate; + private object[] _resultArgs; + + [Test] + public void Should_Return_Same_Template_String_If_Original_String_Is_Null_Or_Empty() + { + /* Setup */ + const string originalTemplate = null; + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, null, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); + } + + [Test] + public void Should_Return_Same_Template_String_If_No_Args_Provided() + { + /* Setup */ + const string originalTemplate = "This is a template without any args"; + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, null, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); + } + + [Test] + public void Should_Format_Entire_String_If_Only_Numeric_Formatting_Is_Used() + { + /* Setup */ + const string originalTemplate = "Look at this, a {0} formatted string!"; + var args = new object[] { "nicely " }; + var expected = string.Format(originalTemplate, args); + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(expected)); + } + + [Test] + public void Should_Keep_String_Unformatted_If_Serilog_Formatting_Used() + { + /* Setup */ + const string originalTemplate = "Look at this, a {serilog} formatted string!"; + var args = new object[] { "serilog" }; + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); + } + + [Test] + public void + Should_Preformat_Numeric_Part_Of_String_But_Keep_Serilog_Formatted_String_And_Arguments() + { + /* Setup */ + const string originalTemplate = "This {@string} has {1} numeric match"; + var args = new object[] { "serilog", "1" }; + const string expectedTemplate = "This {@string} has 1 numeric match"; + + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(expectedTemplate)); + Assert.That(_resultArgs.Length, Is.EqualTo(1)); + Assert.That(_resultArgs[0], Is.EqualTo(args[0])); + } + + [Test] + public void Should_Be_Able_To_Correct_Format_Numeric_String_With_Several_Arguments() + { + /* Setup */ + const string originalTemplate = "Look at this, a {0} formatted string with {1} arguments!"; + var args = new object[] { "nicely", "2" }; + var expected = string.Format(originalTemplate, args); + + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(expected)); + } + + [TestCase("Two different numbers {Number1} and {Number2}!", new object[] { 0, 1 }, + "Two different numbers {Number1} and {Number2}!", new object[] { 0, 1 })] + [TestCase("Two duplicate numbers {Number1} and {Number2}!", new object[] { 1, 1 }, + "Two duplicate numbers {Number1} and {Number2}!", new object[] { 1, 1 })] + [TestCase("Three numbers, only one unique {Number1}, not {1}, and {Number2}!", + new object[] { 0, 42, 0 }, + "Three numbers, only one unique {Number1}, not 42, and {Number2}!", new object[] { 0, 0 })] + [TestCase("Reverse order numbers: {1} and {0}!", new object[] { 0, 1 }, + "Reverse order numbers: 1 and 0!", new object[0])] + [TestCase("Same number twice: {0} and {0}!", new object[] { 0 }, + "Same number twice: 0 and 0!", new object[0])] + public void Should_Be_Able_To_Correct_Format_Numeric_Arguments(string originalTemplate, + object[] args, string expectedResultTemplate, object[] expectedArgs) { - private SerilogPreformatter _preformatter; - private string _resultTemplate; - private object[] _resultArgs; - - [SetUp] - public void Setup() - { - _preformatter = new SerilogPreformatter(); - _resultTemplate = string.Empty; - _resultArgs = null; - } - - [Test] - public void Should_Return_Same_Template_String_If_Original_String_Is_Null_Or_Empty() - { - /* Setup */ - const string originalTemplate = null; - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, null, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); - } - - [Test] - public void Should_Return_Same_Template_String_If_No_Args_Provided() - { - /* Setup */ - const string originalTemplate = "This is a template without any args"; - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, null, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); - } - - [Test] - public void Should_Format_Entire_String_If_Only_Numeric_Formatting_Is_Used() - { - /* Setup */ - const string originalTemplate = "Look at this, a {0} formatted string!"; - var args = new object[] {"nicely "}; - var expected = string.Format(originalTemplate, args); - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(expected)); - } - - [Test] - public void Should_Keep_String_Unformatted_If_Serilog_Formatting_Used() - { - /* Setup */ - const string originalTemplate = "Look at this, a {serilog} formatted string!"; - var args = new object[] { "serilog" }; - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(originalTemplate)); - } - - [Test] - public void Should_Preformat_Numeric_Part_Of_String_But_Keep_Serilog_Formatted_String_And_Arguments() - { - /* Setup */ - const string originalTemplate = "This {@string} has {1} numeric match"; - var args = new object[] { "serilog", "1" }; - const string expectedTemplate = "This {@string} has 1 numeric match"; - - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(expectedTemplate)); - Assert.That(_resultArgs.Length, Is.EqualTo(1)); - Assert.That(_resultArgs[0], Is.EqualTo(args[0])); - } - - [Test] - public void Should_Be_Able_To_Correct_Format_Numeric_String_With_Several_Arguments() - { - /* Setup */ - const string originalTemplate = "Look at this, a {0} formatted string with {1} arguments!"; - var args = new object[] { "nicely", "2" }; - var expected = string.Format(originalTemplate, args); - - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(expected)); - } - - [TestCase("Two different numbers {Number1} and {Number2}!", new object[] { 0, 1 }, - "Two different numbers {Number1} and {Number2}!", new object[] { 0, 1 })] - [TestCase("Two duplicate numbers {Number1} and {Number2}!", new object[] { 1, 1 }, - "Two duplicate numbers {Number1} and {Number2}!", new object[] { 1, 1 })] - [TestCase("Three numbers, only one unique {Number1}, not {1}, and {Number2}!", new object[] { 0, 42, 0 }, - "Three numbers, only one unique {Number1}, not 42, and {Number2}!", new object[] { 0, 0 })] - [TestCase("Reverse order numbers: {1} and {0}!", new object[] { 0, 1 }, - "Reverse order numbers: 1 and 0!", new object[0])] - [TestCase("Same number twice: {0} and {0}!", new object[] { 0 }, - "Same number twice: 0 and 0!", new object[0])] - public void Should_Be_Able_To_Correct_Format_Numeric_Arguments(string originalTemplate, object[] args, string expectedResultTemplate, object[] expectedArgs) - { - /* Test */ - var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs); - - /* Assert */ - Assert.That(result, Is.True); - Assert.That(_resultTemplate, Is.EqualTo(expectedResultTemplate)); - Assert.That(_resultArgs, Is.EquivalentTo(expectedArgs)); - } + /* Test */ + var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, + out _resultArgs); + + /* Assert */ + Assert.That(result, Is.True); + Assert.That(_resultTemplate, Is.EqualTo(expectedResultTemplate)); + Assert.That(_resultArgs, Is.EquivalentTo(expectedArgs)); } -} +} \ No newline at end of file diff --git a/Common.Logging.Serilog.Tests/SerilogVariableEnricherTests.cs b/Common.Logging.Serilog.Tests/SerilogVariableEnricherTests.cs index 96ad57e..5fe86c3 100644 --- a/Common.Logging.Serilog.Tests/SerilogVariableEnricherTests.cs +++ b/Common.Logging.Serilog.Tests/SerilogVariableEnricherTests.cs @@ -1,111 +1,101 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Threading.Tasks; - +using FluentAssertions; using NUnit.Framework; - using Serilog; using Serilog.Core; using Serilog.Events; -namespace Common.Logging.Serilog.Tests +namespace Common.Logging.Serilog.Tests; + +public class TestSerilogSink : ILogEventSink { - public class TestSerilogSink : ILogEventSink - { - public Queue Events = new Queue(); + public Queue Events = new(); - public void Emit(LogEvent logEvent) - { - this.Events.Enqueue(logEvent); - } + public void Emit(LogEvent logEvent) + { + Events.Enqueue(logEvent); } - +} - [TestFixture] - public class SerilogVariableEnricherTests +[TestFixture] +public class SerilogVariableEnricherTests +{ + [Test] + public void GlobalVariableShouldEnrichLog() { - [Test] - public void GlobalVariableShouldEnrichLog() - { - var s = GetSerilog(); - - s.Logger.GlobalVariablesContext.Set("Var1", 1); - s.Logger.GlobalVariablesContext.Set("Var2", 2); + var s = GetSerilog(); - s.Logger.Info("Nothing"); + s.Logger.GlobalVariablesContext.Set("Var1", 1); + s.Logger.GlobalVariablesContext.Set("Var2", 2); - Assert.AreEqual(s.Sink.Events.Count, 1); + s.Logger.Info("Nothing"); - var @event = s.Sink.Events.Dequeue(); + s.Sink.Events.Count.Should().Be(1); - // count includes "message" - Assert.AreEqual(@event.Properties.Count, 3); - Assert.AreEqual(@event.Properties["Var1"].ToString(), "1"); - Assert.AreEqual(@event.Properties["Var2"].ToString(), "2"); + var @event = s.Sink.Events.Dequeue(); - // create new thread and verify that the variables are avaliable - Task.Factory.StartNew( - () => - { - s.Logger.Info("Fun"); - @event = s.Sink.Events.Dequeue(); + // count includes "message" + @event.Properties.Count.Should().Be(3); + @event.Properties["Var1"].ToString().Should().Be("1"); + @event.Properties["Var2"].ToString().Should().Be("2"); - Assert.AreEqual(@event.Properties.Count, 3); - }).Wait(); - } - - [Test] - public void ThreadVariableShouldEnrichLog() - { - var s = GetSerilog(); + // create new thread and verify that the variables are available + Task.Run( + () => + { + s.Logger.Info("Fun"); + @event = s.Sink.Events.Dequeue(); - s.Logger.ThreadVariablesContext.Set("Var1", 1); - s.Logger.ThreadVariablesContext.Remove("Var1"); - s.Logger.ThreadVariablesContext.Set("Var10", 10); + @event.Properties.Count.Should().Be(3); + }).Wait(); + } - s.Logger.Info("Nothing"); + [Test] + public void ThreadVariableShouldEnrichLog() + { + var s = GetSerilog(); - Assert.AreEqual(s.Sink.Events.Count, 1); + s.Logger.ThreadVariablesContext.Set("Var1", 1); + s.Logger.ThreadVariablesContext.Remove("Var1"); + s.Logger.ThreadVariablesContext.Set("Var10", 10); - var @event = s.Sink.Events.Dequeue(); + s.Logger.Info("Nothing"); - // count includes "message" - Assert.AreEqual(@event.Properties.Count, 2); - Assert.AreEqual(@event.Properties["Var10"].ToString(), "10"); + s.Sink.Events.Count.Should().Be(1); - // create new thread and verify that the variables are not avaliable - Task.Factory.StartNew( - () => - { - s.Logger.Info("Fun"); - @event = s.Sink.Events.Dequeue(); + var @event = s.Sink.Events.Dequeue(); - Assert.AreEqual(@event.Properties.Count, 1); - }).Wait(); - } + // count includes "message" + @event.Properties.Count.Should().Be(2); + @event.Properties["Var10"].ToString().Should().Be("10"); - static TestLoggerStructure GetSerilog() - { - var sink = new TestSerilogSink(); - var log = new LoggerConfiguration().WriteTo.Sink(sink).MinimumLevel.Verbose().CreateLogger(); - var logger = new SerilogCommonLogger(log); - logger.GlobalVariablesContext.Clear(); - logger.ThreadVariablesContext.Clear(); + // create new thread and verify that the variables are not available + Task.Run( + () => + { + s.Logger.Info("Fun"); + @event = s.Sink.Events.Dequeue(); - return new TestLoggerStructure(logger, sink); - } + @event.Properties.Count.Should().Be(1); + }).Wait(); + } - internal class TestLoggerStructure - { - public SerilogCommonLogger Logger { get; } - public TestSerilogSink Sink { get; } + private static TestLoggerStructure GetSerilog() + { + var sink = new TestSerilogSink(); + var log = new LoggerConfiguration().WriteTo.Sink(sink).MinimumLevel.Verbose() + .CreateLogger(); + var logger = new SerilogCommonLogger(log); + logger.GlobalVariablesContext.Clear(); + logger.ThreadVariablesContext.Clear(); + + return new TestLoggerStructure(logger, sink); + } - public TestLoggerStructure(SerilogCommonLogger logger, TestSerilogSink sink) - { - this.Logger = logger; - this.Sink = sink; - } - } + internal class TestLoggerStructure(SerilogCommonLogger logger, TestSerilogSink sink) + { + public SerilogCommonLogger Logger { get; } = logger; + public TestSerilogSink Sink { get; } = sink; } } \ No newline at end of file diff --git a/Common.Logging.Serilog.Tests/packages.config b/Common.Logging.Serilog.Tests/packages.config deleted file mode 100644 index d58b0bf..0000000 --- a/Common.Logging.Serilog.Tests/packages.config +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Common.Logging.Serilog.sln b/Common.Logging.Serilog.sln index 67e01c1..45fcd57 100644 --- a/Common.Logging.Serilog.sln +++ b/Common.Logging.Serilog.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26127.0 @@ -14,7 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Assets", "Assets", "{D6378B content\web.config.transform = content\web.config.transform EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Logging.Serilog.Tests", "Common.Logging.Serilog.Tests\Common.Logging.Serilog.Tests.csproj", "{E7035ACA-D59B-4CA2-9606-E7634C5D590F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Logging.Serilog.Tests", "Common.Logging.Serilog.Tests\Common.Logging.Serilog.Tests.csproj", "{E7035ACA-D59B-4CA2-9606-E7634C5D590F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Common.Logging.Serilog.csproj b/src/Common.Logging.Serilog.csproj index 7879602..7e276be 100644 --- a/src/Common.Logging.Serilog.csproj +++ b/src/Common.Logging.Serilog.csproj @@ -1,39 +1,19 @@ - - - - - Common.Logging.Serilog - net452;net461;netstandard2.0 - Common.Logging.Serilog - Copyright © CaptiveAire Systems 2015-2017 - Common.Logging.Serilog.Tests - 4.3.0 - 4.3.0 - - - - - - - - - - - - - - - - + + + + Common.Logging.Serilog + net461;netstandard2.0 + Common.Logging.Serilog + Copyright © CaptiveAire Systems 2015-2017 + Common.Logging.Serilog.Tests + 5.0.0 + 5.0.0 + + + + + + + - - - - \ No newline at end of file diff --git a/src/CommonLoggingSerilogHelpers.cs b/src/CommonLoggingSerilogHelpers.cs index fdf6713..08f2485 100644 --- a/src/CommonLoggingSerilogHelpers.cs +++ b/src/CommonLoggingSerilogHelpers.cs @@ -1,4 +1,4 @@ -// Copyright 2014-2017 CaptiveAire Systems +// Copyright 2014-2024 CaptiveAire Systems // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/src/SerilogCommonLogger.cs b/src/SerilogCommonLogger.cs index c58d8a4..8cfc4dc 100644 --- a/src/SerilogCommonLogger.cs +++ b/src/SerilogCommonLogger.cs @@ -1,4 +1,16 @@ -// Common.Logging.Serilog - Copyright (c) 2017 CaptiveAire +// Copyright 2014-2024 CaptiveAire Systems +// +// 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; @@ -558,12 +570,17 @@ protected FormatMessageHandler MakeFormatted(LogLevel level, IFormatProvider for var messageHandler = new FormatMessageHandler( delegate(string message, object[] parameters) { - string formatted = string.Format(formatProvider, message, parameters); - if (formatProvider == null) - WriteFormat(level, exception, message, parameters); - else - Write(level, exception, formatted); + { + _preformatter.TryPreformat(message, parameters, out var serilogTemplate, out var serilogArgs); + _logger.Write(level.ToSerilogEventLevel(), exception, serilogTemplate, serilogArgs); + + return serilogTemplate; + } + + var formatted = string.Format(formatProvider, message, parameters); + + Write(level, exception, formatted); return formatted; }); @@ -590,9 +607,7 @@ protected void WriteFormat(LogLevel level, Exception exception, IFormatProvider { if (formatProvider == null) { - string serilogTemplate; - object[] serilogArgs; - _preformatter.TryPreformat(message, parameters, out serilogTemplate, out serilogArgs); + _preformatter.TryPreformat(message, parameters, out var serilogTemplate, out var serilogArgs); _logger.Write(level.ToSerilogEventLevel(), exception, serilogTemplate, serilogArgs); } else diff --git a/src/SerilogFactoryAdapter.cs b/src/SerilogFactoryAdapter.cs index 6ffb859..0979aad 100644 --- a/src/SerilogFactoryAdapter.cs +++ b/src/SerilogFactoryAdapter.cs @@ -1,4 +1,4 @@ -// Copyright 2014-2017 CaptiveAire Systems +// Copyright 2014-2024 CaptiveAire Systems // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/src/SerilogInstanceWrapper.cs b/src/SerilogInstanceWrapper.cs index f89a887..de8e9c0 100644 --- a/src/SerilogInstanceWrapper.cs +++ b/src/SerilogInstanceWrapper.cs @@ -1,4 +1,4 @@ -// Copyright 2014-2017 CaptiveAire Systems +// Copyright 2014-2024 CaptiveAire Systems // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/src/SerilogPreformatter.cs b/src/SerilogPreformatter.cs index fc62fe8..602ca69 100644 --- a/src/SerilogPreformatter.cs +++ b/src/SerilogPreformatter.cs @@ -1,4 +1,4 @@ -// Copyright 2014-2017 CaptiveAire Systems +// Copyright 2014-2024 CaptiveAire Systems // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/src/SerilogVariableContextEnricher.cs b/src/SerilogVariableContextEnricher.cs index 2f07a78..48f4edf 100644 --- a/src/SerilogVariableContextEnricher.cs +++ b/src/SerilogVariableContextEnricher.cs @@ -1,8 +1,20 @@ -using System.Collections; +// Copyright 2014-2024 CaptiveAire Systems +// +// 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.Collections.Concurrent; using System.Collections.Generic; using System.Threading; - using Serilog.Core; using Serilog.Events; @@ -10,7 +22,8 @@ namespace Common.Logging.Serilog { public class SerilogVariableContextEnricher : ILogEventEnricher { - public static readonly ConcurrentVariableContext GlobalVariablesContext = new ConcurrentVariableContext(); + public static readonly ConcurrentVariableContext GlobalVariablesContext = + new ConcurrentVariableContext(); public static readonly ThreadLocal ThreadLocal = new ThreadLocal(() => new ConcurrentVariableContext()); @@ -21,15 +34,13 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { // enrich with global variables first... foreach (var globalVar in GlobalVariablesContext.GetAll()) - { - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(globalVar.Key, globalVar.Value, true)); - } + logEvent.AddPropertyIfAbsent( + propertyFactory.CreateProperty(globalVar.Key, globalVar.Value, true)); // then enrich with the thread varables foreach (var threadVar in ThreadLocal.Value.GetAll()) - { - logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(threadVar.Key, threadVar.Value, true)); - } + logEvent.AddPropertyIfAbsent( + propertyFactory.CreateProperty(threadVar.Key, threadVar.Value, true)); } catch { @@ -39,38 +50,39 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) public class ConcurrentVariableContext : IVariablesContext { - readonly ConcurrentDictionary _variables = new ConcurrentDictionary(); - - public IReadOnlyList> GetAll() - { - return this._variables.ToArray(); - } + private readonly ConcurrentDictionary _variables = + new ConcurrentDictionary(); public void Set(string key, object value) { - this._variables[key] = value; + _variables[key] = value; } public object Get(string key) { object value; - return this._variables.TryGetValue(key, out value) ? value : null; + return _variables.TryGetValue(key, out value) ? value : null; } public bool Contains(string key) { - return this._variables.ContainsKey(key); + return _variables.ContainsKey(key); } public void Remove(string key) { object dummy; - this._variables.TryRemove(key, out dummy); + _variables.TryRemove(key, out dummy); } public void Clear() { - this._variables.Clear(); + _variables.Clear(); + } + + public IReadOnlyList> GetAll() + { + return _variables.ToArray(); } } }