Click here to Skip to main content
15,302,239 members
Articles / Programming Languages / C#
Technical Blog
Posted 22 Apr 2012

Tagged as


9 bookmarked

Common.Logging and Compatibility with Other Libraries

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 May 2012CPOL3 min read
This post shows you how to configure correctly Common.Logging together with Log4Net or NLog and explains the typical problems that you might expect while using this logging library.

It has been the second time since I have run into the issue of correctly configuring Common.Logging on my project. So what is the problem? Let's start with the basics:

Common.Logging should be a generic interface for logging which can be used by other frameworks and libraries to perform logging. The final user (you or me) uses several frameworks in his/her final application and if all of these frameworks will use different logging frameworks, it will turn into a configuration nightmare. So our favorite frameworks such as Spring.NET, Quartz.NET are using Common.Logging. This interface in turn uses a concrete logging framework to perform the logging (the act of writing the log lines to somewhere).

A typical scenario can be, for instance, the Common.Logging and Log4Net combination. In our application configuration file (web.config or app.config), we have to configure Common.Logging to use the Log4Net and then we can continue with the Log4Net configuration specifying what should be logged.

  <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
 <arg key="configType" value="INLINE" />

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%date %-5level %logger - %message%newline"/>

My general problem is that Common.Loggin.Log4Net facade is looking for a concrete version of the Log4Net library. Concretely, the version: 'log4net (= 1.2.10)'. That is not a problem if you are not using some other framework which depends on higher version of Log4Net.

In my case, the le_log4net library (the logentries library) is using log4net 2.0. So if you are using NuGet, you might obtain the following exception while adding the references:

Image 1

A similar thing might happen if you just decide to use the latest Log4Net by default. Then, you might get an exception when initializing Spring.NET context or starting the Quartz.NET scheduler:

Could not load file or assembly 'log4net, Version=, Culture=neutral, 
PublicKeyToken=b32731d11ce58905' or one of its dependencies. 
The located assembly's manifest definition does not match the assembly reference. 
(Exception from HRESULT: 0x80131040) 

Solution 1: Ignore NuGet, Define Runtime Binding

One way to get around this is to define runtime assembly binding. But this solution forces you to add the reference to log4net manually. NuGet controls the version and won't let you at references on the fly the way that you would. So to get over, add the latest Common.logging.Log4net façade and Log4Net version 2 (which you need for some reason). Then, you have to define the assembly binding in the configuration file.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e"/>
 <bindingRedirect oldVersion="" newVersion=""/>

Solution 2: Just Use the Older Version of Log4Net (1.2.10)

If you do not have libraries that are dependent on log4Net version 2.0.0, then just remember to always use log4net 1.2.10. This is the version which Common.Logging.Log4Net is looking for. Or just let NuGet manage it for you. You can add Common.Logging.Log4Net via NuGet and it will automatically load the correct version of Log4Net.

Solution 3: Try Other Logging Library for Instance NLog

This actually is not a real solution. I have experienced similar issues while using NLog, concretely try to use the latest NLog library with the Common.Logging.Nlog façade and you will obtain something similar to:

{"Could not load file or assembly 'NLog, Version=, Culture=neutral, 
PublicKeyToken=5120e14c03d0593c' or one of its dependencies. The located assembly's 
manifest definition does not match the assembly reference. 
(Exception from HRESULT: 0x80131040)":"NLog, Version=, Culture=neutral, 

The solution here is similar, you will have to define Runtime Binding:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <assemblyIdentity name="NLog" 
 publicKeyToken="5120e14c03d0593c" culture="neutral" />
 <bindingRedirect oldVersion="" newVersion="" />

What was interesting here is that NuGet actually took care of this for me. I have just added the Common.Logging.NLog façade and I guess NuGet spotted that I have already NLog 2 and that this Runtime Binding is necessary. If you look at the documentation of bindingRedirect, you will see that we have the right to specify the range of versions in the oldVersion attribute. Here, all the versions will be bound to the version.


Anyway, NLog and Log4Net are both cool logging frameworks, just use the one you prefer. As I have shown above, it is possible to use them together with Common.Logging. It just takes a few more lines to configure it correctly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior) OCTO Technology
Czech Republic Czech Republic
Writing software at ITG RFQ-hub.
Articles at OCTO blog

Comments and Discussions

SuggestionImages and code blocks Pin
Wendelius21-Apr-12 9:00
mveWendelius21-Apr-12 9:00 
The images should be located on this site in order to be available every time the blog is read.

Also the formatting of the code in the code blocks seems to be corrupted.
GeneralRe: Images and code blocks Pin
hoonzis21-Apr-12 22:41
Memberhoonzis21-Apr-12 22:41 
GeneralRe: Images and code blocks Pin
Wendelius21-Apr-12 22:53
mveWendelius21-Apr-12 22:53 
GeneralRe: Images and code blocks Pin
hoonzis22-Apr-12 0:06
Memberhoonzis22-Apr-12 0:06 
GeneralRe: Images and code blocks Pin
Wendelius22-Apr-12 0:10
mveWendelius22-Apr-12 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.