NHibernate Forge
The official new home for the NHibernate for .NET community

Using NLog via Common.Logging with NHibernate

Wiki Page Hierarchy


Page Details

First published by:
on 08-16-2010
Last revision by:
on 09-27-2010
3 people found this article useful.
Comments (7)
History (11)
100% of people found this useful

Using NLog via Common.Logging with NHibernate

The NHibernate 3 Trunk-Version contains a  new Interface "IInternalLogger" which provides
interface for custom loggers to be configured with NHibernate. NHibernate provides'
a log4net log provider for this interface out of the box. So if you are using NHibernate
with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and
you are done. [:)]

But if you need or want to use an other logging framework, like NLog, you can provide
an ILogger implementation for your preferred logger. An implementantion can be found
on Codeplex: NHibernate.Logging. As some of you might know there
is the Common.Logging logging abstration framework available. Common.Logging
supports several other logging frameworks. So you can use each of them with NHibernate
via Common.Logging.

The following logging frameworks are supported by Common.Logging:

  • System.Console
  • System.Diagnostics.Trace
  • Log4Net 1.2.9
  • Log4Net 1.2.10
  • NLog
  • Enterprise Library 3.1 Logging
  • Enterprise Library 4.1 Logging

To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you
have to do is to copy the following Assemblies to you projects output directory:

  • Common.Logging.dll
  • NHibernate.Logging.CommonLogging.dll

Needless to say that you have to copy your loggers Assembly and the
corresponding Common.Logging-Provider Assembly too.

To enable the log provider you have to add the following lines to your App.config:

    <add key="nhibernate-logger"
              value="NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging"/>

The Common.Logging providers source code and binaries can be found on Codeplex:
NHibernate.Logging. You may try out the provider and send me feedback about it. Have fun. [:)]

Recent Comments

By: mgernand Posted on 02-08-2013 10:42

I just moved to project to GitHub: github.com/.../nhibernate-logging

The provider is also available as NuGet package: nuget.org/.../NHibernate.Logging

By: tillias Posted on 05-04-2011 14:47

There is also another possibility to integrate with NLog directly without Common.Logging at all.

Here is the description: tillias.wordpress.com/.../nhibernate-logging-and-removing-nlog-direct-reference

By: Daniel Lidstrom Posted on 01-23-2011 18:17

The configuration example is wrong. Here's a complete example that works (for a console application):

<?xml version="1.0"?>



   <sectionGroup name="common">

     <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />


   <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>




     <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog">

       <arg key="configType" value="INLINE" />




 <nlog xmlns="www.nlog-project.org/.../NLog.xsd"



     <target name="console" xsi:type="Console" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" />



     <logger name="*" minlevel="Debug" writeTo="console" />




   <add key="nhibernate-logger" value="NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, NHibernate.Logging.CommonLogging" />



The above is configured for NLog. You need the following assemblies in your bin:






By: mgernand Posted on 12-19-2010 8:48

After the release of NH 3.0.0-GA I have created a corresponding realease for the Common.Logging provider. You can find the release on Codeplex: nhlogging.codeplex.com/.../56937

By: Fabio Maulo Posted on 08-27-2010 8:36

For NH3.0Alpha2 you can download binaries.

btw my question is about the content of the wiki.

Can I use it with NHibernate 3.0.0.Alpha2 and above or only with the trunk ?

View All
Powered by Community Server (Commercial Edition), by Telligent Systems