加密log4net AdoNetAppender使用的connectionString

时间:2021-11-14 22:01:34

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

我想加密log4net AdoNetAppender使用的连接字符串。

Can this be done without encrypting the entire appender?

这可以在不加密整个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.

Dion Olsthoorn在博客中写了关于在代码中设置连接字符串的内容,但我更愿意在专用于日志记录的配置文件中进行,因为它将使用与应用程序其余部分不同的数据库。

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

以下是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>

2 个解决方案

#1


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

当前源已经具有ConnectionStringName属性,该属性完全符合您的要求。

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

ConnectionStringName属性是在版本607748中引入的。版本1.2.10是版本395324,与之相比相当旧。

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.

如果您对使用未发布的代码感到满意,可以获得最新的源代码并自行编译。您可能还可以获得更新的AdoNetAppender类,尽管它可能依赖于对log4net核心的其他更新。

#2


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

我已经能够通过继承log4net AdoNetAppender并添加一个名为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.

如果设置了此项,它将从web.config连接字符串中读取连接字符串,并将其传递给基础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();
            }
        }
    }

#1


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

当前源已经具有ConnectionStringName属性,该属性完全符合您的要求。

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

ConnectionStringName属性是在版本607748中引入的。版本1.2.10是版本395324,与之相比相当旧。

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.

如果您对使用未发布的代码感到满意,可以获得最新的源代码并自行编译。您可能还可以获得更新的AdoNetAppender类,尽管它可能依赖于对log4net核心的其他更新。

#2


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

我已经能够通过继承log4net AdoNetAppender并添加一个名为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.

如果设置了此项,它将从web.config连接字符串中读取连接字符串,并将其传递给基础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();
            }
        }
    }