How i can use the connectionString of the current website for log4Net instead of configuring

Aiping He picture Aiping He · Mar 15, 2012 · Viewed 17.7k times · Source

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?

Answer

gustavodidomenico picture gustavodidomenico · Aug 20, 2013

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" />
...