The nightmare of NLog and Common.Logging

So, I tried everything I could find to get these two to play together.

I installed the nuget Common.Logging.NLog20 package,

My config is as follows:

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

I use the Nuget NLog.Configuration package, so my nlog config is in a separate NLog.config file:

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

I tried changing FactoryAdaptor to NLog, NLog2 and NLog20, I tried changing the binding redirection, I tried updating Common.Logging to version 2.2.0.0. No matter what I do, I get an exception:

{"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)"}

What am I missing? It should not be hard to work.

+4
source share
4 answers

, Common.Logging v2.2.0.0, . Common.Logging.NLog20 nuget. .

+3

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>
+2

NLog Common.Logging. , " 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

Here is an approach that will save you a lot of trouble. The adapter that comes with Common.Logging.NLogXX, the NLogLoggerFactoryAdapter, does nothing special, but configures NLog for you. This is what most people can do on their own.

In other words, if you already configured NLog, all you have to do is connect Common.Logging to NLog. This can be done by writing a simple factory that will create NLog Loggers as follows:

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();
}
0
source

Source: https://habr.com/ru/post/1539488/


All Articles