-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: The answer to getting log4net logging working
PostPosted: Tue Feb 21, 2006 7:29 am 
Beginner
Beginner

Joined: Tue Jan 17, 2006 12:55 pm
Posts: 49
I wated a long time trying to get this logging working. Documentation is poor to say the least! So, here is the secret.

1) Add log4net configuration to your config file (ie app.config or web.config)
For instance, for a unit test app, you can use the trace output so it is displayed in the output window during IDE testing: You can set the other appenders for other situations.

Code:
  <log4net debug="false">
   
    <!-- Define some output appenders -->
   
    <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
   
    <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>

    <appender name="NHibernateFileLog" type="log4net.Appender.FileAppender">
      <file value="NHibernate.Output.log" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
   

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="NHibernate.Output.log" />
      <appendToFile value="false" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="-1" />
      <maximumFileSize value="50MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
      <file value="NHibernate.Output.log" />
      <appendToFile value="true" />
      <maximumFileSize value="100KB" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n"  />
      </layout>
    </appender>


    <!-- Setup the root category, add the appenders and set the default priority -->
   
   <root>
      <level value="WARN"/>
      <appender-ref ref="trace" />
    </root>

    <logger name="NHibernate.SQL" additivity="false">
      <level value="DEBUG"/>
      <appender-ref ref="trace"/>
    </logger>
  </log4net>


2) Add the following code to initialise logging. For instance, in the constructor of your unit test:
Code:
log4net.Config.XmlConfigurator.Configure();


Voila, lots of output!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.