Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
adding unit tests for the console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sonjakhan committed Oct 10, 2014
1 parent 9c35a51 commit e3cf807
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 15 deletions.
29 changes: 16 additions & 13 deletions src/Microsoft.Framework.Logging.Console/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public class ConsoleLogger : ILogger
public ConsoleLogger(string name, Func<string, TraceType, bool> filter)
{
_name = name;
_filter = filter;
_filter = filter ?? ((category, traceType) => true);
Console = new LogConsole();
}

public IConsole Console { get; set; }

public bool WriteCore(TraceType traceType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
if (!_filter(_name, traceType))
Expand All @@ -43,43 +46,43 @@ public bool WriteCore(TraceType traceType, int eventId, object state, Exception
{
return false;
}
var foregroundColor = System.Console.ForegroundColor; // save current colors
var backgroundColor = System.Console.BackgroundColor;
var foregroundColor = Console.ForegroundColor; // save current colors
var backgroundColor = Console.BackgroundColor;
var severity = traceType.ToString().ToUpperInvariant();
SetConsoleColor(traceType);
try
{
System.Console.WriteLine("[{0}:{1}] {2}", severity, _name, message);
Console.WriteLine("[{0}:{1}] {2}", severity, _name, message);
}
finally
{
System.Console.ForegroundColor = foregroundColor; // reset initial colors
System.Console.BackgroundColor = backgroundColor;
Console.ForegroundColor = foregroundColor; // reset initial colors
Console.BackgroundColor = backgroundColor;
}
return true;
}

// sets the console text color to reflect the given TraceType
private static void SetConsoleColor(TraceType traceType)
private void SetConsoleColor(TraceType traceType)
{
switch (traceType)
{
case TraceType.Critical:
System.Console.BackgroundColor = ConsoleColor.Red;
System.Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
break;
case TraceType.Error:
System.Console.ForegroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Red;
break;
case TraceType.Warning:
System.Console.ForegroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case TraceType.Information:
System.Console.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.White;
break;
case TraceType.Verbose:
default:
System.Console.ForegroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Gray;
break;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.Framework.Logging.Console/IConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Microsoft.Framework.Logging.Console
{
public interface IConsole
{
ConsoleColor BackgroundColor { get; set; }
ConsoleColor ForegroundColor { get; set; }
void WriteLine(string format, object arg0, object arg1, object arg2);
}
}
38 changes: 38 additions & 0 deletions src/Microsoft.Framework.Logging.Console/LogConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace Microsoft.Framework.Logging.Console
{
public class LogConsole : IConsole
{
public ConsoleColor BackgroundColor
{
get
{
return System.Console.BackgroundColor;
}

set
{
System.Console.BackgroundColor = value;
}
}

public ConsoleColor ForegroundColor
{
get
{
return System.Console.ForegroundColor;
}

set
{
System.Console.ForegroundColor = value;
}
}

public void WriteLine(string format, object arg0, object arg1, object arg2)
{
System.Console.WriteLine(format, arg0, arg1, arg2);
}
}
}
2 changes: 0 additions & 2 deletions src/Microsoft.Framework.Logging.Console/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"configurations": {
"net45": { },
"aspnet50": {
"dependencies": {
}
},
"aspnetcore50": {
"dependencies": {
Expand Down
13 changes: 13 additions & 0 deletions test/Microsoft.Framework.Logging.Test/Console/ConsoleContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Microsoft.Framework.Logging.Test.Console
{
public class ConsoleContext
{
public ConsoleColor BackgroundColor { get; set; }

public ConsoleColor ForegroundColor { get; set; }

public string Message { get; set; }
}
}
20 changes: 20 additions & 0 deletions test/Microsoft.Framework.Logging.Test/Console/ConsoleSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;

namespace Microsoft.Framework.Logging.Test.Console
{
public class ConsoleSink
{
public ConsoleSink()
{
Writes = new List<ConsoleContext>();
}

public List<ConsoleContext> Writes { get; set; }

public void Write(ConsoleContext context)
{
Writes.Add(context);
}
}
}
54 changes: 54 additions & 0 deletions test/Microsoft.Framework.Logging.Test/Console/TestConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Microsoft.Framework.Logging.Console;

namespace Microsoft.Framework.Logging.Test.Console
{
public class TestConsole : IConsole
{
private ConsoleColor _backgroundColor;
private ConsoleColor _foregroundColor;
private ConsoleSink _sink;

public TestConsole(ConsoleSink sink)
{
_sink = sink;
}

public ConsoleColor BackgroundColor
{
get
{
return _backgroundColor;
}

set
{
_backgroundColor = value;
}
}

public ConsoleColor ForegroundColor
{
get
{
return _foregroundColor;
}

set
{
_foregroundColor = value;
}
}

public void WriteLine(string format, object arg0, object arg1, object arg2)
{
var message = string.Format(format, arg0, arg1, arg2);
_sink.Write(new ConsoleContext()
{
ForegroundColor = _foregroundColor,
BackgroundColor = _backgroundColor,
Message = message
});
}
}
}
Loading

0 comments on commit e3cf807

Please sign in to comment.