2014-05-06 132 views
4

所以我已經嘗試了一切,我可以找到讓這兩個一起玩。NLog和Common.Logging噩夢

我已經安裝了NuGet包Common.Logging.NLog20,

我的配置是這樣的:

<configSections> 
    <sectionGroup name="common"> 
     <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> 
    </sectionGroup> 
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog20" /> 
</configSections> 
<common> 
    <logging> 
     <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20"> 
      <arg key="configType" value="INLINE" /> 
     </factoryAdapter> 
    </logging> 
</common> 
<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
      <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" /> 
      <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
      <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" /> 
      <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
</runtime> 

我使用的NuGet NLog.Configuration包,所以我的n日誌配置是在一個單獨的文件所謂NLog.config:

<?xml version="1.0" encoding="utf-8" ?> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    internalLogFile="nlog.ERRORS.txt" internalLogLevel="Error" > 

<!-- 
See http://nlog-project.org/wiki/Configuration_file 
for information on customizing logging rules and outputs. 
--> 
<targets> 
    <!-- add your targets here --> 
    <target xsi:type="File" name="log" keepFileOpen="true" 
      fileName="${basedir}/log_${date:format=yyyyMMdd}.txt" 
      layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
    <target name="log_errors_memory" xsi:type="Memory" 
      layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
    <target name="log_all_memory" xsi:type="Memory" 
      layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
</targets> 

<rules> 
    <!-- add your logging rules here --> 
    <logger name="*" minlevel="Trace" writeTo="log" /> 
    <logger name="*" minlevel="Trace" writeTo="log_all_memory" /> 
    <logger name="*" minlevel="Error" writeTo="log_errors_memory" /> 
</rules> 
</nlog> 

我曾試圖改變FactoryAdaptor到NLOG,NLog2和NLog20,我試圖改變綁定重定向,我曾嘗試更新Common.Logging到版本2.2.0.0。不管我做什麼,我得到的例外:

{"Failed obtaining configuration for Common.Logging from configuration section 'common/logging'."} 

Inner Exception: 
{"An error occurred creating the configuration section handler for common/logging: Type Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20, Version=2.2.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e does not implement Common.Logging.ILoggerFactoryAdapter\r\nParameter name: factoryAdapterType\r\nActual value was Common.Logging.NLog.NLogLoggerFactoryAdapter. (D:\\Development\\Code\\DotNet\\vs2013\\exe\\CommandLine\\PSVImporter\\FidessaPSVImport.Test\\bin\\Debug\\FidessaPSVImport.Test.dll.config line 17)"} 

我錯過了什麼?這不應該很難得到工作。

+3

太多的收益太多的痛苦。創建了我自己的簡單抽象。 Goodby Common.Logging。 – Bitfiddler

+0

所以我注意到我的nlog configSection有NLog20而不是NLog,修正了這個問題,沒有骰子。此外,工廠屬性表示內聯,所以我刪除了NLog.config文件並將nlog部分放入appconfig。仍然沒有運氣。 – Bitfiddler

+0

晚會到了派對,但對於任何登陸此處的人:只有當你的NLog配置在同一個文件中時,'如果您的NLog配置位於單獨的文件中(如NLog.config),那麼您需要指定''並指定文件名。請參閱[docs](http://netcommon.sourceforge.net/docs/2.0.0/reference/html/ch01.html) – JamesQMurphy

回答

3

好的,在完成上述所有修復之後,我還必須將Common.Logging包更新到v2.2.0.0,然後手動更新綁定重定向。這實際上是Common.Logging.NLog20 nuget軟件包的次優部署。你不應該這樣做。

+0

我做了您所建議的所有事情,但我仍然收到相同的錯誤 –

+0

不幸的是我不是nLog/Common.Logging專家,所以以上所有內容都是我必須提供的。我所能建議的是仔細檢查以確保你的配置文件沒有輸入錯誤。我會將所有版本發佈在另一個答案中,併發布我的配置。它適用於我,所以它應該爲你工作。 – Bitfiddler

+0

我能想到的唯一的其他事情就是權限問題(就像你沒有訪問你的app.config文件一樣)。但是如果你在調試時遇到這個錯誤,我不會看到這會發生。 – Bitfiddler

2

Common.Logging.NLog20 - 版本2.2.0.0

Common.Logging - 版本2.2.0.0

NLOG - 版本2.1.0.0

配置是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<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.NLog20"> 
       <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" 
      internalLogFile="nlog.ERRORS.txt" internalLogLevel="Error"> 

     <!-- 
      See http://nlog-project.org/wiki/Configuration_file 
      for information on customizing logging rules and outputs. 
     --> 
     <targets> 
      <!-- add your targets here --> 
      <target xsi:type="File" name="log" keepFileOpen="true" 
        fileName="${basedir}/log_${date:format=yyyyMMdd}.txt" 
        layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
      <target name="log_errors_memory" xsi:type="Memory" 
        layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
      <target name="log_all_memory" xsi:type="Memory" 
        layout="${longdate} ${level:uppercase=true:padding=5} - ${logger:shortName=true} - ${message} ${exception:format=tostring}" /> 
     </targets> 

     <rules> 
      <!-- add your logging rules here --> 
      <logger name="*" minlevel="Trace" writeTo="log" /> 
      <logger name="*" minlevel="Trace" writeTo="log_all_memory" /> 
      <logger name="*" minlevel="Error" writeTo="log_errors_memory" /> 
     </rules> 
    </nlog> 
    </value> 
    <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
       <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
       <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" /> 
      </dependentAssembly> 
     </assemblyBinding> 
    </runtime> 
</configuration> 
0

最新的NLog沒有使用Common.Logging。在我來說,我只是不得不做以下,以避免錯誤「無法獲取配置從配置節‘共同/日誌’Common.Logging」

Uninstall-Package Common.Logging 
Uninstall-Package Common.Logging.Core 
Install-Package NLog.Config 

然後清除的app.config從不必要的配置&使用改爲使用nlog.config。喜歡這裏描述:http://nlog-project.org/download/

之後,我不得不重構了一下代碼,但它的工作原理幾乎相同的方式爲common.logging

LogManager.GetLogger(GetType().ToString()).Info("demo") 
0

這裏有一個祕密的方式,將節省你很多的麻煩。 Common.Logging.NLogXX,NLogLoggerFactoryAdapter附帶的適配器沒有什麼特別之處,但爲您設置了NLog。這是大多數人可以自己做的事情。

換句話說,如果你已經設置了NLog,你需要做的就是連接Common.Logging到NLog。這可以通過編寫一個簡單的工廠來完成,該工廠將創建如下所示的NLog Loggers:

public class MyAdapter : AbstractCachingLoggerFactoryAdapter 
{ 
    protected override ILog CreateLogger(string name) 
    { 
     return (ILog)typeof(NLogLogger).GetConstructor(
      BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(NLog.Logger) }, null) 
      .Invoke(new object[] { NLog.LogManager.GetLogger(name) }); 
    } 
} 

public static void ConfigureLogging() 
{ 
    Common.Logging.LogManager.Adapter = new MyAdapter(); 
}