如何在Quartz.Net中从Log4Net切换到NLog?

7

我公司的标准日志工具是NLog。我正在尝试引入Quartz.net,并被问及是否可以使用NLog而不是Log4Net。

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

3个回答

9
假设您使用的是Quartz.net 1.0.3,您需要添加对以下程序集的引用:
Common.Logging Common.Logging.NLog 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文件都被复制到正确的文件夹中。 - Brad Bruce
1
如果有人想在他/她的项目中只使用一个Common.Logging版本,我已经轻松地重新编译了Quartz 2.0。下载源代码,替换二进制文件并重新编译即可。 - Kjellski

4

使用配置指令确实是一种方法,但如果您已经有了现有的nlog配置,并且想要“插入”quartz日志记录,这是不必要的。

只需从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


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