代码之家  ›  专栏  ›  技术社区  ›  Martin

从log4net转发到NLog

  •  7
  • Martin  · 技术社区  · 16 年前

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

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

    有一个 post about a log4net forwarder at the NLog forum

    4 回复  |  直到 14 年前
        1
  •  7
  •   Joachim Kerschbaumer    16 年前

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

    here , here here

        2
  •  2
  •   Anton Gogolev    16 年前

    基本上你需要一个log4net ( log4net.Appender.IAppender )这将委托所有人 DoAppend Logger Target .

        3
  •  1
  •   Julian    9 年前

    1. 使用NuGet添加Common。Logging.log4net和Common。登录中。NLog(通过包依赖关系获取log4net和NLog)
    2. 在……里面 Main() 使用以下命令初始化log4net 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>
    

    Common.Logging Bridging Logging Systems

    日志记录

        4
  •  0
  •   Manushin Igor    7 年前

    只需使用 log4net.NLogAppender

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

    Install-Package log4net.NLogAppender