How do I use a common log4net reference in assemblies loaded at runtime?
Asked Answered
S

5

10

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

      }
   }
Saying answered 25/9, 2008 at 21:11 Comment(1)
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.Selfservice
R
2

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.

Regression answered 25/9, 2008 at 21:33 Comment(0)
B
2

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

log4net.LogManager.GetLogger("SomeLogger");
Byrle answered 9/10, 2008 at 1:44 Comment(0)
S
2

This answer comes 4 years late, but I just encountered the same issue. In my case, I discovered that the assembly that was being loaded (from a different folder than the calling assembly) was also loading its own instance of log4net.

I fixed the issue by deleting the log4net.dll file from the folder where the runtime assembly was being loaded.

Singspiel answered 16/10, 2012 at 15:43 Comment(1)
OMG ! This one solved my problem where logging in dynamically loaded assemblies never worked whereas it was working fine when the assemblies where in reference in the project.Devlin
S
0

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

Saying answered 25/9, 2008 at 21:43 Comment(0)
G
0

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

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

It is not really beauty .. but it runs.

Gunthar answered 22/10, 2008 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.