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

Using NLog via Common.Logging with NHibernate

Wiki Page Hierarchy

Pages

Page Details

First published by:
mgernand
on 08-16-2010
Last revision by:
mgernand
on 09-27-2010
3 people found this article useful.
Article
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:

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

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

<configuration>

 <configSections>

   <sectionGroup name="common">

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

   </sectionGroup>

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

 </configSections>

 <common>

   <logging>

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

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

     </factoryAdapter>

   </logging>

 </common>

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

       xmlns:xsi="www.w3.org/.../XMLSchema-instance">

   <targets>

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

   </targets>

   <rules>

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

   </rules>

 </nlog>

 <appSettings>

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

 </appSettings>

</configuration>

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

Common.Logging.dll

Common.Logging.NLog.dll

NHibernate.Logging.CommonLogging.dll

NLog.dll

Daniel

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