/Rhino.Etl.Core/WithLoggingMixin.cs

http://github.com/ayende/rhino-etl · C# · 109 lines · 62 code · 11 blank · 36 comment · 6 complexity · 35f43a322b6043f7cc97d73379ad28cf MD5 · raw file

  1. using Common.Logging;
  2. namespace Rhino.Etl.Core
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. /// <summary>
  8. /// A base class that expose easily logging events
  9. /// </summary>
  10. public class WithLoggingMixin
  11. {
  12. private readonly ILog log;
  13. readonly List<Exception> errors = new List<Exception>();
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="WithLoggingMixin"/> class.
  16. /// </summary>
  17. protected WithLoggingMixin()
  18. {
  19. log = LogManager.GetLogger(GetType());
  20. }
  21. /// <summary>
  22. /// Logs an error message
  23. /// </summary>
  24. /// <param name="exception">The exception.</param>
  25. /// <param name="format">The format.</param>
  26. /// <param name="args">The args.</param>
  27. protected void Error(Exception exception, string format, params object[] args)
  28. {
  29. string message = string.Format(CultureInfo.InvariantCulture, format, args);
  30. string errorMessage;
  31. if(exception!=null)
  32. errorMessage = string.Format("{0}: {1}", message, exception.Message);
  33. else
  34. errorMessage = message.ToString();
  35. errors.Add(new RhinoEtlException(errorMessage, exception));
  36. if (log.IsErrorEnabled)
  37. {
  38. log.Error(message, exception);
  39. }
  40. }
  41. /// <summary>
  42. /// Logs a warn message
  43. /// </summary>
  44. /// <param name="format">The format.</param>
  45. /// <param name="args">The args.</param>
  46. protected void Warn(string format, params object[] args)
  47. {
  48. if (log.IsWarnEnabled)
  49. {
  50. log.Warn(string.Format(CultureInfo.InvariantCulture, format, args), null);
  51. }
  52. }
  53. /// <summary>
  54. /// Logs a debug message
  55. /// </summary>
  56. /// <param name="format">The format.</param>
  57. /// <param name="args">The args.</param>
  58. protected void Debug(string format, params object[] args)
  59. {
  60. if (log.IsDebugEnabled)
  61. {
  62. log.Debug(string.Format(CultureInfo.InvariantCulture, format, args), null);
  63. }
  64. }
  65. /// <summary>
  66. /// Logs a notice message
  67. /// </summary>
  68. /// <param name="format">The format.</param>
  69. /// <param name="args">The args.</param>
  70. protected void Trace(string format, params object[] args)
  71. {
  72. if (log.IsTraceEnabled)
  73. {
  74. log.Trace(string.Format(CultureInfo.InvariantCulture, format, args), null);
  75. }
  76. }
  77. /// <summary>
  78. /// Logs an information message
  79. /// </summary>
  80. /// <param name="format">The format.</param>
  81. /// <param name="args">The args.</param>
  82. protected void Info(string format, params object[] args)
  83. {
  84. if (log.IsInfoEnabled)
  85. {
  86. log.Info(string.Format(CultureInfo.InvariantCulture, format, args), null);
  87. }
  88. }
  89. /// <summary>
  90. /// Gets all the errors
  91. /// </summary>
  92. /// <value>The errors.</value>
  93. public Exception[] Errors
  94. {
  95. get { return errors.ToArray(); }
  96. }
  97. }
  98. }