6
votes

In my current project I'm using two libraries where one is using log4net and the other NLog for its logging. I'm personally prefer NLog so it is used in my application as well.

I'm not knowing much about log4net so I'm asking what would be the best way to programmatically forward all the messages from log4net to NLog.

There is a post about a log4net forwarder at the NLog forum but it looks like no one had done this before.

4
I would drop NLog altogether. It was advertised as a better alternative, but its creator stopped working on it after being hired by Microsoft, which is a shame.. I started using log4net later on and I like it more, maybe take a look the documentation and see for yourself.Pawel Krakowiak
Thanks for the hint ... I will consider to use log4net for my application insteadMartin
NLog appears to be very much active :-)Scott P
It is true that NLog's creator, Jaroslaw Kowalski works for Microsoft. But he is very actively working on NLog. Check out the NLog website for recent downloads, blog articles, etc. nlog-project.org In contrast, log4net hasn't been updated since 2006.Jay Cincotta
Since 2008, actually, look at its source commits.Henrik

4 Answers

7
votes

create a custom log4net Appender that logs the messages to a nlog logger. this may be at least the solution if you just want to pass the log information to nlog instead of replacing all occurences of log4net logging with nlog.

look here, here and here

2
votes

Basically you'll need a log4net appender (log4net.Appender.IAppender) which would delegate all DoAppend calls to NLogs' Logger or Target.

1
votes

I'm trying to do this tonight. I see Commons.Logging says it has bi-directional event routing between logging libraries.

  1. Use NuGet to add Common.Logging.log4net and Common.Logging.NLog (which will get log4net and NLog via package dependencies)
  2. Add the configuration below.
  3. In Main() initialize log4net using log4net.Config.XmlConfigurator.Configure();
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets async="true">
      <target name="file" xsi:type="File" fileName="d:\logs\app1\logging.txt"/>
      <target name="console" xsi:type="ColoredConsole" />
    </targets>
    <rules>
      <logger name="*" writeTo="file"/>
      <logger name="*" writeTo="console"/>
    </rules>
  </nlog>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <!-- Commons.Logging will bridge the log4net messages to NLog, required to see TopShelf log messages -->
    <appender name="CommonLoggingAppender" type="Common.Logging.Log4Net.CommonLoggingAppender, Common.Logging.Log4Net">
      <layout type="log4net.Layout.PatternLayout, log4net">
        <param name="ConversionPattern" value="%level - %class.%method: %message" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="CommonLoggingAppender" />
    </root>
  </log4net>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

References: Common.Logging Bridging Logging Systems

Full bi-directional event routing support for Entlib 3.1, EntLib 4.1, log4net 1.2.9, log4net 1.2.10 and NLog logging

0
votes

Just use log4net.NLogAppender

Nuget link: https://www.nuget.org/packages/log4net.NLogAppender/

Nuget installation: Install-Package log4net.NLogAppender