-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from serilog/dev
3.1.0 Release
- Loading branch information
Showing
28 changed files
with
812 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Serilog.Settings.Configuration/Settings/Configuration/Assemblies/AssemblyFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyModel; | ||
|
||
namespace Serilog.Settings.Configuration.Assemblies | ||
{ | ||
abstract class AssemblyFinder | ||
{ | ||
public abstract IReadOnlyList<AssemblyName> FindAssembliesContainingName(string nameToFind); | ||
|
||
protected static bool IsCaseInsensitiveMatch(string text, string textToFind) | ||
{ | ||
return text != null && text.ToLowerInvariant().Contains(textToFind.ToLowerInvariant()); | ||
} | ||
|
||
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) | ||
{ | ||
return new DependencyContextAssemblyFinder(DependencyContext.Default); | ||
} | ||
return new DllScanningAssemblyFinder(); | ||
} | ||
|
||
public static AssemblyFinder ForSource(ConfigurationAssemblySource configurationAssemblySource) | ||
{ | ||
switch (configurationAssemblySource) | ||
{ | ||
case ConfigurationAssemblySource.UseLoadedAssemblies: | ||
return Auto(); | ||
case ConfigurationAssemblySource.AlwaysScanDllFiles: | ||
return new DllScanningAssemblyFinder(); | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(configurationAssemblySource), configurationAssemblySource, null); | ||
} | ||
} | ||
|
||
public static AssemblyFinder ForDependencyContext(DependencyContext dependencyContext) | ||
{ | ||
return new DependencyContextAssemblyFinder(dependencyContext); | ||
} | ||
} | ||
} |
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
...ttings.Configuration/Settings/Configuration/Assemblies/DependencyContextAssemblyFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyModel; | ||
|
||
namespace Serilog.Settings.Configuration.Assemblies | ||
{ | ||
sealed class DependencyContextAssemblyFinder : AssemblyFinder | ||
{ | ||
readonly DependencyContext _dependencyContext; | ||
|
||
public DependencyContextAssemblyFinder(DependencyContext dependencyContext) | ||
{ | ||
_dependencyContext = dependencyContext ?? throw new ArgumentNullException(nameof(dependencyContext)); | ||
} | ||
|
||
public override IReadOnlyList<AssemblyName> FindAssembliesContainingName(string nameToFind) | ||
{ | ||
var query = from library in _dependencyContext.RuntimeLibraries | ||
from assemblyName in library.GetDefaultAssemblyNames(_dependencyContext) | ||
where IsCaseInsensitiveMatch(assemblyName.Name, nameToFind) | ||
select assemblyName; | ||
|
||
return query.ToList().AsReadOnly(); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...log.Settings.Configuration/Settings/Configuration/Assemblies/DllScanningAssemblyFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Serilog.Settings.Configuration.Assemblies | ||
{ | ||
sealed class DllScanningAssemblyFinder : AssemblyFinder | ||
{ | ||
public override IReadOnlyList<AssemblyName> FindAssembliesContainingName(string nameToFind) | ||
{ | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.