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 . What are the alternatives ? Just need to hide the ConnectionString only .
{
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
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:
<configuration>
<connectionStrings configSource="connections.secret.config" />
</configuration>
Then create a connections.secret.config file, but keep it away from your solution:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.