简体   繁体   English

log4net 在 dll 中不起作用

[英]log4net not working in dll

I'm currently having issues with getting log4net to work within a particular dll.我目前在让 log4net 在特定 dll 中工作时遇到问题。 I'm currently using log4net in other dlls being called by my test app and logging is working fine within those dlls and also within my test app.我目前在我的测试应用程序调用的其他 dll 中使用 log4net,并且日志记录在这些 dll 和我的测试应用程序中工作正常。 It's this one particular dll that I'm having trouble with.这是我遇到问题的一个特殊的 dll。 Here is snippet of code from the dll I'm having trouble with.这是我遇到问题的 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<OrderHeaderMap>()
                  .Conventions.AddFromAssemblyOf<PascalCaseColumnNameConvention>())
               .ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
              .BuildSessionFactory();

            log.Debug("Finished initializing the session factory");
        }
        catch(Exception ex)
        {
            //Code not shown
        }
    }
}

In my test app I am calling:在我的测试应用程序中,我正在调用:

log4net.Config.XmlConfigurator.Configure();

Here is my log4net configuration in my App.config file:这是我的 App.config 文件中的 log4net 配置:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <!-- ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="SpeedTest"/>
    </root>

    <!-- This is a default logger that nhibernate uses to push out all the SQL statements to-->
    <logger name="NHibernate.SQL" additivity="false">
      <level value="DEBUG"/>
      <appender-ref ref="NHibernateConsoleLog"/>
      <appender-ref ref="NHibernateFileLog"/>
    </logger>

    <!-- This is a default logger that nhibernate uses to push out all the debugging type information to-->
    <logger name="NHibernate" additivity="false">
      <level value="DEBUG"/>
      <appender-ref ref="NHibernateFileLog"/>
    </logger>

    <appender name="NHibernateConsoleLog" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>

    <appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/nhibernate.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
      </layout>
    </appender>

    <appender name="SpeedTest" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/SpeedTest.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
      </layout>
    </appender>
  </log4net>

</configuration>

Again logging is working fine within my test application using the SpeedTest appender but the logging within the above snippet of code is not working.再次使用 SpeedTest 附加程序在我的测试应用程序中进行日志记录工作正常,但上述代码片段中的日志记录不起作用。 I set breakpoints above on when it initializes the logger and it seems to hit it.我在初始化记录器时在上面设置了断点,它似乎命中了它。 I can post the log4net debug output if necessary but I didn't really see much.如有必要,我可以发布 log4net 调试 output 但我并没有真正看到太多。 Just let me know if you need it and I will post.如果您需要,请告诉我,我会发布。

Any suggestions as to why logging is not being recorded in the above snippet of code?关于为什么上面的代码片段中没有记录日志记录的任何建议?

It seems that this issue was stemming from me changing the directory to all my external dependencies (log4net being one of them) awhile back in TFS.似乎这个问题源于我在 TFS 中将目录更改为我的所有外部依赖项(log4net 就是其中之一)。 All I did was drop all my references in my visual studio project and re-add them from my new dependencies folder and everything worked as expected after this.我所做的只是在我的 Visual Studio 项目中删除所有引用,然后从我的新依赖项文件夹中重新添加它们,之后一切都按预期工作。 Thanks for all those that helped here.感谢所有在这里提供帮助的人。

My suspicion would be that it isn't reading the configuration from the configuration file when you call configure.我的怀疑是当你调用 configure 时它没有从配置文件中读取配置。

If you add the following lines to your application, what do you see (either on the console, or in IDE output window, or by stepping through in the debugger):如果您将以下几行添加到您的应用程序,您会看到什么(在控制台上,或在 IDE output window 中,或通过调试器中的单步执行):

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

Does it look like the configuration is actually available from the configuration file?看起来配置实际上可以从配置文件中获得吗?

[Edit: in response to your comment] [编辑:回应您的评论]

If you now add another line of debug code to your app:如果您现在向您的应用添加另一行调试代码:

Console.WriteLine(log.IsDebugEnabled);

what output do you get?你得到什么 output? Does the logger think it is configured for debug logging?记录器是否认为它已配置为调试日志记录? (I'm particularly interested in the behaviour of this in, or after, SessionFactory.Init). (我对 SessionFactory.Init 中或之后的行为特别感兴趣)。

The immediate thought would be that your logging code isn't getting hit, possibly due to an exception being thrown prior to your first log4net call.直接的想法是您的日志记录代码没有受到影响,可能是由于在您的第一次 log4net 调用之前引发了异常。 Can you put a log entry into a finally block, just to test that the logger works?您可以将日志条目放入 finally 块中,只是为了测试记录器是否工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM