Skip to content
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

DllScanningAssemblyFinder fixes (#157, #150, #122, #156) #159

Merged
merged 2 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Microsoft.Extensions.Configuration (appsettings.json) support for Serilog.</Description>
Expand All @@ -21,6 +21,10 @@
<RootNamespace>Serilog</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="('$(TargetFramework)' == 'net451') Or ('$(TargetFramework)' == 'net461')">
<DefineConstants>$(DefineConstants);PRIVATE_BIN</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
<PackageReference Include="Serilog" Version="2.6.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

Expand All @@ -9,12 +10,57 @@ sealed class DllScanningAssemblyFinder : AssemblyFinder
{
public override IReadOnlyList<AssemblyName> FindAssembliesContainingName(string nameToFind)
{
var query = from outputAssemblyPath in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")
let assemblyFileName = System.IO.Path.GetFileNameWithoutExtension(outputAssemblyPath)
where IsCaseInsensitiveMatch(assemblyFileName, nameToFind)
select AssemblyName.GetAssemblyName(outputAssemblyPath);
var probeDirs = new List<string>();

if (!string.IsNullOrEmpty(AppDomain.CurrentDomain.BaseDirectory))
{
probeDirs.Add(AppDomain.CurrentDomain.BaseDirectory);

#if PRIVATE_BIN
var privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
if (!string.IsNullOrEmpty(privateBinPath))
{
foreach (var path in privateBinPath.Split(';'))
{
if (Path.IsPathRooted(path))
{
probeDirs.Add(path);
}
else
{
probeDirs.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path));
}
}
}
#endif
}
else
{
probeDirs.Add(Path.GetDirectoryName(typeof(AssemblyFinder).Assembly.Location));
}

var query = from probeDir in probeDirs
where Directory.Exists(probeDir)
from outputAssemblyPath in Directory.GetFiles(probeDir, "*.dll")
let assemblyFileName = Path.GetFileNameWithoutExtension(outputAssemblyPath)
where IsCaseInsensitiveMatch(assemblyFileName, nameToFind)
let assemblyName = TryGetAssemblyNameFrom(outputAssemblyPath)
where assemblyName != null
select assemblyName;

return query.ToList().AsReadOnly();

AssemblyName TryGetAssemblyNameFrom(string path)
{
try
{
return AssemblyName.GetAssemblyName(path);
}
catch (BadImageFormatException)
{
return null;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.IO;

using Xunit;

using Serilog.Settings.Configuration.Assemblies;

namespace Serilog.Settings.Configuration.Tests
{
public class DllScanningAssemblyFinderTests : IDisposable
{
readonly string _privateBinPath;

public DllScanningAssemblyFinderTests()
{
var d1 = GetOrCreateDirectory("bin1");
skomis-mm marked this conversation as resolved.
Show resolved Hide resolved
var d2 = GetOrCreateDirectory("bin2");
var d3 = GetOrCreateDirectory("bin3");

_privateBinPath = $"{d1.Name};{d2.FullName};{d3.Name}";

DirectoryInfo GetOrCreateDirectory(string name)
=> Directory.Exists(name) ? new DirectoryInfo(name) : Directory.CreateDirectory(name);
}

public void Dispose()
{
Directory.Delete("bin1", true);
Directory.Delete("bin2", true);
Directory.Delete("bin3", true);
}

[Fact]
public void ShouldProbeCurrentDirectory()
{
var assemblyNames = new DllScanningAssemblyFinder().FindAssembliesContainingName("testdummies");
Assert.Single(assemblyNames);
}

#if PRIVATE_BIN
[Fact]
public void ShouldProbePrivateBinPath()
{
File.Copy("testdummies.dll", "bin1/customSink1.dll", true);
File.Copy("testdummies.dll", "bin2/customSink2.dll", true);
File.Copy("testdummies.dll", "bin3/thirdpartydependency.dll", true);

var ad = AppDomain.CreateDomain("serilog", null,
new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
PrivateBinPath = _privateBinPath
});

try
{
ad.DoCallBack(DoTestInner);
}
finally
{
AppDomain.Unload(ad);
}

void DoTestInner()
{
var assemblyNames = new DllScanningAssemblyFinder().FindAssembliesContainingName("customSink");
Assert.Equal(2, assemblyNames.Count);
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net452'">
<DefineConstants>$(DefineConstants);PRIVATE_BIN</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Settings.Configuration\Serilog.Settings.Configuration.csproj" />
<ProjectReference Include="..\TestDummies\TestDummies.csproj" />
Expand Down