Table of Contents

Class ExceptionConverter

Namespace
Codebelt.Extensions.YamlDotNet.Converters
Assembly
Codebelt.Extensions.YamlDotNet.dll

Converts an Exception to YAML.

public class ExceptionConverter : YamlConverter<Exception>, IYamlTypeConverter
Inheritance
ExceptionConverter
Implements
IYamlTypeConverter
Inherited Members

Examples

This example demonstrates how to use the ExceptionConverter to serialize exceptions with two different detail configurations, showing how to control which exception information appears in the serialized YAML. It shows the workflow: creating an exception converter with specific settings (stack trace and data), adding it to the formatter options, then demonstrating two scenarios—one with full details and one with minimal details. The observable outcome is that the same exception appears differently in YAML format depending on the converter configuration.

using System;
using System.Collections.Generic;
using System.IO;
using Codebelt.Extensions.YamlDotNet.Converters;
using Codebelt.Extensions.YamlDotNet.Formatters;

namespace ExampleNamespace;

class Program
{
    static void Main()
    {
        // Create an exception converter that includes stack traces and data
        var converter = new ExceptionConverter(includeStackTrace: true, includeData: true);
        
        // Create formatter options and add the converter
        var options = new YamlFormatterOptions();
        options.Settings.Converters.Add(converter);
        
        // Create a formatter with the custom converter
        var formatter = new YamlFormatter(options);
        
        // Demonstrate serialization with different levels of detail
        try
        {
            throw new InvalidOperationException("An error occurred during processing.", 
                new ArgumentException("Invalid argument provided"));
        }
        catch (Exception ex)
        {
            // Serialize with full details
            var yaml = formatter.Serialize(ex, typeof(Exception));
            var reader = new StreamReader(yaml);
            Console.WriteLine("Exception as YAML:");
            Console.WriteLine(reader.ReadToEnd());
        }
        
        // Converter without stack trace
        var minimalConverter = new ExceptionConverter(includeStackTrace: false, includeData: false);
        var minimalOptions = new YamlFormatterOptions();
        minimalOptions.Settings.Converters.Add(minimalConverter);
        
        try
        {
            throw new NullReferenceException("Value was null");
        }
        catch (Exception ex)
        {
            var formatter2 = new YamlFormatter(minimalOptions);
            var yaml = formatter2.Serialize(ex, typeof(Exception));
            var reader = new StreamReader(yaml);
            Console.WriteLine("\nMinimal exception details:");
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

Constructors

ExceptionConverter(bool, bool)

Initializes a new instance of the ExceptionConverter class.

public ExceptionConverter(bool includeStackTrace = false, bool includeData = false)

Parameters

includeStackTrace bool

A value that indicates if the stack of an exception is included in the converted result.

includeData bool

A value that indicates if the data of an exception is included in the converted result.

Properties

IncludeData

Gets a value indicating whether the data of an exception is included in the converted result.

public bool IncludeData { get; }

Property Value

bool

true if the data of an exception is included in the converted result; otherwise, false.

IncludeStackTrace

Gets a value indicating whether the stack of an exception is included in the converted result.

public bool IncludeStackTrace { get; }

Property Value

bool

true if the stack of an exception is included in the converted result; otherwise, false.

Methods

CanConvert(Type)

Determines whether this instance can convert the specified object type.

public override bool CanConvert(Type typeToConvert)

Parameters

typeToConvert Type

Type of the object.

Returns

bool

true if this instance can convert the specified object type; otherwise, false.

ReadYaml(IParser, Type)

Reads and converts the YAML to Exception.

public override Exception ReadYaml(IParser reader, Type typeToConvert)

Parameters

reader IParser

The reader to read from.

typeToConvert Type

The type to convert.

Returns

Exception

The converted value.

Exceptions

NotImplementedException

WriteYaml(IEmitter, Exception)

Writes a specified value as YAML.

public override void WriteYaml(IEmitter writer, Exception value)

Parameters

writer IEmitter

The writer to write to.

value Exception

The value to convert to YAML.

See Also