diff --git a/SimpleLog/LogHandlers/ConsoleLogHandler.cs b/SimpleLog/LogHandlers/ConsoleLogHandler.cs
index bece8ed..5141150 100644
--- a/SimpleLog/LogHandlers/ConsoleLogHandler.cs
+++ b/SimpleLog/LogHandlers/ConsoleLogHandler.cs
@@ -1,14 +1,23 @@
using System;
namespace SimpleLog.LogHandlers {
+
+ ///
+ /// Log handler for console output (e.g. use this instead of Console.WriteLine()).
+ ///
public class ConsoleLogHandler : ILogHandler {
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
+ //stolen from log4net
public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
+ ///
+ /// Constructs a new ConsoleLogHandler with a default
+ /// message handler
+ ///
public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
@@ -16,9 +25,12 @@ namespace SimpleLog.LogHandlers {
#region ILogHandler Members
public virtual void GracefulShutDown() {
- throw new NotImplementedException();
+ //does nothing
}
+ ///
+ /// Gets or sets the log level for this handler
+ ///
public LogLevel? LogLevel {
get {
return this.logLevel;
@@ -28,35 +40,21 @@ namespace SimpleLog.LogHandlers {
}
}
+ ///
+ /// Logs a message to the console
+ ///
+ /// The message to log
+ /// The log level of the message
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);
+ uint streamType = this.GetStreamType(level);
+ System.IO.TextWriter writer = (System.IO.TextWriter)this.GetOutputStream(streamType);
+ writer.Write(message);
return true;
}
///
- /// Gets the output stream for the specified log level
+ /// Gets or sets the message handler for this log handler
///
- /// Console.Out or Console.Error
- 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;
@@ -66,5 +64,35 @@ namespace SimpleLog.LogHandlers {
}
}
#endregion
+
+ ///
+ /// Gets the output stream for the specified log level
+ ///
+ /// Console.Out or Console.Error
+ public System.IO.TextWriter GetOutputStream(uint streamType) {
+ if (streamType == ConsoleLogHandler.STD_ERROR_HANDLE) {
+ return Console.Error;
+ }
+
+ return Console.Out;
+ }
+
+ ///
+ /// Gets the output stream type (stderr or stdout) based on the
+ /// log level.
+ ///
+ /// Override to change the stderr settings.
+ ///
+ /// The log level
+ /// One of the STD_* constants
+ protected virtual uint GetStreamType(LogLevel level) {
+ if (level >= SimpleLog.LogLevel.Warning) {
+ return STD_ERROR_HANDLE;
+ }
+
+ return STD_OUTPUT_HANDLE;
+ }
+
+
}
}
diff --git a/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs b/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs
index 8171c72..907e3fa 100644
--- a/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs
+++ b/SimpleLog/LogHandlers/ConsoleWindowLogHandler.cs
@@ -4,6 +4,9 @@ using System.Diagnostics;
namespace SimpleLog.LogHandlers {
+ ///
+ /// Colors for console output. Stolen from log4net.ColoredConsoleAppender (verbatim).
+ ///
[Flags]
public enum ConsoleColor : int {
Blue = 0x0001,
@@ -16,68 +19,114 @@ namespace SimpleLog.LogHandlers {
HighIntensity = 0x0008,
}
+ ///
+ /// Log handler for writing log messages to a console window (e.g. cmd.exe).
+ ///
+ /// The colors of the various log levels are defined in GetConsoleColor().
+ ///
public class ConsoleWindowLogHandler : ConsoleLogHandler {
- public ConsoleWindowLogHandler() : this("Log Window") {
+ #region Constructors
+ ///
+ /// Constructs a new ConsoleWindowLogHandler with the default title
+ /// and width
+ ///
+ public ConsoleWindowLogHandler() : this("SimpleLog Console Window") {
}
- public ConsoleWindowLogHandler(string title) : base() {
- ConsoleWindowLogHandler.AllocConsole();
- Console.Title = title;
+ ///
+ /// Constructs a new ConsoleWindowLogHandler with the specified title
+ /// and default width
+ ///
+ /// Title of the console window
+ public ConsoleWindowLogHandler(string title) : this(title, 100) {
+
}
+ ///
+ /// Constructs a new ConsoleWindowLogHandler with a user defined
+ /// title and width
+ ///
+ /// Title of the console window
+ /// Width of the console window
+ public ConsoleWindowLogHandler(string title, int width) : base() {
+ ConsoleWindowLogHandler.AllocConsole();
+ Console.Title = title;
+ Console.WindowWidth = width;
+ }
+ #endregion
+
+ ///
+ /// Gets the console handler
+ ///
+ /// Either stderr or stdout
protected IntPtr GetConsoleHandle(uint streamType) {
return ConsoleWindowLogHandler.GetStdHandle(streamType);
}
- protected ConsoleColor GetColorInfo(LogLevel level) {
+ ///
+ /// Gets the colors for the console based on the given log level.
+ ///
+ /// Override this method in your own class if you want to modify the
+ /// colors. Ideally these would be set by some config file, but
+ /// that can come later.
+ ///
+ protected virtual ConsoleColor GetConsoleColors(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;
+ return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
case SimpleLog.LogLevel.Error:
- return ConsoleColor.Red;
- case SimpleLog.LogLevel.Critical:
return ConsoleColor.Red | ConsoleColor.HighIntensity;
+ case SimpleLog.LogLevel.Critical:
+ return (ConsoleColor.White | ConsoleColor.HighIntensity) + ((int)(ConsoleColor.Red | ConsoleColor.HighIntensity) << 4);
default:
return ConsoleColor.White;
}
}
+ #region Overrides
+ ///
+ /// Logs a message to the console window
+ ///
+ /// The message to log
+ /// The log level of the message
public override bool Log(string message, LogLevel level) {
- uint streamType = this.GetStreamType(level);
+ uint streamType = this.GetStreamType(level);
System.IO.TextWriter writer = this.GetOutputStream(streamType);
-
+ IntPtr consoleHandle = GetConsoleHandle(streamType);
- IntPtr consoleHandle = GetConsoleHandle(streamType);
-
- ushort colorInfo = (ushort)this.GetColorInfo(level);
+ ushort colorInfo = (ushort)this.GetConsoleColors(level);
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
writer.Write(message);
return true;
}
-
- public new void GracefulShutDown() {
-
- }
+ #endregion
#region Win32 DLL imports
-
+ ///
+ /// Allocates a console window
+ ///
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
-
- [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- private static extern int GetConsoleOutputCP();
-
+ ///
+ /// Gets the output pointer based on the stream type (stderr or stdout)
+ ///
+ /// One of the STD_* constants
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetStdHandle(UInt32 type);
+ ///
+ /// Sets the color of the text to be written to the console
+ ///
+ /// A console handle returned by GetStdHandle()
+ /// Bitwise pairing of ConsoleColors
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
#endregion