log4net SQL配置而不是web.config

时间:2018-02-02 14:56:47

标签: c# asp.net log4net

需要在我的项目中使用sql配置log4net,但由于安全原因,必须在web.config上没有连接字符串详细信息。有哪些替代方案?只需要隐藏ConnectionString。

class OwnerCourseEditMixin(OwnerCourseMixin):
    fields = ['subject', 'title', 'slug', 'overview']
    template_name = 'courses/manage/course/form.html'

这不是解决我的问题,隐藏连接字符串  细节 。连接详细信息将传递给web.config以使用log.net sql logging

2 个答案:

答案 0 :(得分:1)

您可以将连接字符串存储在单独的文件中,您可以将其保留在源代码管理之外,以避免公开凭据。将以下内容添加到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>

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

答案 1 :(得分:1)

   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>