diff --git a/doc/history.txt b/doc/history.txt index 9aa593a5d..8ad0af42d 100644 --- a/doc/history.txt +++ b/doc/history.txt @@ -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 ======= diff --git a/src/Catel.Core/Logging/Extensions/LogExtensions.cs b/src/Catel.Core/Logging/Extensions/LogExtensions.cs index 3f7f8cd19..6ba9c63ae 100644 --- a/src/Catel.Core/Logging/Extensions/LogExtensions.cs +++ b/src/Catel.Core/Logging/Extensions/LogExtensions.cs @@ -7,9 +7,6 @@ namespace Catel.Logging { using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; using Reflection; /// @@ -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()); @@ -103,6 +100,26 @@ public static void LogDeviceInfo(this ILog log) Write(log, LogEvent.Info, string.Empty); } + /// + /// Determines whether the log is Catel logging and can be ignored if Catel logging is disabled. + /// + /// The log. + /// true if the logging is Catel logging *and* can be ignored. + 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; + } /// /// Writes the specified message as the specified log event. @@ -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); } - } + } /// /// Writes the specified message as the specified log event. @@ -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; } @@ -386,7 +403,7 @@ public static Exception ErrorAndCreateException(this ILog log, Excep var exception = ExceptionFactory.CreateException(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) { @@ -445,7 +462,7 @@ public static Exception ErrorAndCreateException(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) { diff --git a/src/Catel.Core/Logging/Log.cs b/src/Catel.Core/Logging/Log.cs index 3f380f879..b4247e4cb 100644 --- a/src/Catel.Core/Logging/Log.cs +++ b/src/Catel.Core/Logging/Log.cs @@ -17,6 +17,7 @@ public class Log : ILog #region Fields private int _indentSize = 2; private int _indentLevel = 0; + private readonly Lazy _shouldIgnoreIfCatelLoggingIsDisabled; #endregion #region Constructors @@ -54,6 +55,7 @@ public Log(string name, Type targetType) TargetType = targetType; IsCatelLogging = targetType?.IsCatelType() ?? false; + _shouldIgnoreIfCatelLoggingIsDisabled = new Lazy(ShouldIgnoreIfCatelLoggingIsDisabled); } #endregion @@ -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; } @@ -172,7 +174,7 @@ public void WriteWithData(string message, LogData logData, LogEvent logEvent) return; } - if (LogManager.LogInfo.IgnoreCatelLogging && IsCatelLogging) + if (LogManager.LogInfo.IgnoreCatelLogging && _shouldIgnoreIfCatelLoggingIsDisabled.Value) { return; } @@ -180,6 +182,15 @@ public void WriteWithData(string message, LogData logData, LogEvent logEvent) WriteMessage(message, null, logData, logEvent); } + /// + /// Retuns a value whether this should be ignored if it as Catel logging. + /// + /// true if this log should be ignored if Catel logging is + protected virtual bool ShouldIgnoreIfCatelLoggingIsDisabled() + { + return this.IsCatelLoggingAndCanBeIgnored(); + } + /// /// Raises the event. /// @@ -218,4 +229,4 @@ public void Unindent() } #endregion } -} \ No newline at end of file +} diff --git a/src/Catel.Core/Logging/LogListenerBase.cs b/src/Catel.Core/Logging/LogListenerBase.cs index 6b0354ee7..3fa5fd64f 100644 --- a/src/Catel.Core/Logging/LogListenerBase.cs +++ b/src/Catel.Core/Logging/LogListenerBase.cs @@ -162,19 +162,9 @@ public TimeDisplay TimeDisplay /// The time. 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); @@ -192,19 +182,9 @@ void ILogListener.Write(ILog log, string message, LogEvent logEvent, object extr /// The time. 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); @@ -220,19 +200,9 @@ void ILogListener.Debug(ILog log, string message, object extraData, LogData logD /// The time. 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); @@ -248,19 +218,9 @@ void ILogListener.Info(ILog log, string message, object extraData, LogData logDa /// The time. 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); @@ -276,19 +236,9 @@ void ILogListener.Warning(ILog log, string message, object extraData, LogData lo /// The ti me. 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); @@ -304,26 +254,31 @@ void ILogListener.Error(ILog log, string message, object extraData, LogData logD /// The ti me. 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; + } + /// /// Returns whether the log message should be ignored /// @@ -453,4 +408,4 @@ protected virtual void Status(ILog log, string message, object extraData, LogDat } #endregion } -} \ No newline at end of file +} diff --git a/src/Catel.Core/Logging/LogManager.cs b/src/Catel.Core/Logging/LogManager.cs index 6c2a53fe0..8ed9dc226 100644 --- a/src/Catel.Core/Logging/LogManager.cs +++ b/src/Catel.Core/Logging/LogManager.cs @@ -767,7 +767,7 @@ private static List GetThreadSafeLogListeners() /// The is not supported. private static void OnLogMessage(object sender, LogMessageEventArgs e) { - if (LogInfo.IgnoreCatelLogging && e.Log.IsCatelLogging) + if (LogInfo.IgnoreCatelLogging && e.Log.IsCatelLoggingAndCanBeIgnored()) { return; } diff --git a/src/Catel.Tests/Catel.Core.approved.cs b/src/Catel.Tests/Catel.Core.approved.cs index 873615bbd..502009eed 100644 --- a/src/Catel.Tests/Catel.Core.approved.cs +++ b/src/Catel.Tests/Catel.Core.approved.cs @@ -2556,6 +2556,7 @@ public class Log : Catel.Logging.ILog public System.Type TargetType { get; } public event System.EventHandler 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) { } @@ -2637,6 +2638,7 @@ public static System.Exception ErrorAndCreateException(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) { }