Skip to content

Commit

Permalink
(x) #1225 CatelLog.AlwaysLog is not being respected in LogExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Oct 18, 2018
1 parent 79b96d0 commit cf14410
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 92 deletions.
1 change: 1 addition & 0 deletions doc/history.txt
Expand Up @@ -47,6 +47,7 @@ Added/fixed
(+) #1224 Add HasWarningsOrErrors as extension method for IValidationContext
(x) #1218 UIVisualizerService is not setting parent window correctly
(x) #1220 DispatcherExtensions.BeginInvoke should never return null as task
(x) #1225 CatelLog.AlwaysLog is not being respected in LogExtensions

Roadmap
=======
Expand Down
49 changes: 33 additions & 16 deletions src/Catel.Core/Logging/Extensions/LogExtensions.cs
Expand Up @@ -7,9 +7,6 @@
namespace Catel.Logging
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Reflection;

/// <summary>
Expand Down Expand Up @@ -48,16 +45,16 @@ public static void LogProductInfo(this ILog log)
var assembly = AssemblyHelper.GetEntryAssembly();
Write(log, LogEvent.Info, "Assembly: {0}", assembly.Title());
Write(log, LogEvent.Info, "Version: {0}", assembly.Version());
try
{
Write(log, LogEvent.Info, "Informational version: {0}", assembly.InformationalVersion());
}
catch (Exception)
{

try
{
Write(log, LogEvent.Info, "Informational version: {0}", assembly.InformationalVersion());
}
catch (Exception)
{
// Ignore
}
}

Write(log, LogEvent.Info, string.Empty);
Write(log, LogEvent.Info, "Company: {0}", assembly.Company());
Write(log, LogEvent.Info, "Copyright: {0}", assembly.Copyright());
Expand Down Expand Up @@ -103,6 +100,26 @@ public static void LogDeviceInfo(this ILog log)
Write(log, LogEvent.Info, string.Empty);
}

/// <summary>
/// Determines whether the log is Catel logging and can be ignored if Catel logging is disabled.
/// </summary>
/// <param name="log">The log.</param>
/// <returns><c>true</c> if the logging is Catel logging *and* can be ignored.</returns>
public static bool IsCatelLoggingAndCanBeIgnored(this ILog log)
{
if (!log.IsCatelLogging)
{
return false;
}

var catelLog = log as CatelLog;
if (catelLog != null && catelLog.AlwaysLog)
{
return false;
}

return true;
}

/// <summary>
/// Writes the specified message as the specified log event.
Expand Down Expand Up @@ -209,7 +226,7 @@ public static void Write(this ILog log, LogEvent logEvent, string messageFormat,
{
log?.WriteWithData(string.Format(messageFormat, s1, s2, s3, s4, s5), null, logEvent);
}
}
}

/// <summary>
/// Writes the specified message as the specified log event.
Expand Down Expand Up @@ -287,7 +304,7 @@ public static void WriteWithData(this ILog log, Exception exception, string mess
return;
}

if (LogManager.LogInfo.IgnoreCatelLogging && log.IsCatelLogging)
if (LogManager.LogInfo.IgnoreCatelLogging && log.IsCatelLoggingAndCanBeIgnored())
{
return;
}
Expand Down Expand Up @@ -386,7 +403,7 @@ public static Exception ErrorAndCreateException<TException>(this ILog log, Excep
var exception = ExceptionFactory.CreateException<TException>(msg, innerException);
if (exception == null)
{
var error = $"Exception type '{typeof (TException).Name}' does not have a constructor accepting a string";
var error = $"Exception type '{typeof(TException).Name}' does not have a constructor accepting a string";
if (log != null)
{
Expand Down Expand Up @@ -445,7 +462,7 @@ public static Exception ErrorAndCreateException<TException>(this ILog log, Excep
var exception = createExceptionCallback(message);
if (exception == null)
{
var error = $"Exception type '{typeof (TException).Name}' does not have a constructor accepting a string";
var error = $"Exception type '{typeof(TException).Name}' does not have a constructor accepting a string";

if (log != null)
{
Expand Down
17 changes: 14 additions & 3 deletions src/Catel.Core/Logging/Log.cs
Expand Up @@ -17,6 +17,7 @@ public class Log : ILog
#region Fields
private int _indentSize = 2;
private int _indentLevel = 0;
private readonly Lazy<bool> _shouldIgnoreIfCatelLoggingIsDisabled;
#endregion

#region Constructors
Expand Down Expand Up @@ -54,6 +55,7 @@ public Log(string name, Type targetType)
TargetType = targetType;

IsCatelLogging = targetType?.IsCatelType() ?? false;
_shouldIgnoreIfCatelLoggingIsDisabled = new Lazy<bool>(ShouldIgnoreIfCatelLoggingIsDisabled);
}
#endregion

Expand Down Expand Up @@ -151,7 +153,7 @@ public void WriteWithData(string message, object extraData, LogEvent logEvent)
return;
}

if (LogManager.LogInfo.IgnoreCatelLogging && IsCatelLogging)
if (LogManager.LogInfo.IgnoreCatelLogging && _shouldIgnoreIfCatelLoggingIsDisabled.Value)
{
return;
}
Expand All @@ -172,14 +174,23 @@ public void WriteWithData(string message, LogData logData, LogEvent logEvent)
return;
}

if (LogManager.LogInfo.IgnoreCatelLogging && IsCatelLogging)
if (LogManager.LogInfo.IgnoreCatelLogging && _shouldIgnoreIfCatelLoggingIsDisabled.Value)
{
return;
}

WriteMessage(message, null, logData, logEvent);
}

/// <summary>
/// Retuns a value whether this should be ignored if it as Catel logging.
/// </summary>
/// <returns><c>true</c> if this log should be ignored if Catel logging is </returns>
protected virtual bool ShouldIgnoreIfCatelLoggingIsDisabled()
{
return this.IsCatelLoggingAndCanBeIgnored();
}

/// <summary>
/// Raises the <see cref="LogMessage" /> event.
/// </summary>
Expand Down Expand Up @@ -218,4 +229,4 @@ public void Unindent()
}
#endregion
}
}
}
99 changes: 27 additions & 72 deletions src/Catel.Core/Logging/LogListenerBase.cs
Expand Up @@ -162,19 +162,9 @@ public TimeDisplay TimeDisplay
/// <param name="time">The time.</param>
void ILogListener.Write(ILog log, string message, LogEvent logEvent, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
if (ShouldIgnoreLogging(log, message, logEvent, extraData, logData, time))
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, logEvent, extraData, logData, time))
{
return;
}
return;
}

Write(log, message, logEvent, extraData, logData, time);
Expand All @@ -192,19 +182,9 @@ void ILogListener.Write(ILog log, string message, LogEvent logEvent, object extr
/// <param name="time">The time.</param>
void ILogListener.Debug(ILog log, string message, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
if (ShouldIgnoreLogging(log, message, LogEvent.Debug, extraData, logData, time))
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, LogEvent.Debug, extraData, logData, time))
{
return;
}
return;
}

Debug(log, message, extraData, logData, time);
Expand All @@ -220,19 +200,9 @@ void ILogListener.Debug(ILog log, string message, object extraData, LogData logD
/// <param name="time">The time.</param>
void ILogListener.Info(ILog log, string message, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
if (ShouldIgnoreLogging(log, message, LogEvent.Info, extraData, logData, time))
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, LogEvent.Info, extraData, logData, time))
{
return;
}
return;
}

Info(log, message, extraData, logData, time);
Expand All @@ -248,19 +218,9 @@ void ILogListener.Info(ILog log, string message, object extraData, LogData logDa
/// <param name="time">The time.</param>
void ILogListener.Warning(ILog log, string message, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
if (ShouldIgnoreLogging(log, message, LogEvent.Warning, extraData, logData, time))
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, LogEvent.Warning, extraData, logData, time))
{
return;
}
return;
}

Warning(log, message, extraData, logData, time);
Expand All @@ -276,19 +236,9 @@ void ILogListener.Warning(ILog log, string message, object extraData, LogData lo
/// <param name="time">The ti me.</param>
void ILogListener.Error(ILog log, string message, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
if (ShouldIgnoreLogging(log, message, LogEvent.Error, extraData, logData, time))
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, LogEvent.Error, extraData, logData, time))
{
return;
}
return;
}

Error(log, message, extraData, logData, time);
Expand All @@ -304,26 +254,31 @@ void ILogListener.Error(ILog log, string message, object extraData, LogData logD
/// <param name="time">The ti me.</param>
void ILogListener.Status(ILog log, string message, object extraData, LogData logData, DateTime time)
{
// If the log is a catel log and the AlwaysLog flag is set, skip additional checks and log the message.
var catelLog = log as ICatelLog;
if (catelLog == null || !catelLog.AlwaysLog)
{
if (IgnoreCatelLogging && log.IsCatelLogging)
{
return;
}

if (ShouldIgnoreLogMessage(log, message, LogEvent.Status, extraData, logData, time))
if (ShouldIgnoreLogging(log, message, LogEvent.Status, extraData, logData, time))
{
return;
}
}

Status(log, message, extraData, logData, time);
}
#endregion

#region Methods
private bool ShouldIgnoreLogging(ILog log, string message, LogEvent logEvent, object extraData, LogData logData, DateTime time)
{
if (IgnoreCatelLogging && log.IsCatelLoggingAndCanBeIgnored())
{
return true;
}

if (ShouldIgnoreLogMessage(log, message, logEvent, extraData, logData, time))
{
return true;
}

return false;
}

/// <summary>
/// Returns whether the log message should be ignored
/// </summary>
Expand Down Expand Up @@ -453,4 +408,4 @@ protected virtual void Status(ILog log, string message, object extraData, LogDat
}
#endregion
}
}
}
2 changes: 1 addition & 1 deletion src/Catel.Core/Logging/LogManager.cs
Expand Up @@ -767,7 +767,7 @@ private static List<ILogListener> GetThreadSafeLogListeners()
/// <exception cref="ArgumentOutOfRangeException">The <see cref="LogEvent"/> is not supported.</exception>
private static void OnLogMessage(object sender, LogMessageEventArgs e)
{
if (LogInfo.IgnoreCatelLogging && e.Log.IsCatelLogging)
if (LogInfo.IgnoreCatelLogging && e.Log.IsCatelLoggingAndCanBeIgnored())
{
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Catel.Tests/Catel.Core.approved.cs
Expand Up @@ -2556,6 +2556,7 @@ public class Log : Catel.Logging.ILog
public System.Type TargetType { get; }
public event System.EventHandler<Catel.Logging.LogMessageEventArgs> LogMessage;
public void Indent() { }
protected virtual bool ShouldIgnoreIfCatelLoggingIsDisabled() { }
public void Unindent() { }
public void WriteWithData(string message, object extraData, Catel.Logging.LogEvent logEvent) { }
public void WriteWithData(string message, Catel.Logging.LogData logData, Catel.Logging.LogEvent logEvent) { }
Expand Down Expand Up @@ -2637,6 +2638,7 @@ public static System.Exception ErrorAndCreateException<TException>(this Catel.Lo
public static void InfoWithData(this Catel.Logging.ILog log, string message, object extraData = null) { }
public static void InfoWithData(this Catel.Logging.ILog log, string message, Catel.Logging.LogData logData) { }
public static void InfoWithData(this Catel.Logging.ILog log, System.Exception exception, string message, object extraData = null) { }
public static bool IsCatelLoggingAndCanBeIgnored(this Catel.Logging.ILog log) { }
public static void LogDebugHeading(this Catel.Logging.ILog log, string headingContent, string messageFormat, params object[] args) { }
public static void LogDebugHeading1(this Catel.Logging.ILog log, string messageFormat, params object[] args) { }
public static void LogDebugHeading2(this Catel.Logging.ILog log, string messageFormat, params object[] args) { }
Expand Down

0 comments on commit cf14410

Please sign in to comment.