This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #9 from tsimbalar/addappsettings
Add support for builder.AddAppSettings
- Loading branch information
Showing
35 changed files
with
860 additions
and
11 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
48 changes: 48 additions & 0 deletions
48
src/Serilog.Settings.Combined/AppSettingsSettingsBuilderExtensions.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,48 @@ | ||
// Copyright 2013-2017 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if APPSETTINGS | ||
using System; | ||
using Serilog.Settings.AppSettings; | ||
using Serilog.Settings.Combined; | ||
|
||
namespace Serilog | ||
{ | ||
/// <summary> | ||
/// Extensions to allow combination of settings originating from config file appSettings | ||
/// </summary> | ||
public static class AppSettingsSettingsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Reads the <appSettings> element of App.config or Web.config, searching for keys | ||
/// that look like: <code>serilog:*</code>, which are used to configure | ||
/// the logger. | ||
/// </summary> | ||
/// <param name="builder">The combined settings builder</param> | ||
/// <param name="settingPrefix">Prefix to use when reading keys in appSettings. If specified the value | ||
/// will be prepended to the setting keys and followed by :, for example "myapp" will use "myapp:serilog:minumum-level. If null | ||
/// no prefix is applied.</param> | ||
/// <param name="filePath">Specify the path to an alternative .config file location. If the file does not exist it will be ignored. | ||
/// By default, the current application's configuration file will be used.</param> | ||
/// <returns>An object allowing configuration to continue.</returns> | ||
public static ICombinedSettingsBuilder AddAppSettings(this ICombinedSettingsBuilder builder, string settingPrefix = null, string filePath = null) | ||
{ | ||
if (builder == null) throw new ArgumentNullException(nameof(builder)); | ||
var appSettings = new AppSettingsSettings(settingPrefix, filePath); | ||
builder.AddKeyValuePairs(appSettings.GetKeyValuePairs()); | ||
return builder; | ||
} | ||
} | ||
} | ||
#endif |
16 changes: 15 additions & 1 deletion
16
src/Serilog.Settings.Combined/CombinedSettingsLoggerConfigurationExtensions.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
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
70 changes: 70 additions & 0 deletions
70
src/Serilog.Settings.Combined/Settings/AppSettings/AppSettingsSettings.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,70 @@ | ||
// Copyright 2013-2015 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if APPSETTINGS | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.Linq; | ||
using Serilog.Debugging; | ||
|
||
namespace Serilog.Settings.AppSettings | ||
{ | ||
class AppSettingsSettings | ||
{ | ||
readonly string _filePath; | ||
readonly string _settingPrefix; | ||
|
||
public AppSettingsSettings(string settingPrefix = null, string filePath = null) | ||
{ | ||
_filePath = filePath; | ||
_settingPrefix = settingPrefix == null ? "serilog:" : $"{settingPrefix}:serilog:"; | ||
} | ||
|
||
public IEnumerable<KeyValuePair<string, string>> GetKeyValuePairs() | ||
{ | ||
IEnumerable<KeyValuePair<string, string>> settings; | ||
|
||
if (!string.IsNullOrWhiteSpace(_filePath)) | ||
{ | ||
if (!File.Exists(_filePath)) | ||
{ | ||
SelfLog.WriteLine("The specified configuration file `{0}` does not exist and will be ignored.", _filePath); | ||
return Enumerable.Empty<KeyValuePair<string, string>>(); | ||
} | ||
|
||
var map = new ExeConfigurationFileMap { ExeConfigFilename = _filePath }; | ||
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); | ||
settings = config.AppSettings.Settings | ||
.Cast<KeyValueConfigurationElement>() | ||
.Select(k => new KeyValuePair<string, string>(k.Key, k.Value)); | ||
} | ||
else | ||
{ | ||
settings = ConfigurationManager.AppSettings.AllKeys | ||
.Select(k => new KeyValuePair<string, string>(k, ConfigurationManager.AppSettings[k])); | ||
} | ||
|
||
var pairs = settings | ||
.Where(k => k.Key.StartsWith(_settingPrefix)) | ||
.Select(k => new KeyValuePair<string, string>( | ||
k.Key.Substring(_settingPrefix.Length), | ||
Environment.ExpandEnvironmentVariables(k.Value))); | ||
|
||
return pairs; | ||
} | ||
} | ||
} | ||
#endif |
16 changes: 15 additions & 1 deletion
16
src/Serilog.Settings.Combined/Settings/Combined/CombinedSettingsBuilder.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
69 changes: 69 additions & 0 deletions
69
test/Serilog.Settings.Combined.Tests/CombinedAppSettingsTests.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,69 @@ | ||
#if APPSETTINGS | ||
using System.Linq; | ||
using Serilog.Events; | ||
using Serilog.Tests.Support; | ||
using TestDummies; | ||
using Xunit; | ||
|
||
namespace Serilog.Settings.Combined.Tests | ||
{ | ||
public class CombinedAppSettingsTests | ||
{ | ||
public CombinedAppSettingsTests() | ||
{ | ||
DummyRollingFileSink.Reset(); | ||
} | ||
|
||
[Fact] | ||
public void CombinedCanMergeSettingsFromMultipleConfigFiles() | ||
{ | ||
LogEvent evt = null; | ||
var log = new LoggerConfiguration() | ||
.ReadFrom.Combined(builder => builder | ||
.AddAppSettings(filePath: "Samples/Initial.config") | ||
.AddAppSettings(filePath: "Samples/Second.config") | ||
.AddAppSettings(filePath: "Samples/Third.config") | ||
) | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information("Has a test property"); | ||
Assert.True(DummyRollingFileSink.Emitted.Any(), "Events should be written to DummyRollingFile"); | ||
Assert.Equal("DefinedInThird", DummyRollingFileSink.PathFormat); | ||
Assert.Equal("DefinedInSecond", DummyRollingFileSink.OutputTemplate); | ||
|
||
Assert.NotNull(evt); | ||
Assert.Equal("OverridenInThird", evt.Properties["AppName"].LiteralValue()); | ||
Assert.Equal("DeclaredInSecond", evt.Properties["ServerName"].LiteralValue()); | ||
} | ||
|
||
[Fact] | ||
public void CombinedCanMergeAppSettingsWithInMemoryKeyValuePairs() | ||
{ | ||
LogEvent evt = null; | ||
var log = new LoggerConfiguration() | ||
.ReadFrom.Combined(builder => builder | ||
.AddKeyValuePair("minimum-level", "Verbose") | ||
.AddKeyValuePair("using:TestDummies", "TestDummies") | ||
.AddKeyValuePair("write-to:DummyRollingFile.restrictedToMinimumLevel", "Debug") | ||
.AddKeyValuePair("enrich:with-property:AppName", "DeclaredInKeyValuePairs") | ||
.AddAppSettings(filePath: "Samples/Second.config") | ||
.AddKeyValuePair("write-to:DummyRollingFile.pathFormat", "DefinedInKeyValuePairs") | ||
.AddKeyValuePair("enrich:with-property:AppName", "OverridenInKeyValuePairs") | ||
) | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information("Has a test property"); | ||
Assert.True(DummyRollingFileSink.Emitted.Any(), "Events should be written to DummyRollingFile"); | ||
Assert.Equal("DefinedInKeyValuePairs", DummyRollingFileSink.PathFormat); | ||
Assert.Equal("DefinedInSecond", DummyRollingFileSink.OutputTemplate); | ||
|
||
Assert.NotNull(evt); | ||
Assert.Equal("OverridenInKeyValuePairs", evt.Properties["AppName"].LiteralValue()); | ||
Assert.Equal("DeclaredInSecond", evt.Properties["ServerName"].LiteralValue()); | ||
} | ||
} | ||
} | ||
|
||
#endif |
41 changes: 41 additions & 0 deletions
41
test/Serilog.Settings.Combined.Tests/CombinedSettingsMixTests.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,41 @@ | ||
#if APPSETTINGS | ||
using System.Linq; | ||
using Serilog.Events; | ||
using Serilog.Tests.Support; | ||
using TestDummies; | ||
using Xunit; | ||
|
||
namespace Serilog.Settings.Combined.Tests | ||
{ | ||
public class CombinedSettingsMixTests | ||
{ | ||
[Fact] | ||
public void CombinedCanMixAndMatchMultipleSources() | ||
{ | ||
LogEvent evt = null; | ||
// typical scenario : doing most of the config in code / default value | ||
// .... and override a few things from config files | ||
var log = new LoggerConfiguration() | ||
.ReadFrom.Combined(builder => builder | ||
.AddKeyValuePair("minimum-level", "Verbose") | ||
.AddKeyValuePair("enrich:with-property:AppName", "DeclaredInInitial") | ||
.AddKeyValuePair("using:TestDummies" ,"TestDummies") | ||
.AddKeyValuePair("write-to:DummyRollingFile.pathFormat", "DeclaredInInitial") | ||
.AddAppSettings(filePath: "Samples/ConfigOverrides.config") | ||
.AddKeyValuePair("enrich:with-property:ExtraProp", "AddedAtTheVeryEnd") | ||
) | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information("Has a test property"); | ||
Assert.True(DummyRollingFileSink.Emitted.Any(), "Events should be written to DummyRollingFile"); | ||
Assert.Equal("DefinedInConfigFile", DummyRollingFileSink.PathFormat); | ||
|
||
Assert.NotNull(evt); | ||
Assert.Equal("DeclaredInInitial", evt.Properties["AppName"].LiteralValue()); | ||
Assert.Equal("DeclaredInConfigFile", evt.Properties["ServerName"].LiteralValue()); | ||
Assert.Equal("AddedAtTheVeryEnd", evt.Properties["ExtraProp"].LiteralValue()); | ||
} | ||
} | ||
} | ||
#endif |
7 changes: 7 additions & 0 deletions
7
test/Serilog.Settings.Combined.Tests/Samples/ConfigOverrides.config
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<appSettings> | ||
<add key="serilog:write-to:DummyRollingFile.pathFormat" value="DefinedInConfigFile" /> | ||
<add key="serilog:enrich:with-property:ServerName" value="DeclaredInConfigFile" /> | ||
</appSettings> | ||
</configuration> |
11 changes: 11 additions & 0 deletions
11
test/Serilog.Settings.Combined.Tests/Samples/Initial.config
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,11 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<appSettings> | ||
<add key="serilog:minimum-level" value="Verbose" /> | ||
<add key="serilog:using:TestDummies" value="TestDummies" /> | ||
<add key="serilog:write-to:DummyRollingFile.restrictedToMinimumLevel" value="Debug" /> | ||
<!-- DummyRollingFile.pathFormat intentionnaly left empty here, to be declared later--> | ||
<!-- DummyRollingFile.outputTemplate intentionnaly left empty here, to be declared later--> | ||
<add key="serilog:enrich:with-property:AppName" value="DeclaredInInitial" /> | ||
</appSettings> | ||
</configuration> |
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<appSettings> | ||
<add key="serilog:write-to:DummyRollingFile.outputTemplate" value="DefinedInSecond" /> | ||
<add key="serilog:enrich:with-property:ServerName" value="DeclaredInSecond" /> | ||
</appSettings> | ||
</configuration> |
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<appSettings> | ||
<add key="serilog:write-to:DummyRollingFile.pathFormat" value="DefinedInThird" /> | ||
<add key="serilog:enrich:with-property:AppName" value="OverridenInThird" /> | ||
</appSettings> | ||
</configuration> |
Oops, something went wrong.