繁体   English   中英

log4net SQL配置而不是web.config

[英]log4net SQL configuration instead of web.config

需要在我的项目中使用sql配置log4net,但是由于安全原因,必须在web.config上不包含连接字符串详细信息。 有哪些选择? 只需要隐藏ConnectionString而已。

{
    var settings = ConfigurationManager.ConnectionStrings[1];
    var fi = typeof(ConfigurationElement).GetField("_bReadOnly", 
    BindingFlags.Instance | BindingFlags.NonPublic);
    fi.SetValue(settings, false);
    string connection = GetConnection();//To get the connection 
    details using a service
    settings.ConnectionString = connection;
}

这不能解决我的问题,而是隐藏了连接字符串的详细信息。 要传递到web.config的连接详细信息以使用log.net sql日志记录

您可以将连接字符串存储在一个单独的文件中,该文件可以不受源代码控制,从而避免暴露凭证。 将以下内容添加到您的Web.config文件中:

<configuration>
    <connectionStrings configSource="connections.secret.config" />
</configuration>

然后创建一个connections.secret.config文件,但不要将其放在解决方案中:

<connectionStrings>
    <add name="connection_name" connectionString="your_connection" providerName="System.Data.SqlClient" />
</connectionStrings>

您需要确保在最终使用环境变量或类似变量部署代码的任何地方都提供连接字符串。

   Finally, the one which helped me to pass the connection information to ado.net appender 


 public static class LogConfigurator
 {
  public static void SetConnectionString(string connectionString)
  {
       Hierarchy logHierarchy = log4net.LogManager.GetRepository() as 
                                Hierarchy;

        if (logHierarchy == null)
       {
        throw new InvalidOperationException
           ("Can't set connection string as hierarchy is null.");
    }

    var appender = logHierarchy.GetAppenders()
                               .OfType<AdoNetAppender>()
                               .SingleOrDefault();

    if (appender == null)
    {
        throw new InvalidOperationException
          ("Can't locate a database appender");
    }

    appender.ConnectionString = connectionString; // Using a service to get 
    //the connection information 
    appender.ActivateOptions();

}}

web.config , give the same name for ado.net appender to take the connection
<connectionStringName value="ConnectionString" /> 

  <connectionStrings>
  <add name="ConnectionString" connectionString=""
  providerName="System.Data.SqlClient" />
  </connectionStrings>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM