Hangfire: Log4Net stop working after Hangfire started

Created on 21 Sep 2016  路  6Comments  路  Source: HangfireIO/Hangfire

I starting hangfire server

Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(Constants.ConnectionString, new SqlServerStorageOptions
{
    PrepareSchemaIfNecessary = false,
    QueuePollInterval = TimeSpan.FromSeconds(15)
});
app.UseHangfireDashboard();

after that log4net stop writing logs

question

Most helpful comment

Thanks for the repository, I was able to reproduce the issue. I removed the [assembly: log4net.Config.XmlConfigurator(Watch = true)] attribute and called XmlConfigurator.Configure(); before initialising Hangfire, and everything is working now. Here are the xmldocs for XmlConfiguratorAttribute class that explain everything:

Log4net will only look for assembly level configuration attributes once. When using the log4net assembly level attributes to control the configuration of log4net you must ensure that the first call to any of the log4net.Core.LoggerManager methods is made from the assembly with the configuration attributes.

If you cannot guarantee the order in which log4net calls will be made from different assemblies you must use programmatic configuration instead, i.e. call the XmlConfigurator.Configure() method directly.

All 6 comments

Wasn't able to reproduce. Can you create a GitHub repository that reproduces the problem?

https://github.com/timurabdurafeev/HangfireLog4netConflict

  • open Startup.cs and remove/comment
    GlobalConfiguration.Configuration.UseSqlServerStorage("Data Source=(local);Initial Catalog=HangfireStuff;Integrated Security=True;"); app.UseHangfireDashboard(); app.UseHangfireServer();
  • run web app
  • make sure that

_debug.log
_error.log
_info.log

has been created

  • remove newly created log files
  • return back code lines which starting "hangfire" server
  • run web app again
  • check log files again - nothing is created

Thanks for the repository, I was able to reproduce the issue. I removed the [assembly: log4net.Config.XmlConfigurator(Watch = true)] attribute and called XmlConfigurator.Configure(); before initialising Hangfire, and everything is working now. Here are the xmldocs for XmlConfiguratorAttribute class that explain everything:

Log4net will only look for assembly level configuration attributes once. When using the log4net assembly level attributes to control the configuration of log4net you must ensure that the first call to any of the log4net.Core.LoggerManager methods is made from the assembly with the configuration attributes.

If you cannot guarantee the order in which log4net calls will be made from different assemblies you must use programmatic configuration instead, i.e. call the XmlConfigurator.Configure() method directly.

It works me as well, thank you

I am happy to find a solution to my issue. I had started testing hangfire before timurabdurafeev posted his question. It looked really impressive however it caused log4net to stop working. I should have posted my question! Thanks for the question and answer.

Thanks for sharing the information. Worked well for me.

Was this page helpful?
0 / 5 - 0 ratings