Skip to content

Commit

Permalink
AppSettingLayoutRenderer2 - Added support for ConnectionStrings Lookup (
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and 304NotModified committed Jun 12, 2019
1 parent 00709be commit a238637
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 8 deletions.
51 changes: 51 additions & 0 deletions src/NLog/Internal/ConfigurationManager2.cs
@@ -0,0 +1,51 @@
//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, 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
44 changes: 44 additions & 0 deletions src/NLog/Internal/IConfigurationManager2.cs
@@ -0,0 +1,44 @@
//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, 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
16 changes: 14 additions & 2 deletions src/NLog/LayoutRenderers/AppSettingLayoutRenderer2.cs
Expand Up @@ -55,6 +55,8 @@ namespace NLog.LayoutRenderers
[ThreadSafe]
public sealed class AppSettingLayoutRenderer2 : LayoutRenderer, IStringValueRenderer
{
private string _connectionStringName = null;

///<summary>
/// The AppSetting item-name
///</summary>
Expand All @@ -75,7 +77,15 @@ public sealed class AppSettingLayoutRenderer2 : LayoutRenderer, IStringValueRend
/// <docgen category='Rendering Options' order='10' />
public string Default { get; set; }

internal IConfigurationManager ConfigurationManager { get; set; } = new ConfigurationManager();
internal IConfigurationManager2 ConfigurationManager { get; set; } = new ConfigurationManager2();

/// <inheritdoc/>
protected override void InitializeLayoutRenderer()
{
string connectionStringSection = "ConnectionStrings.";
_connectionStringName = Item?.TrimStart().StartsWith(connectionStringSection, StringComparison.InvariantCultureIgnoreCase) == true ?
Item.TrimStart().Substring(connectionStringSection.Length) : null;
}

/// <summary>
/// Renders the specified application setting or default value and appends it to the specified <see cref="StringBuilder" />.
Expand All @@ -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;

Expand Down
34 changes: 28 additions & 6 deletions tests/NLog.UnitTests/LayoutRenderers/AppSettingTests.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string, ConnectionStringSettings> ConnectionStrings { get; } = new Dictionary<string, ConnectionStringSettings>();

public ConnectionStringSettings LookupConnectionString(string name)
{
ConnectionStrings.TryGetValue(name, out var value);
return value;
}
}
}
}
Expand Down

0 comments on commit a238637

Please sign in to comment.