From 682857097f3eabb04bd8e30fa88bb9c7a2512612 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Mon, 23 Sep 2024 22:23:19 +0200 Subject: [PATCH 01/12] Update CHANGELOG.MD --- CHANGELOG.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index c27bc49b..3ca4741a 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -2,7 +2,7 @@ Date format: (year/month/day) -### Version 5.3.14 (2024/09/12) +### Version 5.3.14 (2024/09/23) **Improvements** - [#765](https://github.com/NLog/NLog.Extensions.Logging/pull/765): UseNLog should fallback after checking ContentRoot for NLog.config (#765) (@snakefoot) From b5675a06c7a9e28047ea43480f8f6ca0da899d64 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:53:19 +0200 Subject: [PATCH 02/12] Bump xunit from 2.9.0 to 2.9.2 (#767) Bumps [xunit](https://github.com/xunit/xunit) from 2.9.0 to 2.9.2. - [Commits](https://github.com/xunit/xunit/compare/2.9.0...2.9.2) --- updated-dependencies: - dependency-name: xunit dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../NLog.Extensions.Hosting.Tests.csproj | 2 +- .../NLog.Extensions.Logging.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj index 3e592be6..8fdc520b 100644 --- a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj +++ b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj index 2cd88891..9a504d1b 100644 --- a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj +++ b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 71af10a9a3712d80ba4bbff512b637309438258a Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 27 Oct 2024 17:37:10 +0100 Subject: [PATCH 03/12] BeginScope to recognize ITuple with better support for AOT (#768) * BeginScope to recognize ITuple with better support for AOT * Update NET8 to fix vulnerable packages * Fixed unit-test warnings about using Assert.Single with Predicate --- appveyor.yml | 4 +- .../ConsoleExample/ConsoleExample.csproj | 6 +- .../ConsoleExampleJsonConfig.csproj | 6 +- .../HostingExample/HostingExample.csproj | 2 +- .../Logging/NLogBeginScopeParser.cs | 6 ++ .../NLog.Extensions.Hosting.Tests.csproj | 4 +- .../NLog.Extensions.Logging.Tests.csproj | 11 +--- .../NLogLoggingConfigurationTests.cs | 64 +++++++++---------- 8 files changed, 52 insertions(+), 51 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index a359ed21..8e25f204 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -23,11 +23,11 @@ artifacts: test_script: - nuget.exe install OpenCover -ExcludeVersion -DependencyVersion Ignore - OpenCover\tools\OpenCover.Console.exe -register:user -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net462 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.1 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net6.0 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net8.0 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net462 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.1 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net6.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net8.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - pip install codecov - codecov -f "coverage.xml" - set PATH=%JAVA_HOME%\bin;%PATH% diff --git a/examples/NetCore2/ConsoleExample/ConsoleExample.csproj b/examples/NetCore2/ConsoleExample/ConsoleExample.csproj index 73f85b86..7ec32385 100644 --- a/examples/NetCore2/ConsoleExample/ConsoleExample.csproj +++ b/examples/NetCore2/ConsoleExample/ConsoleExample.csproj @@ -2,14 +2,14 @@ Exe - net6.0 + net8.0 latest false - - + + diff --git a/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj b/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj index 8472a4a9..ce1719f2 100644 --- a/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj +++ b/examples/NetCore2/ConsoleExampleJsonConfig/ConsoleExampleJsonConfig.csproj @@ -2,14 +2,14 @@ Exe - net6.0 + net8.0 latest false - - + + diff --git a/examples/NetCore2/HostingExample/HostingExample.csproj b/examples/NetCore2/HostingExample/HostingExample.csproj index 09fb5d08..47d7f3a8 100644 --- a/examples/NetCore2/HostingExample/HostingExample.csproj +++ b/examples/NetCore2/HostingExample/HostingExample.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs index fd8b873b..f784d4fd 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs @@ -45,6 +45,12 @@ public IDisposable ParseBeginScope(T state) else return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties); } +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET471_OR_GREATER + else if (state is System.Runtime.CompilerServices.ITuple tuple && tuple.Length == 2 && tuple[0] is string) + { + return ScopeContext.PushProperty(tuple[0].ToString(), tuple[1]); + } +#endif if (!(state is string)) { diff --git a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj index 8fdc520b..c1b89093 100644 --- a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj +++ b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj @@ -1,6 +1,6 @@  - net462;net6.0 + net462;net8.0 Library false full @@ -21,7 +21,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj index 9a504d1b..8f5bfdb3 100644 --- a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj +++ b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj @@ -1,7 +1,7 @@  - net462;net6.0 + net462;net8.0 Library false Full @@ -15,7 +15,7 @@ - + @@ -26,14 +26,9 @@ - + - - - - - diff --git a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs index dcbae6a0..0ab3881b 100644 --- a/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs +++ b/test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs @@ -33,8 +33,8 @@ public void LoadSimpleConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -100,8 +100,8 @@ public void LoadSimpleConfigWithCustomKey() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -117,8 +117,8 @@ public void LoadSimpleConfigAndTrimSpace() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -135,9 +135,9 @@ public void LoadWrapperConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(3, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper)); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("wrappedFile") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -154,9 +154,9 @@ public void LoadWrapperConfigExplicitName() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(3, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper)); - Assert.Single(logConfig.AllTargets.Where(t => t is MemoryTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper); + Assert.Single(logConfig.AllTargets, (t) => t is MemoryTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.NotNull(logConfig.FindTargetByName("wrappedMem") as MemoryTarget); } @@ -171,8 +171,8 @@ public void LoadWrapperConfigWithoutName() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper)); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper); Assert.True(logConfig.FindTargetByName("file")?.WrappedTarget is MemoryTarget); } @@ -189,8 +189,8 @@ public void LoadVariablesConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -209,8 +209,8 @@ public void LoadVariablesDependentConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello/world.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -234,8 +234,8 @@ public void LoadVariableJsonLayoutConfig() Assert.Single(logConfig.Variables); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal(2, logConfig.AllTargets.Count(t => (t as TargetWithLayout)?.Layout is NLog.Layouts.JsonLayout)); } @@ -259,8 +259,8 @@ public void LoadVariableDependentJsonLayoutConfig() Assert.Equal(3, logConfig.Variables.Count); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal(2, logConfig.AllTargets.Count(t => (t as TargetWithLayout)?.Layout is NLog.Layouts.JsonLayout)); } @@ -278,8 +278,8 @@ public void LoadDefaultWrapperConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, (logConfig.AllTargets.Count(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.BatchSize == 1))); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is ConsoleTarget); Assert.Equal("hello.txt", ((logConfig.FindTargetByName("file") as AsyncTargetWrapper)?.WrappedTarget as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -297,8 +297,8 @@ public void LoadTargetDefaultWrapperConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, (logConfig.AllTargets.Count(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.BatchSize == 1))); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is AsyncTargetWrapper asyncTarget && asyncTarget.WrappedTarget is ConsoleTarget); Assert.Equal("hello.txt", ((logConfig.FindTargetByName("file") as AsyncTargetWrapper)?.WrappedTarget as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); } @@ -315,8 +315,8 @@ public void LoadDefaultTargetParametersConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName?.Render(LogEventInfo.CreateNullEvent())); Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.StdErr); } @@ -334,8 +334,8 @@ public void LoadTargetDefaultParametersConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName?.Render(LogEventInfo.CreateNullEvent())); Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.StdErr); } @@ -359,8 +359,8 @@ public void LoadDefaultTargetParametersJsonLayoutConfig() Assert.Single(logConfig.LoggingRules); Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count); Assert.Equal(2, logConfig.AllTargets.Count); - Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget)); - Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget)); + Assert.Single(logConfig.AllTargets, (t) => t is FileTarget); + Assert.Single(logConfig.AllTargets, (t) => t is ConsoleTarget); Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName.Render(LogEventInfo.CreateNullEvent())); Assert.Equal(3, ((logConfig.FindTargetByName("file") as FileTarget)?.Layout as NLog.Layouts.JsonLayout)?.Attributes?.Count); } From e45bddd4e5f724c873b57a64967f0a5ea621ad1e Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 6 Nov 2024 23:06:58 +0100 Subject: [PATCH 04/12] NLogMessageParameterList - Simplify the index operator (#770) --- .../Logging/NLogMessageParameterList.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs index dd9da4ef..f54e532e 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs @@ -179,10 +179,7 @@ public MessageTemplateParameter this[int index] { get { - if (index >= _originalMessageIndex) - index += 1; - - var parameter = _parameterList[index]; + var parameter = _parameterList[index >= _originalMessageIndex ? index + 1 : index]; return _hasComplexParameters ? GetMessageTemplateParameter(parameter.Key, parameter.Value) : new MessageTemplateParameter(parameter.Key, parameter.Value, null, CaptureType.Normal); From 0badef854036a22aa3f2411fb08b5368a54799ed Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 7 Nov 2024 18:02:13 +0100 Subject: [PATCH 05/12] NLogMessageParameterList - Changed _originalMessageIndex from nullable to magic value (#771) --- .../Logging/NLogMessageParameterList.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs index f54e532e..4727d12a 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs @@ -16,19 +16,19 @@ internal class NLogMessageParameterList : IList private static readonly NLogMessageParameterList OriginalMessageList = new NLogMessageParameterList(new[] { new KeyValuePair(NLogLogger.OriginalFormatPropertyName, string.Empty) }, 0, default, default); private readonly IReadOnlyList> _parameterList; - private readonly int? _originalMessageIndex; + private readonly int _originalMessageIndex; private readonly bool _hasComplexParameters; private readonly bool _isPositional; public bool HasComplexParameters => _hasComplexParameters; public bool IsPositional => _isPositional; - public int Count => _parameterList.Count - (_originalMessageIndex.HasValue ? 1 : 0); + public int Count => _originalMessageIndex != int.MaxValue ? _parameterList.Count - 1 : _parameterList.Count; public bool IsReadOnly => true; private NLogMessageParameterList(IReadOnlyList> parameterList, int? originalMessageIndex, bool hasComplexParameters, bool isPositional) { _parameterList = parameterList; - _originalMessageIndex = originalMessageIndex; + _originalMessageIndex = originalMessageIndex ?? int.MaxValue; _hasComplexParameters = hasComplexParameters; _isPositional = isPositional; } @@ -72,14 +72,14 @@ public static NLogMessageParameterList TryParse(IReadOnlyList 0)); + return _originalMessageIndex != int.MaxValue && (HasComplexParameters || (parseMessageTemplates && _parameterList.Count > 1)); } public string GetOriginalMessage(IReadOnlyList> messageProperties) { if (_originalMessageIndex < messageProperties?.Count) { - return messageProperties[_originalMessageIndex.Value].Value as string; + return messageProperties[_originalMessageIndex].Value as string; } return null; } @@ -179,7 +179,7 @@ public MessageTemplateParameter this[int index] { get { - var parameter = _parameterList[index >= _originalMessageIndex ? index + 1 : index]; + var parameter = _parameterList[index < _originalMessageIndex ? index : index + 1]; return _hasComplexParameters ? GetMessageTemplateParameter(parameter.Key, parameter.Value) : new MessageTemplateParameter(parameter.Key, parameter.Value, null, CaptureType.Normal); From 95932f45bda82537a6ef8271b84d880ea7c1d788 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:45:05 +0100 Subject: [PATCH 06/12] Bump Microsoft.NET.Test.Sdk from 17.11.1 to 17.12.0 (#772) Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.11.1 to 17.12.0. - [Release notes](https://github.com/microsoft/vstest/releases) - [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md) - [Commits](https://github.com/microsoft/vstest/compare/v17.11.1...v17.12.0) --- updated-dependencies: - dependency-name: Microsoft.NET.Test.Sdk dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../NLog.Extensions.Hosting.Tests.csproj | 2 +- .../NLog.Extensions.Logging.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj index c1b89093..213220ed 100644 --- a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj +++ b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj @@ -15,7 +15,7 @@ - + all diff --git a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj index 8f5bfdb3..57374bc2 100644 --- a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj +++ b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj @@ -16,7 +16,7 @@ - + all From c9f4aa954ce94359a76f4daf62282cc5446e78da Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 21 Nov 2024 07:47:58 +0100 Subject: [PATCH 07/12] UseNLog fallback to only EnvironmentName when loading NLog config (#773) --- .../Extensions/ConfigureExtensions.cs | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs index 07d3126b..3b51b3dd 100644 --- a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Reflection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -53,45 +54,56 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv { NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null); - var contentRootPath = hostEnvironment?.ContentRootPath; - if (!string.IsNullOrWhiteSpace(contentRootPath)) + string nlogConfigFile = string.Empty; + string contentRootPath = hostEnvironment?.ContentRootPath; + string environmentName = hostEnvironment?.EnvironmentName; + if (!string.IsNullOrWhiteSpace(contentRootPath) || !string.IsNullOrWhiteSpace(environmentName)) { - TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment?.EnvironmentName); + provider.LogFactory.Setup().LoadConfiguration(cfg => + { + if (!IsLoggingConfigurationLoaded(cfg.Configuration)) + { + nlogConfigFile = ResolveEnvironmentNLogConfigFile(contentRootPath, environmentName); + cfg.Configuration = null; + } + }); } + if (!string.IsNullOrEmpty(nlogConfigFile)) + { + provider.LogFactory.Setup().LoadConfigurationFromFile(nlogConfigFile, optional: true); + } + provider.LogFactory.Setup().SetupLogFactory(ext => ext.AddCallSiteHiddenAssembly(typeof(ConfigureExtensions).Assembly)); return provider; } - private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName) + private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName) { - logFactory.Setup().LoadConfiguration(config => + if (!string.IsNullOrWhiteSpace(basePath)) { - if (IsLoggingConfigurationLoaded(config.Configuration)) - return; - - if (!string.IsNullOrEmpty(environmentName)) - { - var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ?? - LoadXmlLoggingConfigurationFromPath(contentRootPath, $"nlog.{environmentName}.config", config.LogFactory) ?? - LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ?? - LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory); - config.Configuration = nlogConfig; - } - else + if (!string.IsNullOrWhiteSpace(environmentName)) { - var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ?? - LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory); - config.Configuration = nlogConfig; + var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environmentName}.config"); + if (File.Exists(nlogConfigEnvFilePath)) + return Path.GetFullPath(nlogConfigEnvFilePath); + nlogConfigEnvFilePath = Path.Combine(basePath, $"NLog.{environmentName}.config"); + if (File.Exists(nlogConfigEnvFilePath)) + return Path.GetFullPath(nlogConfigEnvFilePath); } - }); - } - private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory) - { - var standardPath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName); - return System.IO.File.Exists(standardPath) ? - new XmlLoggingConfiguration(standardPath, logFactory) : null; + var nlogConfigFilePath = Path.Combine(basePath, "nlog.config"); + if (File.Exists(nlogConfigFilePath)) + return Path.GetFullPath(nlogConfigFilePath); + nlogConfigFilePath = Path.Combine(basePath, "NLog.config"); + if (File.Exists(nlogConfigFilePath)) + return Path.GetFullPath(nlogConfigFilePath); + } + + if (!string.IsNullOrWhiteSpace(environmentName)) + return $"nlog.{environmentName}.config"; + + return null; } private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg) From b28d631f30875d4c95c78acd4990238944f66ef2 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 21 Nov 2024 07:48:10 +0100 Subject: [PATCH 08/12] Version 5.3.15 (#774) --- CHANGELOG.MD | 8 ++++++++ build.ps1 | 2 +- .../NLog.Extensions.Logging.csproj | 7 ++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 3ca4741a..6de6b43f 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -2,6 +2,14 @@ Date format: (year/month/day) +### Version 5.3.15 (2024/11/21) + +**Improvements** +- [#773](https://github.com/NLog/NLog.Extensions.Logging/pull/773): UseNLog allow fallback to only EnvironmentName for NLog config (#773) (@snakefoot) +- [#771](https://github.com/NLog/NLog.Extensions.Logging/pull/771): NLogMessageParameterList - Optimize the index operator (#771) (@snakefoot) +- [#770](https://github.com/NLog/NLog.Extensions.Logging/pull/770): NLogMessageParameterList - Simplify the index operator (#770) (@snakefoot) +- [#768](https://github.com/NLog/NLog.Extensions.Logging/pull/768): Recognize ITuple for better AOT support (#768) (@snakefoot) + ### Version 5.3.14 (2024/09/23) **Improvements** diff --git a/build.ps1 b/build.ps1 index 3ecc879b..2ec3cc11 100644 --- a/build.ps1 +++ b/build.ps1 @@ -2,7 +2,7 @@ # creates NuGet package at \artifacts dotnet --version -$versionPrefix = "5.3.14" +$versionPrefix = "5.3.15" $versionSuffix = "" $versionFile = $versionPrefix + "." + ${env:APPVEYOR_BUILD_NUMBER} $versionProduct = $versionPrefix; diff --git a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj index ecd8c2a7..9cf0cdb0 100644 --- a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj +++ b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj @@ -20,9 +20,10 @@ For ASP.NET Core, check: https://www.nuget.org/packages/NLog.Web.AspNetCore ChangeLog: -- UseNLog should fallback after checking ContentRoot for NLog.config (#765) (@snakefoot) -- NLogLoggingConfiguration - Fix memory-leak when using AutoReload (#761) (@snakefoot) -- Updated to NLog v5.3.4 (#761) (@snakefoot) +- UseNLog allow fallback to only EnvironmentName for NLog config (#773) (@snakefoot) +- NLogMessageParameterList - Optimize the index operator (#771) (@snakefoot) +- NLogMessageParameterList - Simplify the index operator (#770) (@snakefoot) +- Recognize ITuple for better AOT support (#768) (@snakefoot) Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANGELOG.MD From a8f22ff1b73143b8a6c6bb9e6ee70e89078f0344 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 24 Nov 2024 19:06:17 +0100 Subject: [PATCH 09/12] MicrosoftConsoleJsonLayout - TimestampLayout replaced by DateLayoutRenderer (#775) --- .../Layouts/MicrosoftConsoleJsonLayout.cs | 93 ++----------------- 1 file changed, 8 insertions(+), 85 deletions(-) diff --git a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs index 0e46a600..1ec1e1b8 100644 --- a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs +++ b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using NLog.Config; +using NLog.LayoutRenderers; using NLog.Layouts; namespace NLog.Extensions.Logging @@ -17,12 +17,14 @@ public class MicrosoftConsoleJsonLayout : JsonLayout { private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray(); + private readonly SimpleLayout _timestampLayout = new SimpleLayout("${date:format=o:universalTime=true}"); + /// /// Initializes a new instance of the class. /// public MicrosoftConsoleJsonLayout() { - Attributes.Add(new JsonAttribute("Timestamp", new TimestampLayout())); + Attributes.Add(new JsonAttribute("Timestamp", _timestampLayout)); Attributes.Add(new JsonAttribute("EventId", Layout.FromMethod(evt => LookupEventId(evt), LayoutRenderOptions.ThreadAgnostic)) { Encode = false }); Attributes.Add(new JsonAttribute("LogLevel", Layout.FromMethod(evt => ConvertLogLevel(evt.Level), LayoutRenderOptions.ThreadAgnostic))); Attributes.Add(new JsonAttribute("Category", "${logger}")); @@ -77,7 +79,7 @@ public string TimestampFormat get { var index = LookupNamedAttributeIndex("Timestamp"); - return index >= 0 ? (Attributes[index].Layout as TimestampLayout)?.TimestampFormat : null; + return index >= 0 ? ((Attributes[index].Layout as SimpleLayout)?.LayoutRenderers?.FirstOrDefault() as DateLayoutRenderer)?.Format : null; } set { @@ -89,7 +91,9 @@ public string TimestampFormat if (!string.IsNullOrEmpty(value)) { - Attributes.Insert(0, new JsonAttribute("Timestamp", new TimestampLayout() { TimestampFormat = value })); + var dateLayoutRenderer = _timestampLayout.LayoutRenderers.First() as DateLayoutRenderer; + dateLayoutRenderer.Format = value; + Attributes.Insert(0, new JsonAttribute("Timestamp", _timestampLayout)); } } } @@ -152,86 +156,5 @@ private static string ConvertLogLevel(LogLevel logLevel) else return nameof(Microsoft.Extensions.Logging.LogLevel.Critical); } - - [ThreadAgnostic] - private sealed class TimestampLayout : Layout - { - public string TimestampFormat - { - get => _timestampFormat; - set - { - if ("o".Equals(value)) - value = "O"; - _timestampFormat = value; - _timestampFormatString = string.IsNullOrEmpty(value) ? null : $"{{0:{value}}}"; - } - } - private string _timestampFormat; - private string _timestampFormatString; - - public TimestampLayout() - { - TimestampFormat = "O"; // Round-trip - ISO8601 - yyyy-MM-ddTHH:mm:ss.fffffffZ - } - - protected override string GetFormattedMessage(LogEventInfo logEvent) - { - return _timestampFormatString != null ? logEvent.TimeStamp.ToUniversalTime().ToString(TimestampFormat, System.Globalization.CultureInfo.InvariantCulture) : String.Empty; - } - - protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) - { - var utcTimestamp = logEvent.TimeStamp.ToUniversalTime(); - if ("O".Equals(TimestampFormat)) - { - // yyyy-MM-ddTHH:mm:ss.fffffffZ - Append4DigitsZeroPadded(target, utcTimestamp.Year); - target.Append('-'); - Append2DigitsZeroPadded(target, utcTimestamp.Month); - target.Append('-'); - Append2DigitsZeroPadded(target, utcTimestamp.Day); - target.Append('T'); - Append2DigitsZeroPadded(target, utcTimestamp.Hour); - target.Append(':'); - Append2DigitsZeroPadded(target, utcTimestamp.Minute); - target.Append(':'); - Append2DigitsZeroPadded(target, utcTimestamp.Second); - target.Append('.'); - var ticks = (int)(utcTimestamp.Ticks % 10000000); - Append7DigitsZeroPadded(target, ticks); - target.Append('Z'); - } - else if (_timestampFormatString != null) - { - target.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, _timestampFormatString, utcTimestamp); - } - } - - internal static void Append2DigitsZeroPadded(StringBuilder builder, int number) - { - builder.Append((char)((number / 10) + '0')); - builder.Append((char)((number % 10) + '0')); - } - - internal static void Append4DigitsZeroPadded( StringBuilder builder, int number) - { - builder.Append((char)(((number / 1000) % 10) + '0')); - builder.Append((char)(((number / 100) % 10) + '0')); - builder.Append((char)(((number / 10) % 10) + '0')); - builder.Append((char)((number % 10) + '0')); - } - - internal static void Append7DigitsZeroPadded(StringBuilder builder, int number) - { - builder.Append((char)(((number / 1000000) % 10) + '0')); - builder.Append((char)(((number / 100000) % 10) + '0')); - builder.Append((char)(((number / 10000) % 10) + '0')); - builder.Append((char)(((number / 1000) % 10) + '0')); - builder.Append((char)(((number / 100) % 10) + '0')); - builder.Append((char)(((number / 10) % 10) + '0')); - builder.Append((char)((number % 10) + '0')); - } - } } } From 699e3abf52735392f355f06c05ee14f25e5b1b4f Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 28 Nov 2024 20:07:50 +0100 Subject: [PATCH 10/12] MicrosoftConsoleJsonLayout - Unit Test TimestampFormat (#776) --- .../Layouts/MicrosoftConsoleJsonLayout.cs | 24 +++++++++---------- .../MicrosoftConsoleJsonLayoutTests.cs | 9 +++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs index 1ec1e1b8..2b631940 100644 --- a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs +++ b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs @@ -17,16 +17,16 @@ public class MicrosoftConsoleJsonLayout : JsonLayout { private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray(); - private readonly SimpleLayout _timestampLayout = new SimpleLayout("${date:format=o:universalTime=true}"); + private readonly SimpleLayout _timestampLayout = new SimpleLayout("\"${date:format=o:universalTime=true}\""); /// /// Initializes a new instance of the class. /// public MicrosoftConsoleJsonLayout() { - Attributes.Add(new JsonAttribute("Timestamp", _timestampLayout)); + Attributes.Add(new JsonAttribute("Timestamp", _timestampLayout) { Encode = false }); Attributes.Add(new JsonAttribute("EventId", Layout.FromMethod(evt => LookupEventId(evt), LayoutRenderOptions.ThreadAgnostic)) { Encode = false }); - Attributes.Add(new JsonAttribute("LogLevel", Layout.FromMethod(evt => ConvertLogLevel(evt.Level), LayoutRenderOptions.ThreadAgnostic))); + Attributes.Add(new JsonAttribute("LogLevel", Layout.FromMethod(evt => ConvertLogLevel(evt.Level), LayoutRenderOptions.ThreadAgnostic)) { Encode = false }); Attributes.Add(new JsonAttribute("Category", "${logger}")); Attributes.Add(new JsonAttribute("Message", "${message}")); Attributes.Add(new JsonAttribute("Exception", "${replace-newlines:${exception:format=tostring,data}}")); @@ -79,7 +79,7 @@ public string TimestampFormat get { var index = LookupNamedAttributeIndex("Timestamp"); - return index >= 0 ? ((Attributes[index].Layout as SimpleLayout)?.LayoutRenderers?.FirstOrDefault() as DateLayoutRenderer)?.Format : null; + return index >= 0 ? ((Attributes[index].Layout as SimpleLayout)?.LayoutRenderers?.OfType().FirstOrDefault())?.Format : null; } set { @@ -91,9 +91,9 @@ public string TimestampFormat if (!string.IsNullOrEmpty(value)) { - var dateLayoutRenderer = _timestampLayout.LayoutRenderers.First() as DateLayoutRenderer; + var dateLayoutRenderer = _timestampLayout.LayoutRenderers.OfType().First(); dateLayoutRenderer.Format = value; - Attributes.Insert(0, new JsonAttribute("Timestamp", _timestampLayout)); + Attributes.Insert(0, new JsonAttribute("Timestamp", _timestampLayout) { Encode = false }); } } } @@ -144,17 +144,17 @@ private static string ConvertEventId(int eventId) private static string ConvertLogLevel(LogLevel logLevel) { if (logLevel == LogLevel.Trace) - return nameof(Microsoft.Extensions.Logging.LogLevel.Trace); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Trace) + "\""; else if (logLevel == LogLevel.Debug) - return nameof(Microsoft.Extensions.Logging.LogLevel.Debug); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Debug) + "\""; else if (logLevel == LogLevel.Info) - return nameof(Microsoft.Extensions.Logging.LogLevel.Information); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Information) + "\""; else if (logLevel == LogLevel.Warn) - return nameof(Microsoft.Extensions.Logging.LogLevel.Warning); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Warning) + "\""; else if (logLevel == LogLevel.Error) - return nameof(Microsoft.Extensions.Logging.LogLevel.Error); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Error) + "\""; else - return nameof(Microsoft.Extensions.Logging.LogLevel.Critical); + return "\"" + nameof(Microsoft.Extensions.Logging.LogLevel.Critical) + "\""; } } } diff --git a/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs b/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs index 73c81244..c69e6d99 100644 --- a/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs +++ b/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs @@ -17,6 +17,15 @@ public void MicrosoftConsoleJsonLayout_NullEvent() Assert.Contains("{ \"EventId\": 0, \"LogLevel\": \"Critical\" }", result); } + [Fact] + public void MicrosoftConsoleJsonLayout_TimestampFormat() + { + var layout = new MicrosoftConsoleJsonLayout() { TimestampFormat = "R" }; + var logEvent = new LogEventInfo(LogLevel.Error, "MyLogger", "Hello World"); + var result = layout.Render(logEvent); + Assert.Equal($"{{ \"Timestamp\": \"{logEvent.TimeStamp.ToUniversalTime().ToString("R")}\", \"EventId\": {0}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Hello World\", \"State\": {{ \"{{OriginalFormat}}\": \"Hello World\" }} }}", result); + } + [Fact] public void MicrosoftConsoleJsonLayout_ExceptionEvent() { From 5da8b93d21f49198d6a2157731363962cc8ca525 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 10 Jan 2025 18:49:10 +0100 Subject: [PATCH 11/12] Bump xunit from 2.9.2 to 2.9.3 (#777) Bumps [xunit](https://github.com/xunit/xunit) from 2.9.2 to 2.9.3. - [Commits](https://github.com/xunit/xunit/compare/v2-2.9.2...v2-2.9.3) --- updated-dependencies: - dependency-name: xunit dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../NLog.Extensions.Hosting.Tests.csproj | 2 +- .../NLog.Extensions.Logging.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj index 213220ed..9bec007c 100644 --- a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj +++ b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj index 57374bc2..0458bb69 100644 --- a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj +++ b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 5cf0f9d17ffe57b5271ea8839bffd46d43bef3e9 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 2 Feb 2025 17:42:46 +0100 Subject: [PATCH 12/12] Version 5.4 (#778) --- CHANGELOG.MD | 7 +++++++ appveyor.yml | 2 +- build.ps1 | 2 +- .../Logging/NLogLoggerFactory.cs | 12 +++++++++++- .../NLog.Extensions.Logging.csproj | 9 ++++----- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 6de6b43f..7fc1456e 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -2,6 +2,13 @@ Date format: (year/month/day) +### Version 5.4 (2025/02/02) + +**Improvements** +- [#775](https://github.com/NLog/NLog.Extensions.Logging/pull/775): MicrosoftConsoleJsonLayout - TimestampLayout replaced by DateLayoutRenderer (#775) (@snakefoot) +- [#778](https://github.com/NLog/NLog.Extensions.Logging/pull/778): NLogLoggerFactory - New constructor with isolated NLog LogFactory as input parameter (#778) (@snakefoot) +- [#778](https://github.com/NLog/NLog.Extensions.Logging/pull/778): Updated to NLog v5.4 (#778) (@snakefoot) + ### Version 5.3.15 (2024/11/21) **Improvements** diff --git a/appveyor.yml b/appveyor.yml index 8e25f204..a3941449 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -37,6 +37,6 @@ test_script: deploy: - provider: NuGet api_key: - secure: C4wzAE+AWne4TbR54gBDkLImCxsfIp21C16wi4LJ3bVSw/HqwZrMSYJ90kl6eAuf + secure: f6oWebyOFLpuuo2PMd6xgoxwMq+JvXVUmPyBme89zS7UF0zcvLYPSKN/p6B/KaMs on: branch: master diff --git a/build.ps1 b/build.ps1 index 2ec3cc11..710122a0 100644 --- a/build.ps1 +++ b/build.ps1 @@ -2,7 +2,7 @@ # creates NuGet package at \artifacts dotnet --version -$versionPrefix = "5.3.15" +$versionPrefix = "5.4.0" $versionSuffix = "" $versionFile = $versionPrefix + "." + ${env:APPVEYOR_BUILD_NUMBER} $versionProduct = $versionPrefix; diff --git a/src/NLog.Extensions.Logging/Logging/NLogLoggerFactory.cs b/src/NLog.Extensions.Logging/Logging/NLogLoggerFactory.cs index 8945b24c..f0f0da8c 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLoggerFactory.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLoggerFactory.cs @@ -27,7 +27,17 @@ public NLogLoggerFactory() /// /// public NLogLoggerFactory(NLogProviderOptions options) - :this(new NLogLoggerProvider(options)) + :this(options, LogManager.LogFactory) + { + } + + /// + /// New factory with options and isolated LogFactory + /// + /// + /// + public NLogLoggerFactory(NLogProviderOptions options, LogFactory logFactory) + : this(new NLogLoggerProvider(options, logFactory)) { RegisterNLogLoggingProvider.SetupNLogConfigSettings(null, null, _provider.LogFactory); } diff --git a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj index 9cf0cdb0..e4be1758 100644 --- a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj +++ b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj @@ -20,10 +20,9 @@ For ASP.NET Core, check: https://www.nuget.org/packages/NLog.Web.AspNetCore ChangeLog: -- UseNLog allow fallback to only EnvironmentName for NLog config (#773) (@snakefoot) -- NLogMessageParameterList - Optimize the index operator (#771) (@snakefoot) -- NLogMessageParameterList - Simplify the index operator (#770) (@snakefoot) -- Recognize ITuple for better AOT support (#768) (@snakefoot) +- MicrosoftConsoleJsonLayout - TimestampLayout replaced by DateLayoutRenderer (#775) (@snakefoot) +- NLogLoggerFactory - New constructor with isolated NLog LogFactory as input parameter (#778) (@snakefoot) +- Updated to NLog v5.4 (#778) (@snakefoot) Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHANGELOG.MD @@ -86,7 +85,7 @@ List of major changes in NLog 5.0: https://nlog-project.org/2021/08/25/nlog-5-0- - +