【问题标题】:forward from log4net to NLog从 log4net 转发到 NLog
【发布时间】:2009-02-25 14:38:31
【问题描述】:

在我当前的项目中,我使用了两个库,其中一个使用 log4net,另一个使用 NLog 进行日志记录。我个人更喜欢 NLog,所以它也用于我的应用程序中。

我对 log4net 了解不多,所以我想知道以编程方式将所有消息从 log4net 转发到 NLog 的最佳方法是什么。

有一个post about a log4net forwarder at the NLog forum,但看起来以前没有人这样做过。

【问题讨论】:

  • 我会完全放弃 NLog。它被宣传为一个更好的选择,但它的创建者在被微软聘用后停止了它的工作,这很遗憾。我后来开始使用 log4net,我更喜欢它,也许看看文档并自己看看。
  • 感谢您的提示...我会考虑将 log4net 用于我的应用程序
  • NLog 似乎非常活跃 :-)
  • NLog 的创建者 Jaroslaw Kowalski 确实为 Microsoft 工作。但他非常积极地致力于 NLog。查看 NLog 网站以获取最近的下载、博客文章等。nlog-project.org 相比之下,log4net 自 2006 年以来一直没有更新。
  • 实际上,从 2008 年开始,看看它的源代码提交。

标签: c# .net logging log4net nlog


【解决方案1】:

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

看看hereherehere

【讨论】:

  • 有一个 Nuget 包可以从 log4net 映射到 NLog。 github.com/lanwin/log4net.NLogAppender 1. 使用 Nuget 安装。 Install-Package log4net.NLogAppender 2. 在C#中引用它:NLogAppender.Initialize();
【解决方案2】:

基本上,您需要一个 log4net appender (log4net.Appender.IAppender),它将所有 DoAppend 调用委托给 NLogs 的 LoggerTarget

【讨论】:

    【解决方案3】:

    我今晚正在尝试这样做。我看到 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 日志记录

    【讨论】:

      【解决方案4】:

      只需使用log4net.NLogAppender

      Nuget 链接:https://www.nuget.org/packages/log4net.NLogAppender/

      Nuget 安装:Install-Package log4net.NLogAppender

      【讨论】:

        猜你喜欢
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        相关资源
        最近更新 更多