Skip to content

Commit

Permalink
BeginScope to recognize ITuple with better support for AOT (#768)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
snakefoot authored Oct 27, 2024
1 parent b5675a0 commit 71af10a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 51 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
6 changes: 3 additions & 3 deletions examples/NetCore2/ConsoleExample/ConsoleExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/NetCore2/HostingExample/HostingExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public IDisposable ParseBeginScope<T>(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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<IsPackable>false</IsPackable>
<DebugType>full</DebugType>
Expand All @@ -21,7 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<IsPackable>false</IsPackable>
<DebugType>Full</DebugType>
Expand All @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
Expand All @@ -26,14 +26,9 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NLog.Extensions.Logging\NLog.Extensions.Logging.csproj" />
</ItemGroup>
Expand Down
64 changes: 32 additions & 32 deletions test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down Expand Up @@ -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()));
}

Expand All @@ -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()));
}

Expand All @@ -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()));
}

Expand All @@ -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);
}

Expand All @@ -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<AsyncTargetWrapper>("file")?.WrappedTarget is MemoryTarget);
}

Expand All @@ -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()));
}

Expand All @@ -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()));
}

Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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()));
}

Expand All @@ -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()));
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 71af10a

Please sign in to comment.