Configuring logging¶

Starting from Hangfire 1.3.0, you are not required to do anything, if your application already uses one of the following libraries through the reflection (so that Hangfire itself does not depend on any of them). Logging implementation is automatically chosen by checking for the presence of corresponding types in the order shown below.

If you want to log Hangfire events and have no logging library installed, please choose one of the above and refer to its documentation.

Console logger¶

For console applications and sandbox installations, there is the ColouredConsoleLogProvider class. You can start to use it by doing the following:

  1. LogProvider.SetCurrentLogProvider(new ColouredConsoleLogProvider());

Adding a custom logger¶

It is very simple to add a custom logger implementation. If your application uses another logging library that is not listed above, you only need to implement the following interfaces:

  1. public interface ILog
  2. {
  3. /// <summary>
  4. /// Log a message the specified log level.
  5. /// </summary>
  6. /// <param name="logLevel">The log level.</param>
  7. /// <param name="messageFunc">The message function.</param>
  8. /// <param name="exception">An optional exception.</param>
  9. /// <returns>true if the message was logged. Otherwise false.</returns>
  10. /// <remarks>
  11. /// Note to implementers: the message func should not be called if the loglevel is not enabled
  12. /// so as not to incur performance penalties.
  13. ///
  14. /// To check IsEnabled call Log with only LogLevel and check the return value, no event will be written
  15. /// </remarks>
  16. bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception = null);
  17. }
  18.  
  19. public interface ILogProvider
  20. {
  21. ILog GetLogger(string name);
  22. }

After implementing the interfaces above, call the following method:

  1. LogProvider.SetCurrentLogProvider(new CustomLogProvider());

Log level description¶

  • Trace – for debugging Hangfire itself.
  • Debug – to know why background processing does not work for you.
  • Info – to see that everything is working as expected: Hangfire was started or stopped, Hangfire components performed useful work. This is the recommended level to log.
  • Warn – to know learn about potential problems early: performance failed, but automatic retry attempt will be made, thread abort exceptions.
  • Error – to know about problems that may lead to temporary background processing disruptions or problems you should know about: performance failed, you need either to retry or delete a job manually, storage connectivity errors, automatic retry attempt will be made.
  • Fatal – to know that background job processing does not work partly or entirely, and requires manual intervention: storage connectivity errors, retry attempts exceeded, different internal issues, such as OutOfMemoryException and so on.

原文:

http://docs.hangfire.io/en/latest/configuration/configuring-logging.html