diff --git a/SimpleLog.Tests/ConsoleLogHandlerTest.cs b/SimpleLog.Tests/ConsoleLogHandlerTest.cs
index 8d74744..60e998c 100644
--- a/SimpleLog.Tests/ConsoleLogHandlerTest.cs
+++ b/SimpleLog.Tests/ConsoleLogHandlerTest.cs
@@ -22,12 +22,8 @@ namespace SimpleLog.Tests {
[Test]
public void TestGetOutputStream() {
- Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Warning), Is.SameAs(Console.Error));
- Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Error), Is.SameAs(Console.Error));
- Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Critical), Is.SameAs(Console.Error));
-
- Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Debug), Is.SameAs(Console.Out));
- Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Info), Is.SameAs(Console.Out));
+ Assert.That(this.handler.GetOutputStream(ConsoleLogHandler.STD_ERROR_HANDLE), Is.SameAs(Console.Error));
+ Assert.That(this.handler.GetOutputStream(ConsoleLogHandler.STD_OUTPUT_HANDLE), Is.SameAs(Console.Out));
}
[Test]
diff --git a/SimpleLog/LogHandlers/ConsoleLogHandler.cs b/SimpleLog/LogHandlers/ConsoleLogHandler.cs
index d074a37..bece8ed 100644
--- a/SimpleLog/LogHandlers/ConsoleLogHandler.cs
+++ b/SimpleLog/LogHandlers/ConsoleLogHandler.cs
@@ -6,13 +6,16 @@ namespace SimpleLog.LogHandlers {
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
+ public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
+ public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
+
public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
}
#region ILogHandler Members
- public void GracefulShutDown() {
+ public virtual void GracefulShutDown() {
throw new NotImplementedException();
}
@@ -25,8 +28,9 @@ namespace SimpleLog.LogHandlers {
}
}
- public bool Log(string message, LogLevel level) {
- System.IO.TextWriter outputStream = ConsoleLogHandler.GetOutputStream(level);
+ public virtual bool Log(string message, LogLevel level) {
+ UInt32 streamType = this.GetStreamType(level);
+ System.IO.TextWriter outputStream = (System.IO.TextWriter)this.GetOutputStream(streamType);
outputStream.Write(message);
return true;
}
@@ -35,14 +39,24 @@ namespace SimpleLog.LogHandlers {
/// Gets the output stream for the specified log level
///
/// Console.Out or Console.Error
- public static System.IO.TextWriter GetOutputStream(LogLevel level) {
- if (level >= SimpleLog.LogLevel.Warning) {
+ public virtual System.IO.TextWriter GetOutputStream(UInt32 streamType) {
+ if (streamType == STD_ERROR_HANDLE) {
return Console.Error;
}
return Console.Out;
}
+ public UInt32 GetStreamType(LogLevel level) {
+ if (level >= SimpleLog.LogLevel.Warning) {
+ return STD_ERROR_HANDLE;
+ }
+
+ return STD_OUTPUT_HANDLE;
+ }
+
+
+
public IMessageHandler MessageHandler {
get {
return this.messageHandler;
diff --git a/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs b/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs
new file mode 100644
index 0000000..8171c72
--- /dev/null
+++ b/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
+
+namespace SimpleLog.LogHandlers {
+
+ [Flags]
+ public enum ConsoleColor : int {
+ Blue = 0x0001,
+ Green = 0x0002,
+ Red = 0x0004,
+ White = Blue | Green | Red,
+ Yellow = Red | Green,
+ Purple = Red | Blue,
+ Cyan = Green | Blue,
+ HighIntensity = 0x0008,
+ }
+
+ public class ConsoleWindowLogHandler : ConsoleLogHandler {
+
+ public ConsoleWindowLogHandler() : this("Log Window") {
+
+ }
+
+ public ConsoleWindowLogHandler(string title) : base() {
+ ConsoleWindowLogHandler.AllocConsole();
+ Console.Title = title;
+ }
+
+ protected IntPtr GetConsoleHandle(uint streamType) {
+ return ConsoleWindowLogHandler.GetStdHandle(streamType);
+ }
+
+ protected ConsoleColor GetColorInfo(LogLevel level) {
+ switch (level) {
+ case SimpleLog.LogLevel.Debug:
+ return ConsoleColor.Green;
+ case SimpleLog.LogLevel.Info:
+ return ConsoleColor.White;
+ case SimpleLog.LogLevel.Warning:
+ return ConsoleColor.Yellow;
+ case SimpleLog.LogLevel.Error:
+ return ConsoleColor.Red;
+ case SimpleLog.LogLevel.Critical:
+ return ConsoleColor.Red | ConsoleColor.HighIntensity;
+ default:
+ return ConsoleColor.White;
+ }
+ }
+
+ public override bool Log(string message, LogLevel level) {
+ uint streamType = this.GetStreamType(level);
+ System.IO.TextWriter writer = this.GetOutputStream(streamType);
+
+
+ IntPtr consoleHandle = GetConsoleHandle(streamType);
+
+ ushort colorInfo = (ushort)this.GetColorInfo(level);
+ ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
+
+ writer.Write(message);
+ return true;
+ }
+
+ public new void GracefulShutDown() {
+
+ }
+
+ #region Win32 DLL imports
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ private static extern bool AllocConsole();
+
+
+ [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
+ private static extern int GetConsoleOutputCP();
+
+ [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
+ private static extern IntPtr GetStdHandle(UInt32 type);
+
+ [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
+ private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
+ #endregion
+
+ }
+}
diff --git a/SimpleLog/SimpleLog.csproj b/SimpleLog/SimpleLog.csproj
index ffa4358..ea458c2 100644
--- a/SimpleLog/SimpleLog.csproj
+++ b/SimpleLog/SimpleLog.csproj
@@ -55,6 +55,7 @@
+