I've been using Hangfire for ages, but only recently discovered you can use Hangfire.Console to output logging, which can be seen in the Hangfire Dashboard.
I've seen it work on someone else's project, but when adding logging to my own project, I cannot make it work.
This is the gist of the task. I checked with Debugger that all lines are executed and the job does finish successfully.
public async Task MethodName(PerformContext context)
{
var result = await apiClient.MethodName();
if (result != null)
{
context.SetTextColor(ConsoleTextColor.Green);
context.WriteLine($"Checked: {result.Checked}");
context.WriteLine($"Cleared: {result.Cleared}");
}
else
{
context.SetTextColor(ConsoleTextColor.Red);
context.WriteLine($"No result ...");
}
}
I expected to find the output in my Hangfire dashboard, but it shows no logging whatsoever. Is there a crucial step I am missing?
Using Hangfire 1.7.18 and Hangfire.Console 1.4.2
I was having the same problem and it turns out that it's the async nature of your call that is causing the problem. If you make the API call synchronous it will do the console writing just fine. I have yet to find a solution for this. If there's a configuration I'm missing then I'm all ears.
Updated code would be something like this, note the "result" line has changed to
var result = apiClient.MethodName().Result;
: