diff --git a/src/Orchard.Web/Config/log4net.config b/src/Orchard.Web/Config/log4net.config index e129ba1a2..d28757088 100644 --- a/src/Orchard.Web/Config/log4net.config +++ b/src/Orchard.Web/Config/log4net.config @@ -61,7 +61,7 @@ - + @@ -81,7 +81,7 @@ - + diff --git a/src/Orchard/Logging/OrchardLog4netLogger.cs b/src/Orchard/Logging/OrchardLog4netLogger.cs index d204495a8..ebf9a1eca 100644 --- a/src/Orchard/Logging/OrchardLog4netLogger.cs +++ b/src/Orchard/Logging/OrchardLog4netLogger.cs @@ -1,6 +1,9 @@ using System; using System.Globalization; +using System.Web; +using Orchard.Environment; +using Orchard.Environment.Configuration; using log4net; using log4net.Core; using log4net.Util; @@ -9,10 +12,13 @@ using Logger = Castle.Core.Logging.ILogger; namespace Orchard.Logging { [Serializable] - public class OrchardLog4netLogger : MarshalByRefObject, Logger { + public class OrchardLog4netLogger : MarshalByRefObject, Logger, IShim { private static readonly Type declaringType = typeof(OrchardLog4netLogger); + public IOrchardHostContainer HostContainer { get; set; } + private ShellSettings _shellSettings; public OrchardLog4netLogger(log4net.Core.ILogger logger, OrchardLog4netFactory factory) { + OrchardHostContainerRegistry.RegisterShim(this); Logger = logger; Factory = factory; } @@ -24,6 +30,49 @@ namespace Orchard.Logging { : this(log.Logger, factory) { } + // Return a per class variable for each instance of the logger, which is for each tenant. This variable allows outputting the tenant name + private ShellSettings ShellSettings { + get { + if (_shellSettings == null) { + var ctx = HttpContext.Current; + if (ctx == null) + return null; + + var runningShellTable = HostContainer.Resolve(); + if (runningShellTable == null) + return null; + + var shellSettings = runningShellTable.Match(new HttpContextWrapper(ctx)); + if (shellSettings == null) + return null; + + var orchardHost = HostContainer.Resolve(); + if (orchardHost == null) + return null; + + var shellContext = orchardHost.GetShellContext(shellSettings); + if (shellContext == null || shellContext.Settings == null) + return null; + + _shellSettings = shellContext.Settings; + } + + return _shellSettings; + } + } + + // Load the log4net thread with additional properties if they are available + protected internal void AddExtendedThreadInfo() { + if (ShellSettings != null) { + ThreadContext.Properties["Tenant"] = ShellSettings.Name; + } + + var ctx = HttpContext.Current; + if (ctx != null) { + ThreadContext.Properties["Url"] = ctx.Request.Url.ToString(); + } + } + public bool IsDebugEnabled { get { return Logger.IsEnabledFor(Level.Debug); } } @@ -58,210 +107,245 @@ namespace Orchard.Logging { public void Debug(String message) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, message, null); } } public void Debug(Func messageFactory) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, messageFactory.Invoke(), null); } } public void Debug(String message, Exception exception) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, message, exception); } } public void DebugFormat(String format, params Object[] args) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } } public void DebugFormat(Exception exception, String format, params Object[] args) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } } public void DebugFormat(IFormatProvider formatProvider, String format, params Object[] args) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, new SystemStringFormat(formatProvider, format, args), null); } } public void DebugFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { if (IsDebugEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Debug, new SystemStringFormat(formatProvider, format, args), exception); } } public void Error(String message) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, message, null); } } public void Error(Func messageFactory) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, messageFactory.Invoke(), null); } } public void Error(String message, Exception exception) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, message, exception); } } public void ErrorFormat(String format, params Object[] args) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } } public void ErrorFormat(Exception exception, String format, params Object[] args) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } } public void ErrorFormat(IFormatProvider formatProvider, String format, params Object[] args) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, new SystemStringFormat(formatProvider, format, args), null); } } public void ErrorFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { if (IsErrorEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Error, new SystemStringFormat(formatProvider, format, args), exception); } } public void Fatal(String message) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, message, null); } } public void Fatal(Func messageFactory) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, messageFactory.Invoke(), null); } } public void Fatal(String message, Exception exception) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, message, exception); } } public void FatalFormat(String format, params Object[] args) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } } public void FatalFormat(Exception exception, String format, params Object[] args) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } } public void FatalFormat(IFormatProvider formatProvider, String format, params Object[] args) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, new SystemStringFormat(formatProvider, format, args), null); } } public void FatalFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { if (IsFatalEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Fatal, new SystemStringFormat(formatProvider, format, args), exception); } } public void Info(String message) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, message, null); } } public void Info(Func messageFactory) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, messageFactory.Invoke(), null); } } public void Info(String message, Exception exception) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, message, exception); } } public void InfoFormat(String format, params Object[] args) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } } public void InfoFormat(Exception exception, String format, params Object[] args) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } } public void InfoFormat(IFormatProvider formatProvider, String format, params Object[] args) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, new SystemStringFormat(formatProvider, format, args), null); } } public void InfoFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { if (IsInfoEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Info, new SystemStringFormat(formatProvider, format, args), exception); } } public void Warn(String message) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, message, null); } } public void Warn(Func messageFactory) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, messageFactory.Invoke(), null); } } public void Warn(String message, Exception exception) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, message, exception); } } public void WarnFormat(String format, params Object[] args) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } } public void WarnFormat(Exception exception, String format, params Object[] args) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } } public void WarnFormat(IFormatProvider formatProvider, String format, params Object[] args) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, new SystemStringFormat(formatProvider, format, args), null); } } public void WarnFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { if (IsWarnEnabled) { + AddExtendedThreadInfo(); Logger.Log(declaringType, Level.Warn, new SystemStringFormat(formatProvider, format, args), exception); } }