2

I am getting the error message,"Error 19 'LogManager' is an ambiguous reference between 'Common.Logging.LogManager' and 'NLog.LogManager'".

In a C# 2008 application I am trying to add nlog open source logging tool to an application that is already using common.logging that was obtained from the following location: http://netcommon.sourceforge.net.

I have added a reference to the NLog file and I have added the Nlog to the using statement.

The problem is both tools use an object called 'LogManager'.

Thus can you tell me how to solve my problem so I can use both Logmanagers.

The following is my code listed below: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using Common.Logging;
     using sample;
     using System.Configuration;
     using System.Xml.Linq;  
     using NLog;

namespace sample
{
  public class Etest
  {
    private static Logger logger = LogManager.GetCurrentClassLogger(); 
    private static ILog log = LogManager.GetCurrentClassLogger();
  }
}

2 Answers 2

6

You just need to make sure the calls are well qualified.

public class Etest
{
   private static Logger logger = NLog.LogManager.GetCurrentClassLogger(); 
   private static ILog log = Common.Logging.LogManager.GetCurrentClassLogger();
}
0

You can define alias for one of them via using directive, or use full names.

But I don't think you need reference to NLog if you are using Common.Logging. Because Common.Logging library introduces a simple abstraction to allow you to select a specific logging implementation at runtime. So, your code should depend only on Common.Logging library, not some other logging system like log4net, or NLog:

using Common.Logging; // only this library is used
...
ILog log = LogManager.GetCurrentClassLogger();

And configure Common.Logging to use NLog :

<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="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/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>
</configuration> 
2
  • I basically need to use NLog as the configuration manager since I want the level of error messages. Also as part of this process, I will be removing common.logging with the nlog logic. Is there a way to just assign the Nlog to a variable? If so, how would setup setup the assign the value to a variable? Nov 14, 2012 at 3:15
  • @user1816979 actually Common.Logging adapter provides same level of messages - 'Trace, Debug, Info, Warning, Error, Fatal. If you really need to use NLog, then it's not that easy like change using Common.Logging` to using NLog. First - you need to change ILog to Logger. And you for sure have problems with calls to logger, because API is different - NLog uses {Level}Exception methods to write exceptions, Common.Logging uses {Level} methods for this. Also Common.Logging uses FormatMessageCallback for messages formatting. You will have to change all that stuff. Nov 14, 2012 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.