Thursday, April 14, 2011

Using Enterprise Library logging application block in NHibernate

Hi all:

We are trying to integrate NHibernate as our OR/M, however, we are currently using Enterprise Library's logging application block. I know that NHibernate uses log4net to log. Does anyone have any example on how to use Enterprise Library to log NHibernate related logs?

Thanks

From stackoverflow
  • Why not just let nHibernate use log4net? Yes you have to manage two but otherwise you'd have to write an adapter for log4net to log to EntLibrary.

    I use EntLibrary as well, and just deal with Log4Net being there for nHibernate. On their dev discussion group they talked about removing log4net as a depedancy but I don't think any work has been done on it.

    Chris Brandsma : I would agree with this. There is the added issue the of he amount of stuff that NHibernate logs. If you turn on both logging options you can get a megabytes of data in minutes (and really kill performance). This is something you want on for only short periods of time.
  • Write your own log4net appender that writes to a EL logger. It's an adapter pattern.

    inherit a new/custom appender class from log4net.Appender.AppenderSkeleton

    override the Append event handler from the skeleton class, and in it show the RenderedMessage, something like this:

    using System; using log4net; using System.Windows.Forms;

    namespace MyAppender { public class CustomAppender : log4net.Appender.AppenderSkeleton { protected override void Append(log4net.spi.LoggingEvent log) { // log to EL logger based on log properties. } } }

    you then need to configure log4net config file....

        <root>
            <level value="DEBUG" />
                <appender-ref ref="MyAppender" />
        </root>
    </log4net>
    

    I have not tested this, but it should get you going.

  • This was something I had been wondering about myself. I can confirm for you that NHibernate has a hard dependency on Log4Net, therefore, you will have to write an appender as Josh noted.

0 comments:

Post a Comment