using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; using System.Web; using log4net; using log4net.Config; namespace Umbraco.Core.Logging { /// /// Used for logging /// public class Logger : ILogger { public Logger(FileInfo log4NetConfigFile) :this() { XmlConfigurator.Configure(log4NetConfigFile); } private Logger() { //Add custom global properties to the log4net context that we can use in our logging output log4net.GlobalContext.Properties["processId"] = Process.GetCurrentProcess().Id; log4net.GlobalContext.Properties["appDomainId"] = AppDomain.CurrentDomain.Id; } /// /// Creates a logger with the default log4net configuration discovered (i.e. from the web.config) /// /// public static Logger CreateWithDefaultLog4NetConfiguration() { return new Logger(); } /// /// Returns a logger for the type specified /// /// /// internal ILog LoggerFor() { return LogManager.GetLogger(typeof(T)); } /// /// Returns a logger for the object's type /// /// /// internal ILog LoggerFor(object getTypeFromInstance) { if (getTypeFromInstance == null) throw new ArgumentNullException("getTypeFromInstance"); return LogManager.GetLogger(getTypeFromInstance.GetType()); } public void Error(Type callingType, string message, Exception exception) { var logger = LogManager.GetLogger(callingType); if (logger != null) logger.Error((message), exception); } public void Warn(Type callingType, string message, params Func[] formatItems) { var logger = LogManager.GetLogger(callingType); if (logger == null || logger.IsWarnEnabled == false) return; logger.WarnFormat((message), formatItems.Select(x => x.Invoke()).ToArray()); } public void Warn(Type callingType, string message, bool showHttpTrace, params Func[] formatItems) { Mandate.ParameterNotNull(callingType, "callingType"); Mandate.ParameterNotNullOrEmpty(message, "message"); if (showHttpTrace && HttpContext.Current != null) { HttpContext.Current.Trace.Warn(callingType.Name, string.Format(message, formatItems.Select(x => x.Invoke()).ToArray())); } var logger = LogManager.GetLogger(callingType); if (logger == null || logger.IsWarnEnabled == false) return; logger.WarnFormat((message), formatItems.Select(x => x.Invoke()).ToArray()); } public void WarnWithException(Type callingType, string message, Exception e, params Func[] formatItems) { Mandate.ParameterNotNull(e, "e"); Mandate.ParameterNotNull(callingType, "callingType"); Mandate.ParameterNotNullOrEmpty(message, "message"); var logger = LogManager.GetLogger(callingType); if (logger == null || logger.IsWarnEnabled == false) return; var executedParams = formatItems.Select(x => x.Invoke()).ToArray(); logger.WarnFormat((message) + ". Exception: " + e, executedParams); } /// /// Traces if tracing is enabled. /// /// /// public void Info(Type callingType, Func generateMessage) { var logger = LogManager.GetLogger(callingType); if (logger == null || logger.IsInfoEnabled == false) return; logger.Info((generateMessage.Invoke())); } /// /// Traces if tracing is enabled. /// /// The type for the logging namespace. /// The message format. /// The format items. public void Info(Type type, string generateMessageFormat, params Func[] formatItems) { var logger = LogManager.GetLogger(type); if (logger == null || logger.IsInfoEnabled == false) return; var executedParams = formatItems.Select(x => x.Invoke()).ToArray(); logger.InfoFormat((generateMessageFormat), executedParams); } /// /// Debugs if tracing is enabled. /// /// /// public void Debug(Type callingType, Func generateMessage) { var logger = LogManager.GetLogger(callingType); if (logger == null || logger.IsDebugEnabled == false) return; logger.Debug((generateMessage.Invoke())); } /// /// Debugs if tracing is enabled. /// /// The type for the logging namespace. /// The message format. /// The format items. public void Debug(Type type, string generateMessageFormat, params Func[] formatItems) { var logger = LogManager.GetLogger(type); if (logger == null || logger.IsDebugEnabled == false) return; var executedParams = formatItems.Select(x => x.Invoke()).ToArray(); logger.DebugFormat((generateMessageFormat), executedParams); } } }