Skip to content

AspectObjectDumper overview

Val Melamed edited this page May 4, 2017 · 6 revisions

AspectObjectDumper

The name of the project says it pretty much all: to have an easy to integrate and easy to use component which dumps the value of a .NET object in a text form. This is very helpful for debugging and logging.

Features

Here is more detailed list of implemented features:

  • The dump output is separated from the targeted dump media by the familiar TextWriter interface.
  • The programmer can dump the value of any type of object: primitive and enum types (including those marked with FlagsAttribute), custom classes, struct-s, collections, etc.
  • For complex type objects the dumper outputs the names and the values of the properties and possibly fields.
  • The dump is recursive: the complex members of the dumped object are dumped indented.
  • Dumps the elements of sequences (arrays, lists, dictionaries, etc.)
  • Handles circular references.
  • The developers can easily customize various aspects of the dump by using an attribute (DumpAttribute) on the classes and their members, such as:
    • the order in which the properties are dumped;
    • formatting of the property names and/or values;
    • suppressing the dump of some uninteresting properties;
    • masking the values of sensitive (e.g. PII) properties;
    • the depth of the recursion into complex members;
    • the maximum number of dumped elements of sequence properties.
  • The dumper picks the customization attributes also from metadata type classes (buddy classes) declared with MetadataTypeAttribute.
  • Even if the class doesn't have any attributes, the programmer can still pass a metadata class as a separate parameter to the dumping method. This allows for nice control over the dump of BCL and third party classes.

Implementation and Performance

As you might have guessed it, the dumper is a big exercise on .NET reflection. As of v1.7.0-beta there is a promising performance optimization which leverages LINQ expression trees. For each dumped type of object, the dumper uses reflection to iterate through its properties and fields, and the properties of the base classes and the aggregated objects. Along the traversal however it also builds an expression tree that mirrors the dumping process of writing labels, accessing members, formatting and writing properties' values directly without reflection. The so generated expression tree is then compiled cached. Any subsequent calls to dump objects of the same type, retrieve the dumping code from an internal static cache and execute it. The performance of the subsequent calls is about 50-100 times faster.

Usage

The dumper is implemented by the class ObjectTextDumper in the namespace vm.Aspects.Diagnostics. See the more detailed documentation to see how to use this component and how to customize your dumps.