This question already has an answer here:
I use log4Net for my system's log. The connectionString node is mandatory if the appender type is the AdoNetAppender in Log4Net. However, I already have a connectionString in my website where I use Log4Net.
How can I use the connStr of the website for log4Net instead of configuring the same connstr again in the log4net config file?
It's quite simple, you just need to replace the appender connectionString
configuration.
In place of the connection string:
<connectionString value="[Complete Connection]" />
You just use the connectionStringName
configuration:
<connectionStringName value="ApplicationConnection" />
And then you have your application connection string:
<connectionStrings>
<add name="ApplicationConnection" connectionString="Connection" providerName="System.Data.OracleClient" />
</connectionStrings>
Unfortunately you must have the connectionType
with the connectionStringName, example:
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionStringName value="ApplicationConnection" />
...
You can update ConnectionString of AdoNetAppender dynamically, after you have configured log4net for your website, usually in the Global.asax. After your call to configure the log4net using XmlConfigutor()
or something.. you can call below method which checks for all AdoNetAppenders and update the connectionString required.
private static void ConfigureLog4Net()
{
Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
if(hierarchy != null && hierarchy.Configured)
{
foreach(IAppender appender in hierarchy.GetAppenders())
{
if(appender is AdoNetAppender)
{
var adoNetAppender = (AdoNetAppender)appender;
adoNetAppender.ConnectionString = ConfigurationManager.AppSettings["YOURCONNECTIONSTRINGKEY"].ToString();
adoNetAppender.ActivateOptions(); //Refresh AdoNetAppenders Settings
}
}
}
}
You can now use the ConnectionStringName property of the AdoNetAppender do point it to a named connectionString in your app or web.config file:
You could do this by writing a custom ADO.NET appender and overriding the connection string:
public new string ConnectionString {
get {
return base.ConnectionString;
}
//you could set your own connection string here
set {
base.ConnectionString = ConfigurationManager.ConnectionStrings ["Sql"].
ConnectionString;
}
}
You could visit http://technico.qnownow.com/2012/03/12/how-to-write-custom-ado-net-appender-for-log4net/ for a complete example
You can do that by inheriting AdoNetAppender.
- 1) Create a class that inherits from AdoNetAppender.
- 2) Next create a ConnectionStringName property that will set the Log4Net ConnectionString property to a connection string that is retrieved by the .Net ConfigurationManager.
- 3) Create a ConnectionStringName entry in the AdoNetAppender section of your config file which maps to an existing connectionString in the connectionStrings section of your config file.
See the below "Ken Burkhardt" blog for details.
http://kenny-bu.blogspot.com/2011/03/using-connection-string-name-with.html
This should be possible in version 1.2.11. Here is the link to the issue:
来源:https://stackoverflow.com/questions/9723051/how-i-can-use-the-connectionstring-of-the-current-website-for-log4net-instead-of