Example usage
try
{
// code which may throw an exception
}
catch (Exception e)
{
ExceptionHelpers.ColorWithCyanFuchsia(e);
}
Package Spectre.Console
Example usage
try
{
// code which may throw an exception
}
catch (Exception e)
{
ExceptionHelpers.ColorWithCyanFuchsia(e);
}
Package Spectre.Console
using Spectre.Console; | |
namespace SpectreExceptionsLibrary; | |
/// <summary> | |
/// Custom setting for presenting runtime exceptions using AnsiConsole.WriteException. | |
/// | |
/// The idea here is to present different types of exceptions with different colors while | |
/// one would be for all exceptions and the other(s) for specific exception types. | |
/// </summary> | |
public class ExceptionHelpers | |
{ | |
/// <summary> | |
/// Displays the provided exception using custom color settings for enhanced readability. | |
/// </summary> | |
/// <param name="exception"> | |
/// The exception to be displayed. This parameter cannot be null. | |
/// </param> | |
/// <remarks> | |
/// This method uses <see cref="AnsiConsole.WriteException"/> to render the exception | |
/// with a specific color scheme. The method highlights various parts of the exception, such as the | |
/// method name, parameter types, and paths, using colors like Fuchsia, Aqua, and Red. | |
/// </remarks> | |
/// <exception cref="ArgumentNullException"> | |
/// Thrown when the <paramref name="exception"/> parameter is null. | |
/// </exception> | |
public static void ColorWithCyanFuchsia(Exception exception) | |
{ | |
AnsiConsole.WriteException(exception, new ExceptionSettings | |
{ | |
Format = ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks, | |
Style = new ExceptionStyle | |
{ | |
Exception = new Style().Foreground(Color.Grey), | |
Message = new Style().Foreground(Color.DarkSeaGreen), | |
NonEmphasized = new Style().Foreground(Color.Cornsilk1), | |
Parenthesis = new Style().Foreground(Color.Cornsilk1), | |
Method = new Style().Foreground(Color.Fuchsia), | |
ParameterName = new Style().Foreground(Color.Cornsilk1), | |
ParameterType = new Style().Foreground(Color.Aqua), | |
Path = new Style().Foreground(Color.Red), | |
LineNumber = new Style().Foreground(Color.Cornsilk1), | |
} | |
}); | |
} | |
/// <summary> | |
/// Displays the provided exception using a standard color scheme for readability. | |
/// </summary> | |
/// <param name="exception"> | |
/// The exception to be displayed. This parameter cannot be null. | |
/// </param> | |
/// <remarks> | |
/// This method utilizes <see cref="AnsiConsole.WriteException"/> to render the exception | |
/// with a predefined standard color scheme. The method ensures consistent formatting for exception details, | |
/// such as method names, parameter types, and paths. | |
/// </remarks> | |
/// <exception cref="ArgumentNullException"> | |
/// Thrown when the <paramref name="exception"/> parameter is null. | |
/// </exception> | |
public static void ColorStandard(Exception exception) | |
{ | |
AnsiConsole.WriteException(exception, new ExceptionSettings | |
{ | |
Format = ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks, | |
Style = new ExceptionStyle | |
{ | |
Exception = new Style().Foreground(Color.Grey), | |
Message = new Style().Foreground(Color.White), | |
NonEmphasized = new Style().Foreground(Color.Cornsilk1), | |
Parenthesis = new Style().Foreground(Color.GreenYellow), | |
Method = new Style().Foreground(Color.DarkOrange), | |
ParameterName = new Style().Foreground(Color.Cornsilk1), | |
ParameterType = new Style().Foreground(Color.Aqua), | |
Path = new Style().Foreground(Color.White), | |
LineNumber = new Style().Foreground(Color.Cornsilk1), | |
} | |
}); | |
} | |
} |