-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I decouple my logging interface from PerformContext? #90
Comments
Related to #58 |
You can create a custom For example, you can create the Here’s an example of a custom |
I'm just looking to implement this at the moment. Can you please elaborate a little bit? I'm a bit confused how exactly how the custom JobActivator is then used to inject the context? I have implemented the outlined JobActivator from your comment and then done the following implementation so far: //My Job
public class JobProcessor
{
private readonly PerformContext _performContext;
public JobProcessor(PerformContext performContext)
{
_performContext = performContext;
}
public void Do()
{
_performContext.WriteLine("Log Value 1");
//...Logic
_performContext.WriteLine("Log Value 2");
}
}
//Controller
public class HomeController : Controller
{
private readonly IBackgroundJobClient _backgroundJobClient;
private readonly JobProcessor _jobProcessor;
public HomeController(IBackgroundJobClient backgroundJobClient, JobProcessor jobProcessor)
{
_backgroundJobClient = backgroundJobClient;
_jobProcessor = jobProcessor;
}
public IActionResult Index()
{
_backgroundJobClient.Enqueue(() => _jobProcessor.Do());
return View();
}
} I'm not sure what I then register my job as scope wise without a error presenting itself. |
@JonnyBooker I'm not sure which error you're getting. Also, is there a reason you're injecting Usually there's no reason to register your job classes as scoped. You may even not register them at all, so they're always instantiated instead of being resolved from container. |
The injection of the JobProcessor was just as an example but in hindsight, that isn't the ideal implementation. Following your suggestion using
The reason for wanting to use an interface is being able the usage of separate Hangfire servers and clients via a shared assembly interface to split running of the jobs into a separate process. In its simplest form, this code below will yield the exception above all within one application. public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<JobActivator, InjectContextJobActivator>();
services.AddTransient<IJobProcessor, JobProcessor>();
services.AddHangfire(configuration => configuration
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseConsole()
.UseMemoryStorage()
);
services.AddHangfireServer();
}
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobClient)
{
app.UseRouting();
app.UseHangfireDashboard();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
backgroundJobClient.Enqueue<IJobProcessor>(x => x.Output("Sample Output"));
}
//Processor
public interface IJobProcessor
{
void Output(string text);
}
public class JobProcessor : IJobProcessor
{
private readonly PerformContext _performContext;
public JobProcessor(PerformContext performContext)
{
_performContext = performContext;
}
public void Output(string text)
{
_performContext.WriteLine(text);
Console.WriteLine(text);
}
}
} Not registering the job and using the concrete implementation will work as described. |
Can I write logs to dashboard without going through the PerformContext, which is quite obscure to resolve? Can I just call Hangfire.Console code to write logs?
The text was updated successfully, but these errors were encountered: