LoggerUtils.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using NLog;
  3. namespace OhmGraphite
  4. {
  5. public static class LoggerUtils
  6. {
  7. public static void LogAction(this Logger logger, string msg, Action action)
  8. {
  9. try
  10. {
  11. logger.Debug("Starting: {0}", msg);
  12. action();
  13. logger.Debug("Finished: {0}", msg);
  14. }
  15. catch (Exception e)
  16. {
  17. logger.Error(e, "Exception: {0}", msg);
  18. throw new Exception($"Exception with {msg}", e);
  19. }
  20. }
  21. public static T LogFunction<T>(this Logger logger, string msg, Func<T> fn)
  22. {
  23. try
  24. {
  25. logger.Debug("Starting: {0}", msg);
  26. var result = fn();
  27. logger.Debug("Finished: {0}", msg);
  28. return result;
  29. }
  30. catch (Exception e)
  31. {
  32. logger.Error(e, "Exception: {0}", msg);
  33. throw new Exception($"Exception with {msg}", e);
  34. }
  35. }
  36. }
  37. }