【问题标题】:How do I switch from Log4Net to NLog in Quartz.Net?如何在 Quartz.Net 中从 Log4Net 切换到 NLog?
【发布时间】:2011-07-04 10:01:10
【问题描述】:

我公司的标准日志记录工具是 NLog。我正在尝试介绍 Quartz.net,有人问我是否可以使用 NLog 代替 Log4Net。

我知道我可以重新编译以使用 NLog,但如果可能的话,我想从配置文件中进行。

【问题讨论】:

    标签: log4net quartz.net nlog common.logging


    【解决方案1】:

    假设您使用的是 Quartz.net 1.0.3。您必须添加对以下程序集的引用:

    Common.Logging
    Common.Logging.NLog
    日志

    然后你必须在你的应用程序的配置文件中添加以下配置:

    <configuration>
       <configSections>
          <sectionGroup name="common">
             <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
          </sectionGroup>
       </configSections>
    
       ...
    
       <common>
          <logging>
             <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog">
            <arg key="configType" value="FILE" />
            <arg key="configFile" value="~/NLog.config" />
         </factoryAdapter>
          </logging>
       </common>
    
    </configuration>
    

    请注意,我使用的是外部 NLog.config 文件。

    注意

    Quartz.net 使用 Common.Logging 1.2 版。

    【讨论】:

    • 效果很好!谢谢。只是给未来的读者的一个说明。确保将所有 DLL 复制到正确的文件夹中。
    • 以防万一有人在他/她的项目中只需要一个 Common.Logging 版本,我轻而易举地用 2.0 重新编译了 Quartz。下载源代码,替换二进制文件并重新编译。
    【解决方案2】:

    使用配置指令当然是这样做的一种方式,但如果您有一个现有的 nlog 配置并且只想“放入”石英日志记录,则没有必要。

    只需从 nuget 引用适当版本的“Common.Logging.NLog”,正常配置日志并将其添加到末尾:

    var config = new NameValueCollection();
    var adaptor = new NLogLoggerFactoryAdapter(config);
    Common.Logging.LogManager.Adapter = adaptor;
    

    所有石英日志记录(以及所有常见日志记录)现在都将转发到您现有的 nlog 配置。

    【讨论】:

      【解决方案3】:

      今天我面临同样的问题。 这篇文章对我帮助很大,但是事情有点改变......

      我使用 Quartz.Net 2.6.1。

      Common.Logging.NLog 已弃用,每个 NLog 版本都有一个新的 dll。

      所以新的配置是:

      <configuration>
         <configSections>
            <sectionGroup name="common">
               <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
            </sectionGroup>
         </configSections>
      
         ...
      
         <common>
            <logging>
               <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog21">
              <arg key="configType" value="FILE" />
              <arg key="configFile" value="~/NLog.config" />
           </factoryAdapter>
            </logging>
         </common>
      
      </configuration>

      NLogLoggerFactoryAdapter的命名空间没有改变,但是dll名称改变了。

      确保您拥有相同版本的:

      • Common.Logging

      • Common.Logging.Core

      • Common.Logging.NLogXX

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-08
        • 2014-07-27
        • 2010-10-17
        • 1970-01-01
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        相关资源
        最近更新 更多