Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read storage connection string from Configuration #19

Closed
csbac opened this issue Jul 15, 2015 · 2 comments
Closed

Read storage connection string from Configuration #19

csbac opened this issue Jul 15, 2015 · 2 comments

Comments

@csbac
Copy link

csbac commented Jul 15, 2015

When deploying on Azure, it often makes sense not to put storage connection strings into source code. It is possible to configure them as part of the portal service configuration.
They can then be access via ConfigurationManager.ConnectionStrings.

The following patch allows to use

<appender name="AzureTableAppender" ....>
  <param name="ConnectionStringName" value="XXX" />

when configuring AzureTableAppender, and use the same connection definition XXX that is used in other places in the code, anyway.

--- a/log4net.Azure/AzureTableAppender.cs
+++ b/log4net.Azure/AzureTableAppender.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Configuration;
 using System.Linq;
 using log4net.Appender.Extensions;
 using log4net.Appender.Language;
@@ -14,12 +15,19 @@ public class AzureTableAppender : BufferingAppenderSkeleton
         private CloudTableClient _client;
         private CloudTable _table;

+        public string ConnectionStringName { get; set; }
         private string _connectionString;

         public string ConnectionString
         {
             get
             {
+                if (!string.IsNullOrWhiteSpace(ConnectionStringName))
+                {
+                    var config = ConfigurationManager.ConnectionStrings[ConnectionStringName];
+                    if (config != null)
+                        return config.ConnectionString;
+                }
                 if (String.IsNullOrEmpty(_connectionString))
                     throw new ApplicationException(Resources.AzureConnectionStringNotSpecified);
                 return _connectionString;
@csbac
Copy link
Author

csbac commented Jul 15, 2015

Ah, you also need

diff --git a/log4net.Azure/log4net.Appender.Azure.csproj b/log4net.Azure/log4net.Appender.Azure.csproj
index 854f7f4..6d947f6 100644
--- a/log4net.Azure/log4net.Appender.Azure.csproj
+++ b/log4net.Azure/log4net.Appender.Azure.csproj
@@ -67,6 +67,7 @@
       <HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
     </Reference>
     <Reference Include="System" />
+    <Reference Include="System.Configuration" />
     <Reference Include="System.Core" />
     <Reference Include="System.Data" />
     <Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

@stemarie
Copy link
Owner

Very nice change, thanks

I'll put this in and update the NuGet package as soon as possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants