【发布时间】:2011-07-04 10:01:10
【问题描述】:
我公司的标准日志记录工具是 NLog。我正在尝试介绍 Quartz.net,有人问我是否可以使用 NLog 代替 Log4Net。
我知道我可以重新编译以使用 NLog,但如果可能的话,我想从配置文件中进行。
【问题讨论】:
标签: log4net quartz.net nlog common.logging
我公司的标准日志记录工具是 NLog。我正在尝试介绍 Quartz.net,有人问我是否可以使用 NLog 代替 Log4Net。
我知道我可以重新编译以使用 NLog,但如果可能的话,我想从配置文件中进行。
【问题讨论】:
标签: log4net quartz.net nlog common.logging
假设您使用的是 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 版。
【讨论】:
使用配置指令当然是这样做的一种方式,但如果您有一个现有的 nlog 配置并且只想“放入”石英日志记录,则没有必要。
只需从 nuget 引用适当版本的“Common.Logging.NLog”,正常配置日志并将其添加到末尾:
var config = new NameValueCollection();
var adaptor = new NLogLoggerFactoryAdapter(config);
Common.Logging.LogManager.Adapter = adaptor;
所有石英日志记录(以及所有常见日志记录)现在都将转发到您现有的 nlog 配置。
【讨论】:
今天我面临同样的问题。 这篇文章对我帮助很大,但是事情有点改变......
我使用 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
【讨论】: