0

The application is a WinForms .net 3.5 app with multiple projects, one main project and multiple library projects. If I call log4net from a singleton that exists in one of the sub-projects it causes all logging to stop.

Update

Based on some of the comments I changed the TestSingletonLog4net class in the following ways. The problem still exists with these changes.

  • Made the test class thread safe as Jon Skeet pointed this out.
  • Changed the member declaration and added the log4net.Config.XmlConfigurator.Configure(); config call as per Norla's example with the same results, still whenever it is called all logging stops.

Here is the updated class to reproduce the problem.

    public class TestSingletonLog4Net
{
    private static volatile TestSingletonLog4Net instance;
    private static object syncRoot = new Object();

    public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(TestSingletonLog4Net));

    private  TestSingletonLog4Net()
    {
        log4net.Config.XmlConfigurator.Configure();

        log.Info("Test write from singleton");
    }
    public static TestSingletonLog4Net Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new TestSingletonLog4Net();
                }
            }

            return instance;
        }
    }
}

I setup a small project to duplicate the problem, and here is what is happening.

  • if I use log4net from the main UI project with either singleton or normal class logging works.
  • If I use log4net from sub-project libraries that are not a singleton logging works.
  • If I attempt to use log4net from a singleton that is in one of the sub-project libraries all logging stops, not only does it not log but also logging done from main project no longer work either.

I originally had the log4net configuration as part of the app.config but I suspected it may be a problem so I moved log4net configuration into it's own config file.

Using log4net version 1.2.11

I have also tried calling log from outside the constructor with the same results.

The reset of my setup is below.

The following line is just below the using statements in my Program.cs

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

In each class I am calling log4net with:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Any advice for a resolution is greatly appreciated.

4

2 回答 2

1

您应该只在您的应用程序中调用log4net.Config.XmlConfigurator.Configure(); 一次。这应该在主应用程序中调用,而不是在包含单例的类中。

此外,如果您要调用log4net.Config.XmlConfigurator.Configure();以下行,则应删除:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
于 2012-06-20T22:41:38.967 回答
0

我有一个使用 log4net 的单例对象。这就是我正在做的事情。

在成员声明中:

public static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof (TestSingletonLog4Net));

在构造函数中:

log4net.Config.XmlConfigurator.Configure();

而已。迄今为止没有任何问题。请让我知道这是否有效。

于 2012-06-20T19:08:22.070 回答