PageRenderTime 14ms CodeModel.GetById 6ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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