44

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>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
MrSharps
  • 537
  • 1
  • 5
  • 8

5 Answers5

50

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.

10p
  • 5,488
  • 22
  • 30
32

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 = ...   }
    }
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    Stefan Egli's link indicates that a parameter doesn't yet exist. For the time being, this seems like the best way to go about it. Thanks Mr. Petrotta! – MrSharps Mar 14 '10 at 12:50
  • How and where would I load/call/use this class? I'm just getting started with log4net and I'm finding the documentation a little sparse. I'm running this in a Quartz.NET job, which instantiates the logger for me. How would I replace the default logger with my own subclass? Thanks. – InteXX Feb 21 '15 at 02:37
5

fyi this will be implemented in 1.2.11 according to this. however I have no idea when they are going to release it.

Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
5

the answers above all do not work. i got another solution for this, i tried and it worked:

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
           }
        }
    }
}

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

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Aiping He
  • 491
  • 1
  • 6
  • 10
  • I also couldn't get the code above to work *and this did*. For me though, I had to make sure to have a blank ConnectionString in the config: – Philip Pittle Jan 10 '14 at 14:41
0

As of 2017 (log4net 2.0.8.0), the following works:

public class MyAdoNetAppender : AdoNetAppender
{
    public MyAdoNetAppender()
    {
        ConnectionString = ...
    }
}


<appender name="AdoNetAppender" type="MyApp.MyAdoNetAppender">

(Very similar to @Michael Petrotta's answer)

turdus-merula
  • 8,546
  • 8
  • 38
  • 50