将日志记录器log4net转发到NLog

7
在我的当前项目中,我使用了两个库,其中一个使用log4net,另一个使用NLog进行日志记录。我个人更喜欢NLog,因此在我的应用程序中也使用了它。
我对log4net不是很了解,所以我想知道以编程方式将所有消息从log4net转发到NLog的最佳方法是什么。
在NLog论坛上有一篇关于log4net转发器的文章,但似乎之前没有人这样做过。

我会完全放弃NLog。它被宣传为更好的选择,但是它的创建者在被微软聘用后停止了对其的开发,这真是遗憾。后来我开始使用log4net,我更喜欢它,也许你可以看一下文档并自行判断。 - Pawel Krakowiak
谢谢您的提示...我会考虑在我的应用程序中使用log4net。 - Martin
9
NLog似乎非常活跃 :-) - Scott P
4
NLog的创始人Jaroslaw Kowalski确实在微软工作,但他非常积极地致力于NLog的开发。请访问NLog网站以获取最新下载、博客文章等信息。http://nlog-project.org/ 相比之下,log4net自2006年以来就没有进行更新。 - Jay Cincotta
自2008年以来,实际上,看看它的源代码提交。 - Henrik
4个回答

8
创建一个自定义的log4net Appender,将消息记录到nlog日志记录器中。如果您只想将日志信息传递给nlog而不是替换所有log4net日志记录,则这可能至少是解决方案。
查看 这里, 这里这里

2
有一个Nuget包可以将log4net映射到NLog。https://github.com/lanwin/log4net.NLogAppender
  1. 使用Nuget安装。Install-Package log4net.NLogAppender
  2. 在C#中引用:NLogAppender.Initialize();
- Marksl

2

基本上,你需要一个log4net的appenderlog4net.Appender.IAppender),该appender将会委托所有DoAppend调用到NLogs的LoggerTarget中。


1

我今晚想要做这件事。我看到Commons.Logging说它具有日志库之间的双向事件路由。

  1. 使用NuGet添加Common.Logging.log4net和Common.Logging.NLog(将通过包依赖项获取log4net和NLog)
  2. 添加以下配置。
  3. Main()中使用log4net.Config.XmlConfigurator.Configure();初始化log4net
<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>

参考资料:Common.Logging Bridging Logging Systems

Entlib 3.1、EntLib 4.1、log4net 1.2.9、log4net 1.2.10和NLog日志记录的全双向事件路由支持


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接