Table of Contents

Class ExceptionDescriptorConverter

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

Converts an ExceptionDescriptor to YAML.

public class ExceptionDescriptorConverter : YamlConverter<ExceptionDescriptor>, IYamlTypeConverter
Inheritance
ExceptionDescriptorConverter
Implements
IYamlTypeConverter
Inherited Members

Examples

This example demonstrates how to serialize exception objects using the ExceptionDescriptorConverter with customizable detail levels. It shows the complete workflow: creating an ExceptionDescriptor from a caught exception, instantiating the converter with fine-grained sensitivity settings (stack trace, data, and evidence), wrapping it in a YamlFormatter, and then serializing to observe the full exception information in YAML format. The example illustrates how to control what exception details appear in the output by configuring FaultSensitivityDetails flags on the converter.

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

namespace ExampleNamespace;

class Program
{
    static void Main()
    {
        // Create an exception and descriptor
        try
        {
            throw new InvalidOperationException("Operation failed.", 
                new ArgumentException("Invalid input"));
        }
        catch (Exception ex)
        {
            var descriptor = new ExceptionDescriptor(ex, "E001", "An operation error occurred.");
            
            // Create converter with custom sensitivity settings
            var converter = new ExceptionDescriptorConverter(options =>
            {
                options.SensitivityDetails = FaultSensitivityDetails.StackTrace | 
                                            FaultSensitivityDetails.Data | 
                                            FaultSensitivityDetails.Evidence;
            });
            
            // Create formatter with the converter
            var formatterOptions = new YamlFormatterOptions();
            formatterOptions.Settings.Converters.Add(converter);
            
            var formatter = new YamlFormatter(formatterOptions);
            
            // Serialize the descriptor
            var yaml = formatter.Serialize(descriptor, typeof(ExceptionDescriptor));
            using (var reader = new StreamReader(yaml))
            {
                Console.WriteLine("Exception Descriptor as YAML:");
                Console.WriteLine(reader.ReadToEnd());
            }
        }
    }
}

Constructors

ExceptionDescriptorConverter(Action<ExceptionDescriptorOptions>)

Initializes a new instance of the ExceptionDescriptorConverter class.

public ExceptionDescriptorConverter(Action<ExceptionDescriptorOptions> setup = null)

Parameters

setup Action<ExceptionDescriptorOptions>

The ExceptionDescriptorOptions which may be configured.

Methods

CanConvert(Type)

Determines whether this instance can convert the specified object type.

public override bool CanConvert(Type typeToConvert)

Parameters

typeToConvert Type

The 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 ExceptionDescriptor.

public override ExceptionDescriptor ReadYaml(IParser reader, Type typeToConvert)

Parameters

reader IParser

The reader to read from.

typeToConvert Type

The type to convert.

Returns

ExceptionDescriptor

The converted value.

Exceptions

NotImplementedException

WriteYaml(IEmitter, ExceptionDescriptor)

Writes a specified value as YAML.

public override void WriteYaml(IEmitter writer, ExceptionDescriptor value)

Parameters

writer IEmitter

The writer to write to.

value ExceptionDescriptor

The value to convert to YAML.

See Also