From b0b17c4aa1aa674d1a76605096fa44a044cd8cd1 Mon Sep 17 00:00:00 2001 From: Adam Ralph Date: Mon, 3 Mar 2014 19:27:49 +0100 Subject: [PATCH 1/2] #42 added color option to ConsoleOutLoggerFactoryAdapter --- .../Logging/Simple/ConsoleOutLogger.cs | 54 +++++++++++++++++++ .../Simple/ConsoleOutLoggerFactoryAdapter.cs | 20 +++++++ 2 files changed, 74 insertions(+) diff --git a/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs b/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs index 9847083..24e6691 100644 --- a/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs +++ b/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs @@ -19,6 +19,9 @@ #endregion using System; +#if !SILVERLIGHT +using System.Collections.Generic; +#endif using System.Text; namespace Common.Logging.Simple @@ -33,6 +36,20 @@ namespace Common.Logging.Simple #endif public class ConsoleOutLogger : Simple.AbstractSimpleLogger { +#if !SILVERLIGHT + private static readonly Dictionary colors = new Dictionary + { + { LogLevel.Fatal, ConsoleColor.Red }, + { LogLevel.Error, ConsoleColor.Yellow }, + { LogLevel.Warn, ConsoleColor.Magenta }, + { LogLevel.Info, ConsoleColor.White }, + { LogLevel.Debug, ConsoleColor.Gray }, + { LogLevel.Trace, ConsoleColor.DarkGray }, + }; + + private readonly bool useColor; + +#endif /// /// Creates and initializes a logger that writes messages to . /// @@ -47,6 +64,24 @@ public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool { } +#if !SILVERLIGHT + /// + /// Creates and initializes a logger that writes messages to . + /// + /// The name, usually type name of the calling class, of the logger. + /// The current logging threshold. Messages recieved that are beneath this threshold will not be logged. + /// Include the current log level in the log message. + /// Include the current time in the log message. + /// Include the instance name in the log message. + /// The date and time format to use in the log message. + /// Use color when writing the log message. + public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat, bool useColor) + : this(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat) + { + this.useColor = useColor; + } + +#endif /// /// Do the actual logging by constructing the log message using a then /// sending the output to . @@ -61,6 +96,25 @@ protected override void WriteInternal(LogLevel level, object message, Exception FormatOutput(sb, level, message, e); // Print to the appropriate destination +#if !SILVERLIGHT + if (this.useColor) + { + var originalColor = Console.ForegroundColor; + ConsoleColor color = originalColor; + colors.TryGetValue(level, out color); + try + { + Console.ForegroundColor = color; + Console.Out.WriteLine(sb.ToString()); + return; + } + finally + { + Console.ForegroundColor = originalColor; + } + } + +#endif Console.Out.WriteLine(sb.ToString()); } } diff --git a/src/Common.Logging/Logging/Simple/ConsoleOutLoggerFactoryAdapter.cs b/src/Common.Logging/Logging/Simple/ConsoleOutLoggerFactoryAdapter.cs index 86aa977..4a52c28 100644 --- a/src/Common.Logging/Logging/Simple/ConsoleOutLoggerFactoryAdapter.cs +++ b/src/Common.Logging/Logging/Simple/ConsoleOutLoggerFactoryAdapter.cs @@ -60,6 +60,10 @@ namespace Common.Logging.Simple /// Erich Eichinger public class ConsoleOutLoggerFactoryAdapter : Simple.AbstractSimpleLoggerFactoryAdapter { +#if !SILVERLIGHT + private readonly bool useColor; + +#endif /// /// Initializes a new instance of the class using default /// settings. @@ -103,12 +107,28 @@ public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool sh : base(level, showDateTime, showLogName, showLevel, dateTimeFormat) { } +#if !SILVERLIGHT + /// + /// Initializes a new instance of the class with + /// default settings for the loggers created by this factory. + /// + public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, string dateTimeFormat, bool useColor) + : this(level, showDateTime, showLogName, showLevel, dateTimeFormat) + { + this.useColor = useColor; + } + +#endif /// /// Creates a new instance. /// protected override ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat) { +#if !SILVERLIGHT + ILog log = new ConsoleOutLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat, this.useColor); +#else ILog log = new ConsoleOutLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat); +#endif return log; } } From fdef6b874fd24778de7c338aa522f38bd8489906 Mon Sep 17 00:00:00 2001 From: Adam Ralph Date: Tue, 4 Mar 2014 10:35:26 +0100 Subject: [PATCH 2/2] #42 fixed color look up and skipped setting of colors if level unrecognized --- src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs b/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs index 24e6691..04ab1c5 100644 --- a/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs +++ b/src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs @@ -97,11 +97,10 @@ protected override void WriteInternal(LogLevel level, object message, Exception // Print to the appropriate destination #if !SILVERLIGHT - if (this.useColor) + ConsoleColor color; + if (this.useColor && colors.TryGetValue(level, out color)) { var originalColor = Console.ForegroundColor; - ConsoleColor color = originalColor; - colors.TryGetValue(level, out color); try { Console.ForegroundColor = color;