Skip to content

#42 added color option to ConsoleOutLoggerFactoryAdapter #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 53 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,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());
}
}
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