reset the console colors after writing to the console (shamelessly stolen from log4net)

This commit is contained in:
tmont 2009-04-12 07:47:42 +00:00
parent 026437d399
commit ca0445bd87

View File

@ -19,6 +19,8 @@ namespace SimpleLog.LogHandlers {
HighIntensity = 0x0008, HighIntensity = 0x0008,
} }
/// <summary> /// <summary>
/// Log handler for writing log messages to a console window (e.g. cmd.exe). /// Log handler for writing log messages to a console window (e.g. cmd.exe).
/// ///
@ -100,15 +102,50 @@ namespace SimpleLog.LogHandlers {
System.IO.TextWriter writer = this.GetOutputStream(streamType); System.IO.TextWriter writer = this.GetOutputStream(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType); IntPtr consoleHandle = GetConsoleHandle(streamType);
//get the original colors of the console
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
GetConsoleScreenBufferInfo(consoleHandle, out bufferInfo);
ushort colorInfo = (ushort)this.GetConsoleColors(level); ushort colorInfo = (ushort)this.GetConsoleColors(level);
//set the console colors
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo); ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
//write the message to the console
writer.Write(message); writer.Write(message);
//reset the console colors
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, bufferInfo.wAttributes);
return true; return true;
} }
#endregion #endregion
#region Win32 DLL imports #region Win32 API stuff
[StructLayout(LayoutKind.Sequential)]
private struct COORD {
public UInt16 x;
public UInt16 y;
}
[StructLayout(LayoutKind.Sequential)]
private struct SMALL_RECT {
public UInt16 Left;
public UInt16 Top;
public UInt16 Right;
public UInt16 Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CONSOLE_SCREEN_BUFFER_INFO {
public COORD dwSize;
public COORD dwCursorPosition;
public ushort wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
/// <summary> /// <summary>
/// Allocates a console window /// Allocates a console window
/// </summary> /// </summary>
@ -129,6 +166,14 @@ namespace SimpleLog.LogHandlers {
/// <param name="attributes">Bitwise pairing of ConsoleColors</param> /// <param name="attributes">Bitwise pairing of ConsoleColors</param>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes); private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
/// <summary>
/// Gets info about the console window screen
/// </summary>
/// <param name="consoleHandle">A console handle return by GetStdHandle()</param>
/// <param name="bufferInfo"></param>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetConsoleScreenBufferInfo(IntPtr consoleHandle, out CONSOLE_SCREEN_BUFFER_INFO bufferInfo);
#endregion #endregion
} }