Quartz.Net: how to send instance of class through context
Asked Answered
W

1

1

I want pass object of Logger (NLog) to my job. I have a class

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);
        }
    }
}

and Job:

[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");
    }
}

How can I pass a logger to intance of my Job class through? Or in other words: How to put a logger to context? I can't find any information about it, even on offical site of Quartz.Net.

Whittaker answered 3/10, 2017 at 10:56 Comment(1)
Possible duplicate of Quartz Scheduler: How to pass custom objects as JobParameter?Recaption
W
3

I found a solution:

inside SomeClass:

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

Inside a Job:

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

    }
Whittaker answered 3/10, 2017 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.