Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pull the connectionString for a log4net AdoNetAppender from elsewhere in a web.config file?

I already have a db connection string in my web.config file. I scanned the log4net docs, but can't seem to find a way to use it within the log4net section of my web.config file. Is is possible to do something like this?

<connectionStrings>     <add name="connStr" connectionString="Data Source=localhost; ..." /> </connectionStrings>  <log4net>     <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">     <connectionString connectionStringName="connStr"/>       ... </log4net> 
like image 860
MrSharps Avatar asked Mar 14 '10 06:03

MrSharps


People also ask

How do you read ConnectionString from configuration file into code behind?

To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager. ConnectionStrings["myConnectionString"].

What is ConnectionString in web config?

<connectionStrings> <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System. Data. SqlClient" />

Where do I put log4net in web config?

Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.

How do I create a ConnectionString in web config?

string strcon = ConfigurationManager. ConnectionStrings["Dbconnection"]. ConnectionString; SqlConnection DbConnection = new SqlConnection(strcon);


2 Answers

It is possible to use a DB connection string specified in web.config without creating a new class, though you would need to use log4net build that hasn't been released yet. It can be downloaded from SVN repository http://svn.apache.org/viewvc/logging/log4net/trunk/

Your config will look as follows:

<connectionStrings>     <add name="connStr" connectionString="Data Source=localhost; ..." /> </connectionStrings>  <log4net>     <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">     <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />     <connectionStringName value="connStr" />       ... </log4net> 

Please note that connectionType still needs to be specified.

like image 69
10p Avatar answered Sep 20 '22 15:09

10p


Create a class that extends AdoNetAppender - say, WebAppAdoNetAppender. Implement the ConnectionString property in that class, and retrieve the connection string from your web.config file in that property setter.

<log4net>     <appender name="AdoNetAppender" type="MyApp.WebAppAdoNetAppender">     ... 

...

public class WebAppAdoNetAppender : log4net.Appender.AdoNetAppender {     public new string ConnectionString     {         get { return base.ConnectionString; }         set { base.ConnectionString = ...   }     } } 
like image 21
Michael Petrotta Avatar answered Sep 21 '22 15:09

Michael Petrotta