views:

724

answers:

6

I have a single-threaded application that loads several assemblies at runtime using the following:

objDLL = Assembly.LoadFrom(strDLLs[i]);

I would like the assemblies loaded in this manner to use the same log4net.ILog reference as the rest of the assemblies do. But it appears the runtime loaded assemblies have a different reference altogether and need their own configuration. Does anyone know if a single log4net.ILog can be used across assemblies loaded at runtime using a .NET interface?

Here is the log4net.ILog creation and supporting code in the Program class:

   // Configure log4net using the .config file
   [assembly: log4net.Config.XmlConfigurator(Watch = true)]

   public static class Program
   {
      private static log4net.ILog m_Log = null;

      [STAThread]
      public static void Main(string[] args)
      {
         try
         {
            m_Log = log4net.LogManager.GetLogger(
               MethodBase.GetCurrentMethod().DeclaringType);
         }

      }
   }
A: 

Why would you need them all to have the same ILog? You can have one ILog per class, and they all log to the root logger by default.

Matt Howells
"Why would you do X when you can do Y?!" isn't a very good response.
yodaj007
+2  A: 

If all your assemblies implement a common interface, then you could have a property or constructor parameter that allows you to pass your local instance of ILog to the dynamically loaded assemblies.

JPrescottSanders
A: 

Something about the runtime loaded class prevents the usual one ILog per class from working. I can get a valid ILog instance, but unlike all the other instances it appears not to be configured (all the Is**Enabled flags are set to false). Perhaps the "root" logger is not accessible to the classes loaded at runtime???

+1  A: 

You can get the same logger by specifying a literal logger name string, thus getting the same logger instance.

log4net.LogManager.GetLogger("SomeLogger");
Jerome Laban
A: 

I have a stupid solution. You can set the XmlConfiguration to the main log4net config file.

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

Is not really beauty .. but runs.

A: 

http://www.nlog-project.org/faq.html read section "How do I configure logging for a component?" Essentially, create your own LogManager.

anon