Register with a given background Hangfire using NLog

So my problem is this: the method / function that I plan to / from Hangfire does a lot of recordings of the relevant information using NLog. When I run the method without hangfire, I get all the registration information I want, but when I plan the method with hangfire, all these entries are lost.

I know that Hangfire uses NLog (or any other logging structure you prefer) for its own logging purposes. I am using Hangfire from an ASP.NET MVC4 web application. I am using Ninject and installing hangfire using the OWIN launcher class, which currently looks like this:

public class Startup { public void Configuration(IAppBuilder app) { app.UseHangfire(config => { var kernel = new StandardKernel(new MyModule()); config.UseNinjectActivator(kernel); config.UseSqlServerStorage("Context"); LogProvider.SetCurrentLogProvider(new NLogLogProvider()); config.UseServer(); }); } } 

What I'm trying to do is that Hangfire can do its own logging, while my scheduled task also does all of its logging, I'm not interested in having it in one or more files. Hangfire is currently correctly registering:

 2015-02-25 14:32:41.4400|TRACE|Hangfire.Server.AutomaticRetryServerComponentWrapper|Sending start request for server component 'Server Bootstrapper'... 

and etc.

But the record from the scheduled task is nowhere to be seen.

What I am trying to ask is: how can I register from a planned method / task? I assume I don't have a context / context somewhere, but I have no clues.

+8
source share
1 answer

NLog probably cannot find the configuration on startup in the Hangfire job.

The solution is to:

  1. Or load a config with a path, for example

     LogManager.Configuration = new XmlLoggingConfiguration("c:/dir/nlog.config"); 
  2. Or create a config in C #. See documents

     var config = new LoggingConfiguration(); config.AddRuleForAllLevels(new FileTarget() { FileName = "c:/temp/my-logfile.log" }); LogManager.Configuration = config; //apply config 
0
source

Source: https://habr.com/ru/post/983092/


All Articles