Quartz.net 2.x在MVC站点中结合Log4net的使用

首先新建一个MVC的空站点:

Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

 

第二步,添加Quartz.net的引用

Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

 

在搜索处输入quartz.net搜索安装即可(目前是2.3)

Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

 

从上图可以看到它有个依赖项Common.Logging,安装时会自动将Common.Logging也安装上

同时安装上Common.Logging.Log4net(我这里选择的是新的Common.Logging.Log4Net1213)

Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用

 

Quartz.net的安装,你会发现Common.Logging.Log4Net1213会依赖Log4Net,所以Log4Net也将会被自动安装上

 

之所以上面这样用是因为Common.Logging是一个日志容器,假如以后你不想用Log4Net记录日志,改用NLog或者其它的日志记录组件,会很方便,只需更改配置,代码不会变

 

现在先把配置文件准备好,打开站点的Web.Config文件做好如下配置

 

<configSections>
    <!--配置Common.Logging节点-->
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <!--Log4net-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <common>
    <logging>
      <!--1.此Adapter只输出到控制台,如果是用的控制台,用这个输出-->
      <!--  
      <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">  
        <arg key="level" value="INFO" />  
        <arg key="showLogName" value="true" />  
        <arg key="showDataTime" value="true" />  
        <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />  
      </factoryAdapter>-->
      <!--2.此Adapter只输出到Log4.net的配置文件所指定的地方-->
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
        <!--FILE,FILE-WATCH,INLINE,EXTERNAL-->
        <arg key="configType" value="FILE" />
        <arg key="configFile" value="~/log4net.config" />
        <!-- 指定log4net的配置文件名称 -->
        <arg key="level" value="Warn" />
      </factoryAdapter>
    </logging>
  </common>
折叠展开

相关文章: