quartz定时+log4net日志+exchangeservice发邮件

main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using System.IO;
using Quartz;
using Quartz.Impl;

namespace Demolog4net
{
    class Program
    {
        static void Main(string[] args)
        {
            //从工厂中获取一个调度器实例化
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();       //开启调度器
            var logfig = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logfig);

            IJobDetail job1 = JobBuilder.Create<SendMessageJob>()  //创建一个作业
               .WithIdentity("作业名称", "作业组")
               .Build();
            ITrigger trigger1 = TriggerBuilder.Create()
                                       .WithIdentity("mytrigger", "group1")
                                       .StartNow()
                                       .WithCronSchedule("25 28 14 ? * *")    //时间表达式,14时28分25秒 
                                       .Build();
            scheduler.ScheduleJob(job1, trigger1);
        }
    }
}

Log

using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demolog4net
{
    class Logger
    {
        public Logger()
        {
            var logfig = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logfig);
            
        }

        public ILog getLogger( String type)
        {
            return log4net.LogManager.GetLogger(type);
        }
    }
}
using Microsoft.Exchange.WebServices.Data;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace Demolog4net
{
    class SendMessageJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {

            Logger log = new Logger();
            try
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
                service.Credentials = new NetworkCredential("发件人邮件地址", "密码", "邮件实例");
                service.AutodiscoverUrl("发件人邮件地址");
                EmailMessage message = new EmailMessage(service);
                message.Subject = "邮件标题";
                message.Body ="content";
                log.getLogger(typeof(IJob).ToString()).Warn("准备发送邮件");
                message.ToRecipients.Add("mengqin2015@outlook.com");
              //  message.SendAndSaveCopy();
                //发送邮件
           
                log.getLogger(typeof(IJob).ToString()).Info("发生成功");
            }
            catch (Exception ex)
            {
                log.getLogger(typeof(IJob).ToString()).Error("发送邮件失败");
            }
        }
      
    }
}

Log4net 配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--日志路径-->
      <param name= "File" value= "D:\App_Log\"/>
      <!--是否是向文件中追加日志-->
      <param name= "AppendToFile" value= "true"/>
      <!--log保留天数-->
      <param name= "MaxSizeRollBackups" value= "10"/>
      <!--日志文件名是否是固定不变的-->
      <param name= "StaticLogFileName" value= "false"/>
      <!--日志文件名格式为:2008-08-31.log-->
      <param name= "DatePattern" value= "yyyy-MM-dd&quot;.log&quot;"/>
      <!--日志根据日期滚动-->
      <param name= "RollingStyle" value= "Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />
      </layout>
    </appender>

    <!-- 控制台前台显示日志 -->
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="Info" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
      </layout>

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="Info" />
        <param name="LevelMax" value="Fatal" />
      </filter>
    </appender>

    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="all" />
      <appender-ref ref="ColoredConsoleAppender"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
</configuration>

 

posted @ 2016-09-30 14:42  春秋一梦  阅读(478)  评论(0编辑  收藏  举报