pieceofsummer / Hangfire.Console

Job console extension for Hangfire
MIT License
436 stars 80 forks source link

Code with an example #109

Closed erossini closed 3 years ago

erossini commented 3 years ago

Hi, I found this code and it is what I'm looking for but I don't know how to use in my code.

I start a process with

var jobId = BackgroundJob.Enqueue(() => _process.Start(User.Identity.Name, filename));

and then I have a generic class to run the process

public class ImportExcelProcessBase<T> where T : class
{
    protected readonly IImportExcel _excel;

    public ImportExcelProcessBase(IImportExcel excel)
    {
        _email = email;
    }

    public void TaskMethod(PerformContext context)
    {
        context.WriteLine("Hello, world!");
    }

    public async Task Start(string username, string filename)
    {
        _log.LogDebug($"User {username} started the import for {filename}");
    }
}

How can I write a log in Hangfire? Where is PerformContext injected?

ali-gol commented 3 years ago

You should take that parameter to the method which you want to execute. In your case it is the Start method. HangFire simply inject that method. And your code should be like: var jobId = BackgroundJob.Enqueue(() => _process.Start(User.Identity.Name, filename, null));

And your Start method would be like:

public async Task Start(string username, string filename, PerformContext context)
{
       _log.LogDebug($"User {username} started the import for {filename}");
}
erossini commented 3 years ago

Thank you so much!