From cf54a1444f19756dd2dbdb2afb47acf52f1c5bb6 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 12 Jun 2019 22:43:00 +0200 Subject: [PATCH] AppSettingLayoutRenderer2 - Added support for ConnectionStrings Lookup --- src/NLog/Internal/ConfigurationManager2.cs | 51 +++++++++++++++++++ src/NLog/Internal/IConfigurationManager2.cs | 44 ++++++++++++++++ .../AppSettingLayoutRenderer2.cs | 16 +++++- .../LayoutRenderers/AppSettingTests.cs | 34 ++++++++++--- 4 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 src/NLog/Internal/ConfigurationManager2.cs create mode 100644 src/NLog/Internal/IConfigurationManager2.cs diff --git a/src/NLog/Internal/ConfigurationManager2.cs b/src/NLog/Internal/ConfigurationManager2.cs new file mode 100644 index 0000000000..da860fa66c --- /dev/null +++ b/src/NLog/Internal/ConfigurationManager2.cs @@ -0,0 +1,51 @@ +// +// Copyright (c) 2004-2019 Jaroslaw Kowalski , Kim Christensen, Julian Verdurmen +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of Jaroslaw Kowalski nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. +// + +#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD + +namespace NLog.Internal +{ + using System.Collections.Specialized; + + internal class ConfigurationManager2 : IConfigurationManager2 + { + public System.Configuration.ConnectionStringSettings LookupConnectionString(string name) + { + return System.Configuration.ConfigurationManager.ConnectionStrings[name]; + } + + public NameValueCollection AppSettings => System.Configuration.ConfigurationManager.AppSettings; + } +} + +#endif \ No newline at end of file diff --git a/src/NLog/Internal/IConfigurationManager2.cs b/src/NLog/Internal/IConfigurationManager2.cs new file mode 100644 index 0000000000..c999655125 --- /dev/null +++ b/src/NLog/Internal/IConfigurationManager2.cs @@ -0,0 +1,44 @@ +// +// Copyright (c) 2004-2019 Jaroslaw Kowalski , Kim Christensen, Julian Verdurmen +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of Jaroslaw Kowalski nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. +// + +#if !SILVERLIGHT && !__IOS__ && !__ANDROID__ && !NETSTANDARD + +namespace NLog.Internal +{ + internal interface IConfigurationManager2 : IConfigurationManager + { + System.Configuration.ConnectionStringSettings LookupConnectionString(string name); + } +} + +#endif \ No newline at end of file diff --git a/src/NLog/LayoutRenderers/AppSettingLayoutRenderer2.cs b/src/NLog/LayoutRenderers/AppSettingLayoutRenderer2.cs index 3b96b97709..645ffc5b10 100644 --- a/src/NLog/LayoutRenderers/AppSettingLayoutRenderer2.cs +++ b/src/NLog/LayoutRenderers/AppSettingLayoutRenderer2.cs @@ -55,6 +55,8 @@ namespace NLog.LayoutRenderers [ThreadSafe] public sealed class AppSettingLayoutRenderer2 : LayoutRenderer, IStringValueRenderer { + private string _connectionStringName = null; + /// /// The AppSetting item-name /// @@ -75,7 +77,15 @@ public sealed class AppSettingLayoutRenderer2 : LayoutRenderer, IStringValueRend /// public string Default { get; set; } - internal IConfigurationManager ConfigurationManager { get; set; } = new ConfigurationManager(); + internal IConfigurationManager2 ConfigurationManager { get; set; } = new ConfigurationManager2(); + + /// + protected override void InitializeLayoutRenderer() + { + string connectionStringSection = "ConnectionStrings."; + _connectionStringName = Item?.TrimStart().StartsWith(connectionStringSection, StringComparison.InvariantCultureIgnoreCase) == true ? + Item.TrimStart().Substring(connectionStringSection.Length) : null; + } /// /// Renders the specified application setting or default value and appends it to the specified . @@ -94,7 +104,9 @@ private string GetStringValue() if (string.IsNullOrEmpty(Item)) return Default; - string value = ConfigurationManager.AppSettings[Item]; + string value = _connectionStringName != null ? + ConfigurationManager.LookupConnectionString(_connectionStringName)?.ConnectionString : + ConfigurationManager.AppSettings[Item]; if (value == null && Default != null) value = Default; diff --git a/tests/NLog.UnitTests/LayoutRenderers/AppSettingTests.cs b/tests/NLog.UnitTests/LayoutRenderers/AppSettingTests.cs index f15f32e36c..395d2d6f0c 100644 --- a/tests/NLog.UnitTests/LayoutRenderers/AppSettingTests.cs +++ b/tests/NLog.UnitTests/LayoutRenderers/AppSettingTests.cs @@ -35,10 +35,12 @@ namespace NLog.UnitTests.LayoutRenderers { + using System.Collections.Generic; using System.Collections.Specialized; + using System.Configuration; using NLog.Internal; using NLog.LayoutRenderers; - using Xunit; + using Xunit; public class AppSettingTests : NLogTestBase { @@ -109,14 +111,34 @@ public void NoAppSettingTest() Assert.Equal(string.Empty, rendered); } - private class MockConfigurationManager : IConfigurationManager + [Fact] + public void UseConnectionStringTest() { - public MockConfigurationManager() + var configurationManager = new MockConfigurationManager(); + const string expected = "Hello Connection"; + configurationManager.ConnectionStrings["myConnection"] = new ConnectionStringSettings() { ConnectionString = expected }; + var appSettingLayoutRenderer = new AppSettingLayoutRenderer2 { - AppSettings = new NameValueCollection(); - } + ConfigurationManager = configurationManager, + Item = "ConnectionStrings.myConnection", + }; + + var rendered = appSettingLayoutRenderer.Render(LogEventInfo.CreateNullEvent()); - public NameValueCollection AppSettings { get; private set; } + Assert.Equal(expected, rendered); + } + + private class MockConfigurationManager : IConfigurationManager2 + { + public NameValueCollection AppSettings { get; } = new NameValueCollection(); + + public Dictionary ConnectionStrings { get; } = new Dictionary(); + + public ConnectionStringSettings LookupConnectionString(string name) + { + ConnectionStrings.TryGetValue(name, out var value); + return value; + } } } }