簡體   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