Skip to content

Commit

Permalink
(GH-1377) Fix: logger code location is wrong
Browse files Browse the repository at this point in the history
When attempting to change up the logging to output the location of the
log request from the source code, it is providing the values in the
Log4NetLogger and not the original logging location. Fix that to point
to the correct location in most instances.
  • Loading branch information
ferventcoder committed Aug 6, 2017
1 parent 6b199c0 commit 64cc462
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/chocolatey/ILogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace chocolatey
{
using System;
using System.Runtime;
using infrastructure.logging;

// ReSharper disable InconsistentNaming
Expand All @@ -31,16 +32,19 @@ public static class ILogExtensions
/// </summary>
public static bool LogTraceMessages = true;

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Trace(this ILog logger, string message, params object[] formatting)
{
if (LogTraceMessages) ChocolateyLoggers.Trace.to_string().Log().Debug(message, formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Trace(this ILog logger, Func<string> message)
{
if (LogTraceMessages) ChocolateyLoggers.Trace.to_string().Log().Debug(message);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Debug(this ILog logger, ChocolateyLoggers logType, string message, params object[] formatting)
{
switch (logType)
Expand All @@ -58,6 +62,7 @@ public static void Debug(this ILog logger, ChocolateyLoggers logType, string mes
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Debug(this ILog logger, ChocolateyLoggers logType, Func<string> message)
{
switch (logType)
Expand All @@ -74,6 +79,7 @@ public static void Debug(this ILog logger, ChocolateyLoggers logType, Func<strin
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Info(this ILog logger, ChocolateyLoggers logType, string message, params object[] formatting)
{
switch (logType)
Expand All @@ -90,6 +96,7 @@ public static void Info(this ILog logger, ChocolateyLoggers logType, string mess
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Info(this ILog logger, ChocolateyLoggers logType, Func<string> message)
{
switch (logType)
Expand All @@ -106,6 +113,7 @@ public static void Info(this ILog logger, ChocolateyLoggers logType, Func<string
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Warn(this ILog logger, ChocolateyLoggers logType, string message, params object[] formatting)
{
switch (logType)
Expand All @@ -122,6 +130,7 @@ public static void Warn(this ILog logger, ChocolateyLoggers logType, string mess
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Warn(this ILog logger, ChocolateyLoggers logType, Func<string> message)
{
switch (logType)
Expand All @@ -138,6 +147,7 @@ public static void Warn(this ILog logger, ChocolateyLoggers logType, Func<string
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Error(this ILog logger, ChocolateyLoggers logType, string message, params object[] formatting)
{
switch (logType)
Expand All @@ -154,6 +164,7 @@ public static void Error(this ILog logger, ChocolateyLoggers logType, string mes
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Error(this ILog logger, ChocolateyLoggers logType, Func<string> message)
{
switch (logType)
Expand All @@ -170,6 +181,7 @@ public static void Error(this ILog logger, ChocolateyLoggers logType, Func<strin
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Fatal(this ILog logger, ChocolateyLoggers logType, string message, params object[] formatting)
{
switch (logType)
Expand All @@ -186,6 +198,7 @@ public static void Fatal(this ILog logger, ChocolateyLoggers logType, string mes
}
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static void Fatal(this ILog logger, ChocolateyLoggers logType, Func<string> message)
{
switch (logType)
Expand Down
43 changes: 33 additions & 10 deletions src/chocolatey/infrastructure/logging/Log4NetLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
namespace chocolatey.infrastructure.logging
{
using System;
using System.Runtime;
using log4net;
using log4net.Core;

// ReSharper disable InconsistentNaming

Expand All @@ -31,70 +33,91 @@ namespace chocolatey.infrastructure.logging
public sealed class Log4NetLog : ILog, ILog<Log4NetLog>
{
private log4net.ILog _logger;
// ignore Log4NetLog in the call stack
private static readonly Type _declaringType = typeof(Log4NetLog);

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void InitializeFor(string loggerName)
{
_logger = LogManager.GetLogger(loggerName);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Debug(string message, params object[] formatting)
{
if (_logger.IsDebugEnabled) _logger.DebugFormat(decorate_message_with_audit_information(message), formatting);
if (_logger.IsDebugEnabled) Log(Level.Debug, decorate_message_with_audit_information(message), formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Debug(Func<string> message)
{
if (_logger.IsDebugEnabled) _logger.DebugFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
if (_logger.IsDebugEnabled) Log(Level.Debug, decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Info(string message, params object[] formatting)
{
if (_logger.IsInfoEnabled) _logger.InfoFormat(decorate_message_with_audit_information(message), formatting);
if (_logger.IsInfoEnabled) Log(Level.Info, decorate_message_with_audit_information(message), formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Info(Func<string> message)
{
if (_logger.IsInfoEnabled) _logger.InfoFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
if (_logger.IsInfoEnabled) Log(Level.Info, decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Warn(string message, params object[] formatting)
{
if (_logger.IsWarnEnabled) _logger.WarnFormat(decorate_message_with_audit_information(message), formatting);
if (_logger.IsWarnEnabled) Log(Level.Warn, decorate_message_with_audit_information(message), formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Warn(Func<string> message)
{
if (_logger.IsWarnEnabled) _logger.WarnFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
if (_logger.IsWarnEnabled) Log(Level.Warn, decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Error(string message, params object[] formatting)
{
// don't need to check for enabled at this level
_logger.ErrorFormat(decorate_message_with_audit_information(message), formatting);
Log(Level.Error, decorate_message_with_audit_information(message), formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Error(Func<string> message)
{
// don't need to check for enabled at this level
_logger.ErrorFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
Log(Level.Error, decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Fatal(string message, params object[] formatting)
{
// don't need to check for enabled at this level
_logger.FatalFormat(decorate_message_with_audit_information(message), formatting);
Log(Level.Fatal, decorate_message_with_audit_information(message), formatting);
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Fatal(Func<string> message)
{
// don't need to check for enabled at this level
_logger.FatalFormat(decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
Log(Level.Fatal, decorate_message_with_audit_information(message.Invoke()).escape_curly_braces());
}

public string decorate_message_with_audit_information(string message)
{
return message;
}

[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
private void Log(Level level, string message, params object[] formatting)
{
// SystemStringFormat is used to evaluate the message as late as possible. A filter may discard this message.
_logger.Logger.Log(_declaringType, level, message.format_with(formatting), null);
}

}

// ReSharper restore InconsistentNaming
Expand Down

0 comments on commit 64cc462

Please sign in to comment.