Skip to content

Commit 4db7753

Browse files
authored
Merge pull request #136 from MV10/issue_63
fixes #63 - option to control assembly source
2 parents fbe4f2f + 1a46ac7 commit 4db7753

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static LoggerConfiguration Configuration(
4747
DependencyContext dependencyContext = null)
4848
{
4949
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
50+
5051
return settingConfiguration.Settings(
5152
new ConfigurationReader(
5253
configuration,
@@ -69,11 +70,63 @@ public static LoggerConfiguration ConfigurationSection(
6970
{
7071
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
7172
if (configSection == null) throw new ArgumentNullException(nameof(configSection));
72-
73+
7374
return settingConfiguration.Settings(
7475
new ConfigurationReader(
7576
configSection,
7677
dependencyContext ?? (Assembly.GetEntryAssembly() != null ? DependencyContext.Default : null)));
7778
}
79+
80+
/// <summary>
81+
/// Reads logger settings from the provided configuration object using the default section name. Generally this
82+
/// is preferable over the other method that takes a configuration section. Only this version will populate
83+
/// IConfiguration parameters on target methods.
84+
/// </summary>
85+
/// <param name="settingConfiguration">Logger setting configuration.</param>
86+
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
87+
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
88+
/// <returns>An object allowing configuration to continue.</returns>
89+
public static LoggerConfiguration Configuration(
90+
this LoggerSettingsConfiguration settingConfiguration,
91+
IConfiguration configuration,
92+
ConfigurationAssemblySource configurationAssemblySource)
93+
{
94+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
95+
96+
if(configurationAssemblySource == ConfigurationAssemblySource.UseLoadedAssemblies)
97+
{
98+
return Configuration(settingConfiguration, configuration);
99+
}
100+
else
101+
{
102+
return settingConfiguration.Settings(new ConfigurationReader(configuration, null));
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
108+
/// extension method that takes the full configuration object.
109+
/// </summary>
110+
/// <param name="settingConfiguration">Logger setting configuration.</param>
111+
/// <param name="configSection">The Serilog configuration section</param>
112+
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
113+
/// <returns>An object allowing configuration to continue.</returns>
114+
public static LoggerConfiguration ConfigurationSection(
115+
this LoggerSettingsConfiguration settingConfiguration,
116+
IConfigurationSection configSection,
117+
ConfigurationAssemblySource configurationAssemblySource)
118+
{
119+
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
120+
if (configSection == null) throw new ArgumentNullException(nameof(configSection));
121+
122+
if (configurationAssemblySource == ConfigurationAssemblySource.UseLoadedAssemblies)
123+
{
124+
return Configuration(settingConfiguration, configSection);
125+
}
126+
else
127+
{
128+
return settingConfiguration.Settings(new ConfigurationReader(configSection, null));
129+
}
130+
}
78131
}
79132
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013-2016 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Serilog.Settings.Configuration
16+
{
17+
/// <summary>
18+
/// Defines how the package will identify the assemblies which are scanned for sinks and other Type information.
19+
/// </summary>
20+
public enum ConfigurationAssemblySource
21+
{
22+
/// <summary>
23+
/// Try to scan the assemblies already in memory. This is the default. If GetEntryAssembly is null, fallback to DLL scanning.
24+
/// </summary>
25+
UseLoadedAssemblies,
26+
27+
/// <summary>
28+
/// Scan for assemblies in DLLs from the working directory. This is the fallback when GetEntryAssembly is null.
29+
/// </summary>
30+
AlwaysScanDllFiles
31+
}
32+
}

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ public void ParameterlessSinksAreConfigured(string syntax, string json)
7777
Assert.Equal(1, DummyWithLevelSwitchSink.Emitted.Count);
7878
}
7979

80+
[Fact]
81+
public void ConfigurationAssembliesFromDllScanning()
82+
{
83+
var json = @"{
84+
""Serilog"": {
85+
""Using"": [""TestDummies""],
86+
""WriteTo"": [""DummyConsole""]
87+
}
88+
}";
89+
90+
var builder = new ConfigurationBuilder().AddJsonString(json);
91+
var config = builder.Build();
92+
var log = new LoggerConfiguration()
93+
.ReadFrom.Configuration(
94+
configuration: config,
95+
configurationAssemblySource: ConfigurationAssemblySource.AlwaysScanDllFiles)
96+
.CreateLogger();
97+
98+
DummyConsoleSink.Emitted.Clear();
99+
100+
log.Write(Some.InformationEvent());
101+
102+
Assert.Equal(1, DummyConsoleSink.Emitted.Count);
103+
}
104+
80105
[Fact]
81106
public void SinksAreConfigured()
82107
{

test/Serilog.Settings.Configuration.Tests/LoggerConfigurationExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Microsoft.Extensions.Configuration;
2-
using System;
1+
using System;
2+
using Microsoft.Extensions.Configuration;
33
using Xunit;
44

55
namespace Serilog.Settings.Configuration.Tests

0 commit comments

Comments
 (0)