log4net在dll中不起作用

我目前在让log4net在特定的dll中工作时遇到问题。 我正在使用我的测试应用程序调用的其他dll中的log4net,并且日志记录在这些dll中运行正常,也在我的测试应用程序中。 这是我遇到麻烦的一个特殊的dll。 这是我遇到麻烦的dll代码片段。

//This is from ABC.dll public class SessionFactory { protected static ISessionFactory sessionFactory; private static readonly ILog log = LogManager.GetLogger(typeof(SessionFactory)); private static void Init() { try { //Read the configuration from hibernate.xml.cfg or app.config Configuration normalConfig = new Configuration().Configure(); ConfigureNhibernateValidator(normalConfig); log.Debug("Initializing session factory"); sessionFactory = Fluently.Configure(normalConfig) .Mappings(m => m.FluentMappings .AddFromAssemblyOf() .Conventions.AddFromAssemblyOf()) .ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu") .BuildSessionFactory(); log.Debug("Finished initializing the session factory"); } catch(Exception ex) { //Code not shown } } } 

在我的测试应用程序中,我打电话:

 log4net.Config.XmlConfigurator.Configure(); 

这是我的App.config文件中的log4net配置:

   

再次使用SpeedTest appender在我的测试应用程序中正常运行,但上面代码片段中的日志记录无法正常工作。 我在初始化记录器时设置了上面的断点,它似乎击中了它。 如果有必要,我可以发布log4net调试输出,但我没有看到太多。 如果您需要它,请告诉我,我会发布。

有关为什么日志记录没有记录在上面的代码片段中的任何建议?

似乎这个问题源于我在TFS中将目录更改为所有外部依赖项(log4net是其中之一)。 我所做的只是删除我在visual studio项目中的所有引用,并从我的新依赖项文件夹中重新添加它们,并且在此之后一切都按预期工作。 感谢所有在这里提供帮助的人。

我怀疑当你调用configure时它不会从配置文件中读取配置。

如果将以下行添加到应用程序中,您会看到什么(在控制台上,或在IDE输出窗口中,或通过在调试器中单步执行):

 var log4netConfig = ConfigurationManager.GetSection("log4net"); var log4netConfigIsNull = log4netConfig == null; Console.WriteLine(log4netConfigIsNull); 

看起来配置文件中实际可以使用配置吗?

[编辑:回复你的评论]

如果您现在向应用添加另一行调试代码:

 Console.WriteLine(log.IsDebugEnabled); 

你得到什么输出? 记录器是否认为它是为调试日志记录配置的? (我对SessionFactory.Init中或之后的行为特别感兴趣)。

直接的想法是你的日志记录代码没有被命中,可能是由于你的第一次log4net调用之前抛出exception。 你能把日志条目放入finally块,只是为了测试记录器的工作原理吗?