Quartz.NET配置(Log4net)

时间:2023-03-08 15:39:55

最近有个任务关于服务调度,想起以前看过Quartz.NET调度任务非常棒。

今天小试Quartz.NET,前面配置Quartz.NET很轻松,控制台也输出了。但是想配合Log4net来做日志文件,怎么设置App.config文件都不行。

报如下错误:

Failed obtaining configuration for Common.Logging from configuration section 'common/logging'.

Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211‘.

各程序集版本如下:

  • Quartz.NET (2.2.4.0)
  • Common.Logging.dll (2.1.2.0)(下载Quartz.NET时自带)
  • Common.Logging.Log4Net1211.dll (2.2.0.0)(重新从Nuget下载)
  • log4net.dll (1.2.11.0)(下载Common.Logging.Log4Net1211.dl时自带)

折腾了一个下午,没有搞出来。

看了下面地址中的思路,感觉也不对:

http://*.com/questions/11368267/failed-obtaining-configuration-for-common-logging-from-configuration-section-co

回到家后,再想是不是Quartz.NET自带的Common.Logging.dll 的版本不正确。

重新升级到Common.Logging.dll(2.2.0.0),还自带出了Common.Logging.Core.dll(感觉对头了,哈哈)

不知道是不是Quartz.NET出了问题?

再测试,果然出现了日志文件,并且日志文件中有内容。

重新贴出各程序集版本:

  • Quartz.NET (2.2.4.0)
  • Common.Logging.dll (2.2.0.0)(升级后)
  • Common.Logging.Core.dll(2.2.0.0)
  • Common.Logging.Log4Net1211.dll (2.2.0.0)(重新从Nuget下载)
  • log4net.dll (1.2.11.0)(下载Common.Logging.Log4Net1211.dl时自带)

App.config配置文件如下:

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<log4net>
<appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log/" />
<appendToFile value="true" />
<param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="1024KB" />
<staticLogFileName value="false" />
<Encoding value="UTF-8" />
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log/error.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10240KB" />
<staticLogFileName value="true" />
<Encoding value="UTF-8" />
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="InfoFileAppender" />
<appender-ref ref="ErrorFileAppender" />
</root>
</log4net>
<common>
<logging>
<!-- <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true"/>
<arg key="showDataTime" value="true"/>
<arg key="level" value="INFO"/>
<arg key="dateTimeFormat" value="HH:mm:ss:fff"/>
</factoryAdapter>-->
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
<quartz>
<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="60000" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
<!-- sample configuration based db provider -->
<add key="quartz.dbprovider.customProvider.productName" value="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" />
<add key="quartz.dbprovider.customProvider.assemblyName" value="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.connectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.commandType" value="System.Data.SqlClient.SqlCommand, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.parameterType" value="System.Data.SqlClient.SqlParameter, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.commandBuilderType" value="System.Data.SqlClient.SqlCommandBuilder, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.parameterDbType" value="System.Data.SqlDbType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.parameterDbTypePropertyName" value="SqlDbType" />
<add key="quartz.dbprovider.customProvider.parameterNamePrefix" value="@" />
<add key="quartz.dbprovider.customProvider.exceptionType" value="System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add key="quartz.dbprovider.customProvider.useParameterNamePrefixInParameterCollection" value="true" />
<add key="quartz.dbprovider.customProvider.bindByName" value="true" />
<add key="quartz.dbprovider.customProvider.dbBinaryTypeName" value="Image" />
</quartz>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.11.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

配置文件