private void WriteToOutput(LogEventInfo logEvent, string message)
{
ConsoleColor oldForegroundColor = Console.ForegroundColor;
ConsoleColor oldBackgroundColor = Console.BackgroundColor;
bool didChangeForegroundColor = false, didChangeBackgroundColor = false;
try
{
var matchingRule = GetMatchingRowHighlightingRule(logEvent);
didChangeForegroundColor = IsColorChange(matchingRule.ForegroundColor, oldForegroundColor);
if (didChangeForegroundColor)
Console.ForegroundColor = (ConsoleColor)matchingRule.ForegroundColor;
didChangeBackgroundColor = IsColorChange(matchingRule.BackgroundColor, oldBackgroundColor);
if (didChangeBackgroundColor)
Console.BackgroundColor = (ConsoleColor)matchingRule.BackgroundColor;
try
{
var consoleStream = this.ErrorStream ? Console.Error : Console.Out;
if (this.WordHighlightingRules.Count == 0)
{
consoleStream.WriteLine(message);
}
else
{
message = message.Replace("\a", "\a\a");
foreach (ConsoleWordHighlightingRule hl in this.WordHighlightingRules)
{
message = hl.ReplaceWithEscapeSequences(message);
}
ColorizeEscapeSequences(consoleStream, message, new ColorPair(Console.ForegroundColor, Console.BackgroundColor), new ColorPair(oldForegroundColor, oldBackgroundColor));
consoleStream.WriteLine();
didChangeForegroundColor = didChangeBackgroundColor = true;
}
}
catch (IndexOutOfRangeException ex)
{
//this is a bug and therefor stopping logging. For docs, see PauseLogging property
pauseLogging = true;
InternalLogger.Warn(ex, "An IndexOutOfRangeException has been thrown and this is probably due to a race condition." +
"Logging to the console will be paused. Enable by reloading the config or re-initialize the targets");
}
}
finally
{
if (didChangeForegroundColor)
Console.ForegroundColor = oldForegroundColor;
if (didChangeBackgroundColor)
Console.BackgroundColor = oldBackgroundColor;
}
}