How can I use the connectionString of the current site for log4Net instead of setting

I am using log4Net for my syslog. ConnectionString node is required if the application type is AdoNetAppender in Log4Net. However, I already have a connectionString in my site, where I use Log4Net.

How can I use the site connStr for log4Net instead of configuring the same connstr in the log4net configuration file again?

+23
c # connection-string log4net
Mar 15 2018-12-15T00:
source share
6 answers

It's pretty simple, you just need to replace the appender connectionString configuration.

Instead of the connection string:

 <connectionString value="[Complete Connection]" /> 

You just use the connectionStringName configuration:

 <connectionStringName value="ApplicationConnection" /> 

And then you have a connection string to your application:

  <connectionStrings> <add name="ApplicationConnection" connectionString="Connection" providerName="System.Data.OracleClient" /> </connectionStrings> 

Unfortunately, you must have a connectionType named connectionStringName, for 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" /> ... 
+31
Aug 20 '13 at 14:12
source share

You can dynamically update ConnectionString from AdoNetAppender after you have configured log4net for your site, usually in Global.asax. After your call to configure log4net using XmlConfigutor() or something else .. you can call the method below, which checks all AdoNetAppenders and updates the required connectionString.

 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 } } } } 
+14
Mar 17 '12 at 10:05
source share

Now you can use the ConnectionStringName property for AdoNetAppender, pointing it to the namedString name in your application or web.config file:

AdoNetAppender.ConnectionStringName Property

+7
Jun 07 2018-12-12T00:
source share

You can do this by writing a custom ADO.NET application 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 can visit http://technico.qnownow.com/2012/03/12/how-to-write-custom-ado-net-appender-for-log4net/ for a complete example

+1
Mar 19 '12 at 18:31
source share

You can do this by inheriting AdoNetAppender.

  • 1) Create a class that inherits from AdoNetAppender.
  • 2) Next, create a ConnectionStringName property that sets the Log4Net ConnectionString property to the connection string that is retrieved by the .NET ConfigurationManager.
  • 3) Create a ConnectionStringName entry in the AdoNetAppender section of your configuration file, which maps to the existing connectionString in the connectionStrings section of your configuration file.

See the Ken Burkhardt blog below for more information.

http://kenny-bu.blogspot.com/2011/03/using-connection-string-name-with.html

0
Mar 15 2018-12-15T00:
source share

This should be possible in version 1.2.11. Here is the link to the question:

https://issues.apache.org/jira/browse/LOG4NET-88

0
Mar 16 '12 at 9:14
source share



All Articles