Чтобы не захламлять код проекта, решил запереть раскраску вывода текста консоли в отдельный класс ConsoleMessagesColorize. Постарался охватить все стандартные события приложений: ошибка, предупреждение, сообщение, вопрос и успех. Уже зарябило, да? Главное не злоупотреблять!
Собственно класс:
using System;
class ConsoleMessagesColorize
{
// Цвета для методов
static ConsoleColor ErrorColor = ConsoleColor.Red;
static ConsoleColor WarningColor = ConsoleColor.Yellow;
static ConsoleColor StableColor = ConsoleColor.Green;
static ConsoleColor MessageColor = ConsoleColor.Blue;
static ConsoleColor QuestionColor = ConsoleColor.Cyan;
// Ошибка
public static void Error(string message)
{
ConsoleColor GetForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = ErrorColor;
Console.WriteLine(message);
Console.ForegroundColor = GetForegroundColor;
}
// Предупреждение
public static void Warning(string message)
{
ConsoleColor GetForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = WarningColor;
Console.WriteLine(message);
Console.ForegroundColor = GetForegroundColor;
}
// Положительный вывод
public static void Stable(string message)
{
ConsoleColor GetForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = StableColor;
Console.WriteLine(message);
Console.ForegroundColor = GetForegroundColor;
}
// Сообщение
public static void Message(string message)
{
ConsoleColor GetForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = MessageColor;
Console.WriteLine(message);
Console.ForegroundColor = GetForegroundColor;
}
// Вопрос
public static void Question(string message)
{
ConsoleColor GetForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = QuestionColor;
Console.WriteLine(message);
Console.ForegroundColor = GetForegroundColor;
}
}
Закидываем отдельным cs в проект и используем:
ConsoleMessagesColorize.Error("Ошибочка вышла!");
ConsoleMessagesColorize.Warning("Предупреждение!");
ConsoleMessagesColorize.Stable("Всё хорошо!");
ConsoleMessagesColorize.Message("Сообщение.");
ConsoleMessagesColorize.Question("Есть вопросы?");
И для примера — простенький testapp:
using System;
namespace testapp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Normal color!");
ConsoleMessagesColorize.Error("Ошибочка вышла!");
ConsoleMessagesColorize.Warning("Предупреждение!");
ConsoleMessagesColorize.Stable("Всё хорошо!");
ConsoleMessagesColorize.Message("Сообщение.");
ConsoleMessagesColorize.Question("Есть вопросы?");
Console.WriteLine("Normal color again!");
Console.ReadLine();
}
}
}
Обращаю внимание, что если надумаете менять цвета методов класса — они должны соответствовать ConsoleColor.
Репозиторий где-то здесь.