Search code examples
nhibernatelog4netenterprise-library

Using Enterprise Library logging application block in NHibernate


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?


Solution

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

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <log4net>
            <appender name="MyAppender" type="MyAppender.CustomAppender,CustomAppender">
                <threshold value="DEBUG"/>
            </appender>
    
            <root>
                <level value="DEBUG" />
                    <appender-ref ref="MyAppender" />
            </root>
        </log4net>
    </configuration>
    

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