如何在运行时加载的程序集中使用通用 log4net 引用?
我有一个单线程应用程序,它在运行时使用以下内容加载多个程序集:
objDLL = Assembly.LoadFrom(strDLLs[i]);
我希望以这种方式加载的程序集使用与其余程序集相同的 log4net.ILog 引用。 但运行时加载的程序集似乎具有完全不同的引用,并且需要自己的配置。 有谁知道单个 log4net.ILog 是否可以在使用 .NET 接口在运行时加载的程序集之间使用?
以下是 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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果所有程序集都实现一个公共接口,那么您可以拥有一个属性或构造函数参数,允许您将 ILog 的本地实例传递到动态加载的程序集。
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.
您可以通过指定文字记录器名称字符串来获取相同的记录器,从而获取相同的记录器实例。
You can get the same logger by specifying a literal logger name string, thus getting the same logger instance.
这个答案迟了四年,但我刚刚遇到了同样的问题。 就我而言,我发现正在加载的程序集(来自与调用程序集不同的文件夹)也在加载其自己的 log4net 实例。
我通过从加载运行时程序集的文件夹中删除 log4net.dll 文件解决了该问题。
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.
运行时加载的类的某些问题会阻止每个类通常使用一个 ILog 工作。 我可以获得有效的 ILog 实例,但与所有其他实例不同,它似乎未配置(所有 Is**Enabled 标志都设置为 false)。 也许运行时加载的类无法访问“根”记录器???
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???
我有一个愚蠢的解决方案。 您可以将 XmlConfiguration 设置为主 log4net 配置文件。
它并不是真正的美丽..但它运行。
I have a stupid solution. You can set the XmlConfiguration to the main log4net config file.
It is not really beauty .. but it runs.