Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Common.Logging/Logging/Simple/ConsoleOutLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#endregion

using System;
#if !SILVERLIGHT
using System.Collections.Generic;
#endif
using System.Text;

namespace Common.Logging.Simple
Expand All @@ -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>
Expand All @@ -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" />.
Expand All @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

try
{
Console.ForegroundColor = color;
Console.Out.WriteLine(sb.ToString());
return;
}
finally
{
Console.ForegroundColor = originalColor;
}
}

#endif
Console.Out.WriteLine(sb.ToString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
Expand Down