Formatting Exception Display - Spectre Console Series
When building a .NET console application, you often have to handle exceptions in a way that makes sense to both developers and users. Plain console output can make exceptions look like long, unreadable strings of data — especially when dealing with stack traces and reflection-heavy libraries. In his video “Formatting Exception Display - Spectre Console Series”, Tim Corey walks us through how to use the Spectre.Console library to make exceptions easier to read, parse, and understand.
In this article, we’ll explore how Tim handles exceptions, uses commands and methods from Spectre.Console, and applies different settings and styles to the exception display.
Introduction: Why Format Exceptions?
At the beginning of the video, Tim Corey introduces Spectre.Console, describing it as a library that helps developers turn their plain console apps into “visually appealing, informative applications.” In this particular lesson, he focuses on how to handle exceptions in a way that makes their display clearer and more readable.
Tim explains that by default, when an exception is thrown in a console app, the output you get from Console.WriteLine(ex) is just a wall of text. It includes paths, stack traces, and type names — all as a continuous string. It’s functional, but not friendly.
To demonstrate, Tim creates a small example:
try
{
throw new Exception("You forgot to give me the data.");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}try
{
throw new Exception("You forgot to give me the data.");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}He runs this command in the terminal, and as expected, the output is messy. Everything blends together. Tim remarks that it’s hard to pick out what matters — the message, the method, or the file location. So he sets out to fix that using Spectre.Console exceptions formatting.
Using AnsiConsole.WriteException – The First Step
To improve the display, Tim replaces the line:
Console.WriteLine(ex);Console.WriteLine(ex);with a Spectre command:
AnsiConsole.WriteException(ex);AnsiConsole.WriteException(ex);This single method call changes everything.
When he runs the app again, the output in the terminal is now colorized. The message appears clearly, and the stack trace sections are styled differently. The text is easier to parse, and key values stand out.
Tim points out that the AnsiConsole class acts as the central access point in Spectre.Console for rendering styled output. The WriteException() method is designed specifically to handle exceptions and format them automatically for you.
He says, “That’s already a step up.” The console now highlights the message, the file path, and the method name, making it much easier to read at a glance.
Adding Formatting Options with ExceptionFormats
After showing the basic example, Tim takes it a step further by adding formatting parameters to control what parts of the exception are displayed and how they are shortened.
He introduces the ExceptionFormats enumeration, which provides several options like ShortenPaths, ShortenTypes, ShortenMethods, and ShowLinks. You can use these individually or combine them using the pipe (|) character.
Tim writes the following code:
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);When he runs this command, the console output becomes shorter and more concise.
He explains that:
Paths are shortened – instead of showing the full directory path, it just shows the filename like Program.cs.
Types are shortened – instead of System.IO.InvalidDataException, you simply see InvalidDataException.
- Methods are shortened – long generic method names are trimmed down for clarity.
This example shows how Spectre.Console’s exception formatting options make the result more compact without losing essential information.
Tim emphasizes that the first version (full path and namespaces) contains “mostly noise,” while this formatted version makes better sense when you just want to see the source of the problem quickly.
Combining Multiple Formats for More Control
Tim doesn’t stop there. He demonstrates how you can combine multiple formatting settings using the pipe operator:
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);At 3:54, Tim explains the ShowLinks option. This allows Spectre.Console to render clickable or hoverable file links in compatible terminals. In his example, the filename Program.cs now appears underlined. When he hovers over it, the terminal shows a tooltip with the full file path.
He notes that this behavior depends on your environment — it works better in terminals that support clickable links (like some bash or VS Code terminals).
By combining formats, you can decide exactly how much information your exception output should display — maybe you want shortened paths but full type names, or you want to hide method details entirely. Spectre.Console gives you that flexibility.
Going Deeper with ExceptionSettings
At this point, Tim introduces a new object called ExceptionSettings. This lets you customize both formatting and styling of exceptions together in a more advanced way.
He explains that instead of passing a single format value, you can pass an entire settings object:
var settings = new ExceptionSettings
{
Format = ExceptionFormats.ShortenEverything,
Style = new ExceptionStyle
{
Message = new Style(foreground: Color.Red, decoration: Decoration.SlowBlink)
}
};
AnsiConsole.WriteException(ex, settings);var settings = new ExceptionSettings
{
Format = ExceptionFormats.ShortenEverything,
Style = new ExceptionStyle
{
Message = new Style(foreground: Color.Red, decoration: Decoration.SlowBlink)
}
};
AnsiConsole.WriteException(ex, settings);Here, Tim configures two properties:
Format – specifies how the exception information should be shortened.
- Style – defines how different exception elements (like method names, paths, or messages) should look.
Spectre.Console provides a rich set of styling options such as foreground colors, background colors, and decorations (like underline, blink, bold, etc.).
Tim notes that you can also style other parts of the exception display, such as parameter names, method calls, or parentheses — not just the message. This allows for very fine-grained control over how your exception output looks in the CLI.
Styling the Exception Message
To show how styling works in action, Tim applies a style specifically to the message part of the exception. He sets the message color to red and adds a slow blink effect to make it stand out even more:
Message = new Style(foreground: Color.Red, decoration: Decoration.SlowBlink)Message = new Style(foreground: Color.Red, decoration: Decoration.SlowBlink)When he runs the app, the message “Hey, you forgot to give me the data.” now blinks in red inside the terminal window.
Tim points out that this helps highlight the most important piece of information in the exception output — the message itself. The rest of the text (like file name and line number) remains available below, but your eyes are immediately drawn to what went wrong.
He explains, “It really draws your eye to the most important thing, which is the message itself. And then they can figure out, oh, it’s in main on Program.cs at line 256.”
This demonstrates how Spectre.Console not only makes data readable but also adds emphasis and visual hierarchy to help you interpret results faster.
Wrapping Up the Spectre Console Series
As the video concludes, Tim mentions that this was the final part of his Spectre.Console series — at least for now. He encourages viewers to leave feedback or suggestions for more topics at suggestions.iamtimcorey.com.
He notes that throughout the series, they’ve covered a range of Spectre features: rendering tables, handling CLI commands and arguments, working with structured text, and now formatting exceptions.
Tim ends by saying this lesson gives developers the tools to handle exceptions gracefully, with clarity and visual feedback — all within a simple .NET console app.
Final Thoughts
Through this walkthrough, Tim Corey clearly demonstrates how to make Spectre.Console exceptions more readable, informative, and visually engaging. Using just a few lines of code, developers can:
Handle exceptions more effectively.
Format console output with shortened paths and cleaner text.
Use ExceptionSettings objects to fine-tune format and style.
Add color and decoration to highlight important information.
- Display file paths as clickable or hoverable links.
This approach not only improves debugging but also makes CLI-based applications look polished and professional. By combining powerful methods like AnsiConsole.WriteException(), customizable settings, and rich styling options, developers can build .NET console apps that provide meaningful, well-structured exception output that’s easy to parse and understand — exactly as Tim Corey shows in his excellent example.
