6
votes

I'm currently having issues with getting log4net to work within a particular 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. It's this one particular dll that I'm having trouble with. Here is snippet of code from the dll I'm having trouble with.

//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:

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

3
Are you having trouble in the IDE or after you deploy? For some reason, no matter how many times I set log4net to CopyLocal=true in my projects, it continually gets set to false. So everything works fine until I deploy, at which point I have to make sure log4net.dll is present in the bin directory. - Dave Ziegler
I haven't actually tried this outside of the IDE (vs2k10 SP1). This is all failing while I actively debug through. - Cole W
I just noticed I'm using <root><priority value="DEBUG"/> where you have <root><level value="DEBUG"/> but I don't know what the difference is. Are the appenders chained together properly in that config xml? - Dave Ziegler
The documentation here says that it should be level. I tried it anyway but still got the same results. - Cole W
I fired up a console app with that exact config, called XmlConfigurator.Configure(), and the output was: nhibernate.txt 0KB, SpeedTest.txt 1KB so I'm not sure there is anything horribly wrong with that config section. Sorry, I guess I have a pretty vanilla log4net setup and haven't had to do anything this fancy before. I hope you figure this out! -edit: my console app was complaining about log4net missing a dependency on System.Web when I first compiled it, could you have dependency issues? - Dave Ziegler

3 Answers

6
votes

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. 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. Thanks for all those that helped here.

3
votes

My suspicion would be that it isn't reading the configuration from the configuration file when you call 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):

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? Does the logger think it is configured for debug logging? (I'm particularly interested in the behaviour of this in, or after, SessionFactory.Init).

0
votes

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. Can you put a log entry into a finally block, just to test that the logger works?