Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

#42 added color option to ConsoleOutLoggerFactoryAdapter #43

Merged
merged 2 commits into from

3 participants

@adamralph

fixes #42 using the 'new constructor for ConsoleOutLoggerFactoryAdapter' solution

@adamralph

Hold off merging for now. I've just realised this implementation is flawed. I'll update the PR later.

@adamralph

OK, I've replaced the commit. I think it's all good to go now, although the Sliverlight #if conditionals are quite cumbersome.

An alternative approach would be to introduce a ColoredConsoleOutLoggerFactoryAdapter and a ColoredConsoleOutLogger and add them only to the non-SL assemblies. If you'd rather go down that route then let me know and I can redo the feature in that manner.

src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs
@@ -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);

If the level isn't found, color will become default(ConsoleColor), which is black. Based on the values in colors, I think we're assuming a dark (black?) background, so that's perhaps not ideal. Maybe better to detect the missing value and fall back to originalColor?

While I'm griping, no love for people with light-coloured backgrounds? :wink:

Does TryGetValue() specifically assign default(T) if the key is not found? I guess it might at least. I'll change it to be immune to that.

Light backgrounds - it crossed my mind too but for now I just went with replicating the default behaviour of the NLog ColoredConsoleTarget with regard to the chosen foreground colors as well as not altering the background color. I guess something clever to detect the lightness of the background and switch to a dark foreground scheme could be added later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@blairconrad

Does TryGetValue() specifically assign default(T) if the key is not found?

That's how I read the docs.

And, yup. Look:

C:\PortableApps\Documents\Source>scriptcs
scriptcs (ctrl-c to exit)

> var dict = new Dictionary<string, System.ConsoleColor>();
> var originalColor = System.ConsoleColor.White;
> dict.TryGetValue("ERROR", out originalColor);
false
> originalColor
0
> (int)System.ConsoleColor.Black
0
> (int)System.ConsoleColor.White
15
@blairconrad

I guess something clever to detect the lightness of the background and switch to a dark foreground scheme could be added later.

Yeah. I was just having a little fun with you on that one. Sorry.

@adamralph

Has anyone had a chance to look at this PR? I'm happy to re-visit if required.

@sbohlen
Owner

@adamralph I'll take a look at it this week and let you know -- thanks for the contribution and sorry for the delayed response!

@sbohlen
Owner

Hang tight -- this weekend is "common.logging weekend" for me so I'll get this in shortly (promise). I've been traveling ridiculously-much for work the past ~30 days.

@adamralph

:smiley:

Sure, no probs. I know how it is...

@sbohlen
Owner

Per your comment ...

An alternative approach would be to introduce a ColoredConsoleOutLoggerFactoryAdapter and a ColoredConsoleOutLogger and add them only to the non-SL assemblies.

...could you re-jigger your PR to use this approach? Now that I've restructured the relationships betw the PCL assemblies and the non-PCL assemblies (so that there's no longer any dependency betw. the two) your suggestion here re: the 'cleaner' way to approach this is actually much more appropriate.

Thanks again for the contribution!

@sbohlen
Owner

Actually, scratch that -- I'll actually do this myself since its relatively straightforward to accomplish in short order....

@sbohlen sbohlen merged commit fdef6b8 into net-commons:master
@sbohlen
Owner

Just FYI, since its the Common.Logging.Portable project that forms the basis for the Silverlight support and ConsoleOutLogger only exists in Common.Logging, I was able to remove all of the IFDEFs from the PR before merging it. Simplicity achieved!

@adamralph adamralph deleted the adamralph:colored-console-logging branch
@adamralph

:joy: thanks!

@sbohlen
Owner

Sorry, just realized that I'm wrong -- there's been so much restructuring of the code here lately that I failed to realize that the ConsoleOutLogger does in fact also appear in the Silverlight project so I've added your IFDEFs back in -- so much for simplicty :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
53 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<LogLevel, ConsoleColor> colors = new Dictionary<LogLevel, ConsoleColor>
+ {
+ { 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
/// <summary>
/// Creates and initializes a logger that writes messages to <see cref="Console.Out" />.
/// </summary>
@@ -47,6 +64,24 @@ public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool
{
}
+#if !SILVERLIGHT
+ /// <summary>
+ /// Creates and initializes a logger that writes messages to <see cref="Console.Out" />.
+ /// </summary>
+ /// <param name="logName">The name, usually type name of the calling class, of the logger.</param>
+ /// <param name="logLevel">The current logging threshold. Messages recieved that are beneath this threshold will not be logged.</param>
+ /// <param name="showLevel">Include the current log level in the log message.</param>
+ /// <param name="showDateTime">Include the current time in the log message.</param>
+ /// <param name="showLogName">Include the instance name in the log message.</param>
+ /// <param name="dateTimeFormat">The date and time format to use in the log message.</param>
+ /// <param name="useColor">Use color when writing the log message.</param>
+ 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
/// <summary>
/// Do the actual logging by constructing the log message using a <see cref="StringBuilder" /> then
/// sending the output to <see cref="Console.Out" />.
@@ -61,6 +96,24 @@ protected override void WriteInternal(LogLevel level, object message, Exception
FormatOutput(sb, level, message, e);
// Print to the appropriate destination
+#if !SILVERLIGHT
+ ConsoleColor color;
+ if (this.useColor && colors.TryGetValue(level, out color))
+ {
+ var originalColor = Console.ForegroundColor;
+ try
+ {
+ Console.ForegroundColor = color;
+ Console.Out.WriteLine(sb.ToString());
+ return;
+ }
+ finally
+ {
+ Console.ForegroundColor = originalColor;
+ }
+ }
+
+#endif
Console.Out.WriteLine(sb.ToString());
}
}
View
20 src/Common.Logging/Logging/Simple/ConsoleOutLoggerFactoryAdapter.cs
@@ -60,6 +60,10 @@ namespace Common.Logging.Simple
/// <author>Erich Eichinger</author>
public class ConsoleOutLoggerFactoryAdapter : Simple.AbstractSimpleLoggerFactoryAdapter
{
+#if !SILVERLIGHT
+ private readonly bool useColor;
+
+#endif
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleOutLoggerFactoryAdapter"/> class using default
/// settings.
@@ -103,12 +107,28 @@ public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool sh
: base(level, showDateTime, showLogName, showLevel, dateTimeFormat)
{ }
+#if !SILVERLIGHT
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AbstractSimpleLoggerFactoryAdapter"/> class with
+ /// default settings for the loggers created by this factory.
+ /// </summary>
+ public ConsoleOutLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, string dateTimeFormat, bool useColor)
+ : this(level, showDateTime, showLogName, showLevel, dateTimeFormat)
+ {
+ this.useColor = useColor;
+ }
+
+#endif
/// <summary>
/// Creates a new <see cref="ConsoleOutLogger"/> instance.
/// </summary>
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;
}
}
Something went wrong with that request. Please try again.