这两天在使用Common.Logger和NLog时。碰到了以前没有见到的问题。
在VS2012,我用Nutget安装了
Common.Logging
Common.Logger.NLog20
NLog
NLog Configuration
日志对象的初始化为:ILog logger = LogManager.GetLogger(Assembly.GetExecutingAssembly().GetName().Name);
程序执行时,发现并没有日志输出,在调试下用Add Watch去查看一个ILog日志对象,发现如下:(最初显示时,这里的IsDebugEnabled其实为false)
经过排查,发现是我在App.config里少了一个配置项,使得Common Logger找不到NLog。添加以下的配置项之后可以正常运行:
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
同时,在App.config这个文件中,还存在另一个必须的配置,在节点<configSections></configSections>(没有的话新建一个)添加:
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" />
</sectionGroup>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
注意:
<
configSections>
</configSections>应该是<configuration></configuration>的第一个节点,新建时要小心。
也有可能是另外的原因,就是NLog执行出错了。对此,我们可以配置查看NLog的执行过程:
在nlog.config文件里,在nlog头添加:
这样就可以查看NLog的执行日志了。(相当给力)
参见: