1

Trying to add logging to an application using Log4Net.

It's possible I have wholly misunderstood what this framework is for and how it works, but what I'd like to do is have a custom facade class, under which I actually spin up my log4net instance and which (and here's the trouble) uses a single config file by default, which can be overridden by applications using the class if necessary.

So I've created a custom Log4NetLogger class and an interface for it:

public class Log4NetLogger : ILogging
{
     private ILog _logger;

     public Log4NetLogger()
     {
         log4net.Config.XmlConfigurator.Configure();
        _logger = LogManager.GetLogger(this.GetType());
        string meh = string.Empty;
     }

    //interface methods for error, warn, info etc
 }

In my AssemblyInfo.cs for the project containing my Log4NetLogger class I have added:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

And to the project I've added a Log4Net.config file which looks a bit like this:

<log4net>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<!-- various gubbins -->
<root>
  <level value="ALL" />
  <appender-ref ref="AdoNetAppender" />
</root>
</log4net>

And which is set to "copy always" when compiled, so there should be a copy of it in the bin folder of any project that references it.

Now, in a test class I've added a reference to this project and done this:

        Log4NetLogger logger = new Log4NetLogger();
        logger.Info("started");

Which does absolutely nothing.

Stepping through the code in the debugger it becomes apparent that Log4Net is not picking up the config file. The logger sent back by the log manager has all the levels turned to disabled.

If you add the same config sections to the web.config of my test project, however, it works. But I really don't want to have to add enormous quantities of identical appender configuration in each and every config file in the solution.

What am I doing wrong, is there anything wrong with my approach, and what's the best way forward?

Cheers, Matt

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Bob Tway
  • 9,301
  • 17
  • 80
  • 162
  • I'd suggest 1) setting bufferSize to 1 so events aren't buffered and 2) see http://stackoverflow.com/a/6353064/43846 – stuartd Jun 19 '13 at 14:32

2 Answers2

2

You should be able to remove the attributes entries from AssemblyInfo.cs and change your constructor to pass in the file name.

public Log4NetLogger()
{
    log4net.Config.XmlConfigurator.ConfigureAndWatch(
       new System.IO.FileInfo("Log4Net.config"));      

    _logger = LogManager.GetLogger(this.GetType());
    string meh = string.Empty;
 }
Philipp M
  • 1,877
  • 7
  • 27
  • 38
sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Thanks - this helped me identify the problem, which is that it's looking in the wrong place for the config file: it defaults to C:\Windows\SysWOW64\inetsrv. Which begs the question of how to make sure it looks in the bin folder wherever it happens to be running. – Bob Tway Jun 20 '13 at 08:32
  • You can build the filename using something like `System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Log4Net.config"); `or `System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4Net.config"); ` see http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app – sgmoore Jun 20 '13 at 08:44
0

You need to have an appender section where you configure where the logging is saved. you can refer to this link as well Log4net does not write the log file

Community
  • 1
  • 1
  • Thanks, but I do have an appender section. I left it out of the question above because I didn't think it was relevant - the logger works as long as the config data is added to the web.config of the *using application* rather than the *facade application* – Bob Tway Jun 19 '13 at 14:26
  • Adi appender depends on the database you are using and its config settings to be proper. First try with a file appender and make sure it is logging to file. And then change to DB appender. Each DB required proper config settings for which you can refer http://logging.apache.org/log4net/release/config-examples.html – Gangadhar Heralgi Jun 20 '13 at 04:30