Skip to content

Configuration assembly auto-discovery fixes #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/sample/Sample/bin/Debug/netcoreapp2.0/Sample.dll",
"program": "${workspaceFolder}/sample/Sample/bin/Debug/netcoreapp3.1/Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/sample/Sample",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
21 changes: 14 additions & 7 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ echo "build: Build started"
Push-Location $PSScriptRoot

if (Test-Path .\artifacts) {
echo "build: Cleaning .\artifacts"
Remove-Item .\artifacts -Force -Recurse
echo "build: Cleaning .\artifacts"
Remove-Item .\artifacts -Force -Recurse
}

& dotnet restore --no-cache
Expand All @@ -18,7 +18,7 @@ echo "build: Version suffix is $suffix"
foreach ($src in dir src/*) {
Push-Location $src

echo "build: Packaging project in $src"
echo "build: Packaging project in $src"

& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix -p:ContinuousIntegrationBuild=true
if ($LASTEXITCODE -ne 0) { exit 1 }
Expand All @@ -29,7 +29,7 @@ foreach ($src in dir src/*) {
foreach ($test in dir test/*.PerformanceTests) {
Push-Location $test

echo "build: Building performance test project in $test"
echo "build: Building performance test project in $test"

& dotnet build -c Release
if ($LASTEXITCODE -ne 0) { exit 2 }
Expand All @@ -40,11 +40,12 @@ foreach ($test in dir test/*.PerformanceTests) {
foreach ($test in dir test/*.Tests) {
Push-Location $test

echo "build: Testing project in $test"
echo "build: Testing project in $test"

if ($PSVersionTable.Platform -eq "Unix") {
& dotnet test -c Release -f netcoreapp2.0
& dotnet test -c Release -f netcoreapp2.1
& dotnet test -c Release -f netcoreapp3.1
& dotnet test -c Release -f net50
} else {
& dotnet test -c Release
}
Expand All @@ -57,7 +58,13 @@ foreach ($test in dir test/*.Tests) {
if ($PSVersionTable.Platform -eq "Unix") {
Push-Location sample/Sample

& dotnet run -f netcoreapp2.0 -c Release --run-once
& dotnet run -f netcoreapp2.1 -c Release --run-once
if ($LASTEXITCODE -ne 0) { exit 4 }

& dotnet run -f netcoreapp3.1 -c Release --run-once
if ($LASTEXITCODE -ne 0) { exit 4 }

& dotnet run -f net50 -c Release --run-once
if ($LASTEXITCODE -ne 0) { exit 4 }

Pop-Location
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* #219 - reduced search graph for configuration dlls to avoid native assets
* #221 - added support for conditional/leveled enrichers from Serilog 2.9+
* #222 - updated Microsoft.Extensions.DependencyModel
* #237 - DependencyContextAssemblyFinder fix: check `serilog` at the start of the name for any dependent package
* #239 - handle NotSupportedException for .net 5.0 single file applications

3.1.0

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ For legacy .NET Framework projects it also scans default probing path(s).

For all other cases, as well as in the case of non-conventional configuration assembly names **DO** use `Using` section.

#### .NET 5.0 Single File Applications

Currently, auto-discovery of configuration assemblies is not supported in bundled mode. Use `Using` section for workaround.

### MinimumLevel, LevelSwitches, overrides and dynamic reload

The `MinimumLevel` configuration property can be set to a single value as in the sample above, or, levels can be overridden per logging source.
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ version: '{build}'
skip_tags: true
image:
- Visual Studio 2019
- Ubuntu1604
- Ubuntu
configuration: Release
build_script:
- ps: ./Build.ps1
for:
-
matrix:
only:
- image: Ubuntu1604
- image: Ubuntu
build_script:
- pwsh ./Build.ps1
test: off
Expand Down
19 changes: 15 additions & 4 deletions sample/Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks>net50;netcoreapp3.1;netcoreapp2.1;net46</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>

Expand All @@ -15,10 +15,22 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.10" />
<PackageReference Include="Serilog.Expressions" Version="1.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net50'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Serilog.Expressions" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -27,7 +39,6 @@
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ protected static bool IsCaseInsensitiveMatch(string text, string textToFind)

public static AssemblyFinder Auto()
{
// Need to check `Assembly.GetEntryAssembly()` first because
// `DependencyContext.Default` throws an exception when `Assembly.GetEntryAssembly()` returns null
if (Assembly.GetEntryAssembly() != null && DependencyContext.Default != null)
try
{
return new DependencyContextAssemblyFinder(DependencyContext.Default);
// Need to check `Assembly.GetEntryAssembly()` first because
// `DependencyContext.Default` throws an exception when `Assembly.GetEntryAssembly()` returns null
if (Assembly.GetEntryAssembly() != null && DependencyContext.Default != null)
{
return new DependencyContextAssemblyFinder(DependencyContext.Default);
}
}
catch (NotSupportedException) when (typeof(object).Assembly.Location is "") // bundled mode detection
{
}

return new DllScanningAssemblyFinder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ where IsCaseInsensitiveMatch(assemblyName.Name, nameToFind)

static bool IsReferencingSerilog(Library library)
{
return library.Dependencies.Any(dependency => dependency.Name.Equals("serilog", StringComparison.OrdinalIgnoreCase));
const string Serilog = "serilog";
return library.Dependencies.Any(dependency =>
dependency.Name.StartsWith(Serilog, StringComparison.OrdinalIgnoreCase) &&
(dependency.Name.Length == Serilog.Length || dependency.Name[Serilog.Length] == '.'));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.0;net452</TargetFrameworks>
<TargetFrameworks>net50;netcoreapp3.1;netcoreapp2.1;net452</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AssemblyName>Serilog.Settings.Configuration.Tests</AssemblyName>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
Expand All @@ -20,20 +20,22 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.1.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.1" />
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.10" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net50'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ public static object LiteralValue(this LogEventPropertyValue @this)
return ((ScalarValue)@this).Value;
}

// netcore3.0 error:
// Could not parse the JSON file. System.Text.Json.JsonReaderException : ''' is an invalid start of a property name. Expected a '"'
public static string ToValidJson(this string str)
{
#if NETCOREAPP3_1
str = str.Replace('\'', '"');
#endif
return str;
}
}
Expand Down