Best way to pass logging from Business Logic up to Hangfire Console

Hi All,
I have my code separated Data Layer, business logic Layer, and the Hang fire service consumes the BL layer.
I would rather not add HF references to my BL, but I want to add logging to the BL and see that logging in the HF console.

I have a couple of ideas but none seem to really work. Hoping someone has done this before and can save my self a few hours of tinkering and maybe someone can point me in the right direction.

Thanks in Advance!
Jeremy

2 Likes

I can think of 2 ways of doing this:

  1. (best way in my opinion) Create a log interface in your BL and Implement that log interface inside the Hangfire service so that it can access Hangfire.Console.

  2. Seperate your method where you want to log, Then call each part from your hangfire service and place log in between.

1 Like

Thanks for the direction. I went with number 1. it took me less than 15 mins to implement.

It really made the most sense. I put the interface for the logger in the same interface file as the back ground jobs Interfaces. minimal amount of code and worked perfectly.

Thanks again for the direction. Sometimes when you have been looking at a problem for and hour some times someone else option is a refreshing new concept :slight_smile:

1 Like

Would you mind sharing some code snippet, as now I’m facing exactly the same problem.

I am using NLog throughout the code and using the same NLog provider for hangfire merges the logs from both my job and hangfire. Again, question was not so clear so I suggested what I have done in my solution.

In our Common Project we have all the interfaces. This includes where we define the interfaces for the HF jobs.

In the same name space as the Interfaces for the HF jobs we added

    public interface IMyProjectBackgroundLogging
{
    void SendToLog(string LogMessage);
}

Then in the Business logic project we added this method to the class to every service that we wanted to add logging to. So this code is at the top of each class after init.

        private void SendToLogger(IMyProjectBackgroundLogging _LoggingObject, string LogMessage)
    {
        if (_LoggingObject != null)
        {
            _LoggingObject.SendToLog(LogMessage);
        }
    }

Then When we wanted to record something back to the HF.Console from with in the Business Logic. we updated the Method signature

        void SomeBusinessMethod(string someValue, IMyProjectBackgroundLogging _LoggingObject = null)
    {
            SendToLogger(_LoggingObject, $"someValue =  {someValue} ");

// other business logic in here
}

Then in side our HF web service We created the class to actually write to the HF.Console.

public class HFConsoleLogger : IMyProjectBackgroundLogging
{

    private PerformContext _currentContext;
    public void SendToLog(string LogMessage)
    {
        if(_currentContext!= null)
        {
            _currentContext.WriteLine(LogMessage);
        }
    }
    public PerformContext currentContext
    {
        get { return _currentContext; }
        set
        {
            _currentContext = value;

        }
    }
}

Then When the HF job runs at the top of the method for the job we put

            HFConsoleLogger _localCL = new HFConsoleLogger();
            _localCL.currentContext = context;

// then call the Business logic method to perform background task and pass the _localCL
myBusinessLogicClass.SomeBusinessMethod(“why so serious?”, _localCL)

By adding “IMyProjectBackgroundLogging _LoggingObject = null” to the method signature, we didn’t break any existing calls to the business logic as the parameter is optional.

Sorry this probably looks like a hot mess here on a message board but thats how we accomplished the task.

Let me know if you have any questions.