Base solution for your next web application
Open Closed

Logger in constructor of PeriodicBackgroundWorkerBase #3503


User avatar
0
ivanosw1 created

Hi, I would like to log same informations when my Worker is initialized, like parameters, options and so on. This is my ctor. The call to repository works, but the Logger doesn't write nothing. On DoWork method the Logger works.

public class IntegrationWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
....
public IntegrationWorker(AbpTimer timer,
            IRepository<ConfigurationData> configurationRepository) : base(timer)
        {
            _impostazioniRepository = impostazioniRepository;
            var periodData = _impostazioniRepository.FirstOrDefault(p => p.Parametro == "Polling");
            var period = periodData == null ? 15000 : int.Parse(periodData.Value);
            Logger.InfoFormat("Integration worker loaded with Period of {0} milliseconds", period);
            timer.Period = Period;
        }

Is possibile to log on constructor ?

Thank you.


3 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Logger come from base class and it is the property of base class. Property injection runs after class constructor method. If you inject the logger class to constructor as parameter, you can use it. I mean something like following:

    public IntegrationWorker(ILogger logger, AbpTimer timer,
                IRepository<ConfigurationData> configurationRepository) : base(timer)
    
  • User Avatar
    0
    ivanosw1 created

    Thank you. Your suggestion works.

    I've two different varibales that refers to the same logger, but in this particulare case I can survive with this.

  • User Avatar
    0
    alper created
    Support Team

    good to hear it works ;)