#19401: Adding tenant specific log information

Work Item: 19401

Signed-off-by: Sebastien Ros <sebastien.ros@microsoft.com>
This commit is contained in:
Jeff Olmstead
2013-11-06 12:37:52 -05:00
committed by Sebastien Ros
parent ee1bcb497b
commit 0c124207eb
2 changed files with 87 additions and 3 deletions

View File

@@ -61,7 +61,7 @@
<!-- Prevents Orchard.exe from displaying locking debug messages. -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %logger - %message%newline" />
<conversionPattern value="%date [%thread] %logger - %P{Tenant} - %message%newline" />
</layout>
</appender>
@@ -81,7 +81,7 @@
<levelMin value="ERROR" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %logger - %message%newline" />
<conversionPattern value="%date [%thread] %logger - %P{Tenant} - %message%newline %P{Url}%newline" />
</layout>
</appender>

View File

@@ -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<IRunningShellTable>();
if (runningShellTable == null)
return null;
var shellSettings = runningShellTable.Match(new HttpContextWrapper(ctx));
if (shellSettings == null)
return null;
var orchardHost = HostContainer.Resolve<IOrchardHost>();
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<string> 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<string> 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<string> 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<string> 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<string> 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);
}
}