如何在运行时加载的程序集中使用通用log4net引用?

时间:2023-02-06 22:01:34

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?

我希望以这种方式加载的程序集使用与其余程序集相同的log4net.ILog引用。但似乎运行时加载的程序集完全具有不同的引用,需要自己的配置。有没有人知道是否可以在运行时使用.NET接口加载的程序集中使用单个log4net.ILog?

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

以下是Program类中的log4net.ILog创建和支持代码:

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

      }
   }

5 个解决方案

#1


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.

如果所有程序集都实现了公共接口,那么您可以使用属性或构造函数参数,该参数允许您将ILog的本地实例传递给动态加载的程序集。

#2


2  

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

您可以通过指定文字记录器名称字符串来获取相同的记录器,从而获得相同的记录器实例。

log4net.LogManager.GetLogger("SomeLogger");

#3


1  

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.

这个答案迟了4年,但我刚遇到同样的问题。在我的例子中,我发现正在加载的程序集(来自与调用程序集不同的文件夹)也加载了自己的log4net实例。

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

我通过从加载运行时程序集的文件夹中删除log4net.dll文件来解决此问题。

#4


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

关于运行时加载类的一些事情阻止了每个类通常的一个ILog工作。我可以获得一个有效的ILog实例,但与所有其他实例不同,它似乎没有配置(所有Is ** Enabled标志都设置为false)。也许运行时加载的类无法访问“根”记录器???

#5


0  

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

我有一个愚蠢的解决方案。您可以将XmlConfiguration设置为主log4net配置文件。

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

It is not really beauty .. but it runs.

它不是真正的美丽......但它运行。

#1


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.

如果所有程序集都实现了公共接口,那么您可以使用属性或构造函数参数,该参数允许您将ILog的本地实例传递给动态加载的程序集。

#2


2  

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

您可以通过指定文字记录器名称字符串来获取相同的记录器,从而获得相同的记录器实例。

log4net.LogManager.GetLogger("SomeLogger");

#3


1  

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.

这个答案迟了4年,但我刚遇到同样的问题。在我的例子中,我发现正在加载的程序集(来自与调用程序集不同的文件夹)也加载了自己的log4net实例。

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

我通过从加载运行时程序集的文件夹中删除log4net.dll文件来解决此问题。

#4


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

关于运行时加载类的一些事情阻止了每个类通常的一个ILog工作。我可以获得一个有效的ILog实例,但与所有其他实例不同,它似乎没有配置(所有Is ** Enabled标志都设置为false)。也许运行时加载的类无法访问“根”记录器???

#5


0  

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

我有一个愚蠢的解决方案。您可以将XmlConfiguration设置为主log4net配置文件。

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

It is not really beauty .. but it runs.

它不是真正的美丽......但它运行。