Quartz.Net:如何通过上下文

时间:2017-10-03 10:56:22

标签: c# quartz.net

我想将Logger(NLog)的传递对象传递给我的工作。 我有一个班级

public partial class SomeClass
{
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
    public void SomeMethod() {
        try
        {
            _scheduler = StdSchedulerFactory.GetDefaultScheduler();
            IJobDetail job = JobBuilder.Create<Job>()
                .WithIdentity("job1", "group1")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(5)
                    .RepeatForever())
                .Build();

            _scheduler.ScheduleJob(job, trigger);
            _scheduler.Start();
        }
        catch (SchedulerException es)
        {
            Logger.Trace(es);
            Logger.Fatal(es);
        }
    }
}

和工作:

[DisallowConcurrentExecution]
public class Job : IJob
{
    private static int i = 0;
    /// <inheritdoc />
    public Job()
    {
    }

    /// <inheritdoc />
    public void Execute(IJobExecutionContext context)
    {
        var logger = (Logger)context.Get("logger");//How ????
        logger.Debug("etc");
    }
}

如何通过记录器传递给我的Job类? 或者换句话说:如何将记录器放到上下文中? 我找不到任何有关它的信息,即使是在Quartz.Net的官方网站上也是如此。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

在SomeClass中

IJobDetail job = JobBuilder.Create<Job>()
            .WithIdentity("job1", "group1")
            .Build();
job.JobDataMap["logger"] = Logger;

在工作中:

    public void Execute(IJobExecutionContext context)
    {
        var logger = (Logger)context.JobDetail.JobDataMap["logger"])

    }