views:

688

answers:

2

I'd like to encrypt the connection string used by the log4net AdoNetAppender.

Can this be done without encrypting the entire appender?

Dion Olsthoorn blogged about setting the connection string in code, but I'd prefer to do it in a config file that is dedicated to logging as it will be using a different database to the rest of the application.

Below is the sample config from http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html

<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%thread" />
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout" value="%level" />
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%logger" />
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout" value="%message" />
  </parameter>
</appender>
+1  A: 

I've been able to work around this by inheriting from the log4net AdoNetAppender and adding a property called ConnectionStringName.

If this is set it will read the connection string from the web.config connection strings and pass it through to the underlying AdoNetAppender.

I'd still be keen for a solution that keeps all the logging configuration in a single file and still allows the connection string to be encrypted.

    public class ConfigAdoNetAppender : AdoNetAppender
    {
        public string ConnectionStringName
        {

            set
            {
                this.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[value].ToString();
            }
        }
    }
Daniel Ballinger
+1  A: 

The current source already have the ConnectionStringName property that does exactly what you are looking for.

The ConnectionStringName property was introduced in revision 607748. Version 1.2.10 is revision 395324 which is quite old compared to that.

If you're comfortable working with unreleased code you could get the latest source and compile it yourself. You could probably also get just the updated AdoNetAppender class though it could have dependencies on other updates to the log4net core.

Peter Lillevold
I just did a search through the source for 1.2.10 and couldn't find ConnectionStringName anywhere. Where should I be looking?Thanks.
Daniel Ballinger
@Daniel - Updated the answer with some references.
Peter Lillevold