简体   繁体   English

log4net SQL配置而不是web.config

[英]log4net SQL configuration instead of web.config

Need to have log4net configuring with sql in my project , but due to security reasons must not have the connection string details on the web.config . 需要在我的项目中使用sql配置log4net,但是由于安全原因,必须在web.config上不包含连接字符串详细信息。 What are the alternatives ? 有哪些选择? Just need to hide the ConnectionString only . 只需要隐藏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;
}

This is not solving my issue , hiding the connection string details . 这不能解决我的问题,而是隐藏了连接字符串的详细信息。 The connection details to be pass to the web.config to consume the log.net sql logging 要传递到web.config的连接详细信息以使用log.net sql日志记录

You could store the connection string in a separate file which you can keep out of source control to avoid exposing the credentials. 您可以将连接字符串存储在一个单独的文件中,该文件可以不受源代码控制,从而避免暴露凭证。 Add the following to your Web.config file: 将以下内容添加到您的Web.config文件中:

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

Then create a connections.secret.config file, but keep it away from your solution: 然后创建一个connections.secret.config文件,但不要将其放在解决方案中:

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

You will need to make sure you provide the connection string wherever you end up deploying your code using an environment variable or similar. 您需要确保在最终使用环境变量或类似变量部署代码的任何地方都提供连接字符串。

   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