• This repository has been archived on 15/Jun/2023
  • Stars
    star
    213
  • Rank 185,410 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Async-friendly format for stack traces and exceptions

Async-Friendly Stack Trace

Archived - no longer needed on .NET 5+

Note I have released version 1.7.0 of this package for .NET 6+ that basically calls StackTrace.ToString() and Exception.ToString() and adds Obsolete attributes, to alert users it is no longer needed.

Async-friendly format for stack traces and exceptions.

NuGet

Also check out Ben's Demystifier which resolves async, iterators, tuples, location functions and more.

System.Exception: Crash! Boom! Bang!
   at async AsyncFriendlyStackTrace.Test.Example1.C(?) in C:\Source\Repos\AsyncFriendlyStackTrace\src\AsyncFriendlyStackTrace.Test\Example1.cs:line 26
   at async AsyncFriendlyStackTrace.Test.Example1.B(?) in C:\Source\Repos\AsyncFriendlyStackTrace\src\AsyncFriendlyStackTrace.Test\Example1.cs:line 20
   at async AsyncFriendlyStackTrace.Test.Example1.A(?) in C:\Source\Repos\AsyncFriendlyStackTrace\src\AsyncFriendlyStackTrace.Test\Example1.cs:line 15
   at async AsyncFriendlyStackTrace.Test.Example1.Run(?) in C:\Source\Repos\AsyncFriendlyStackTrace\src\AsyncFriendlyStackTrace.Test\Example1.cs:line 10
   at AsyncFriendlyStackTrace.Test.Program.Run[TExample](TextWriter writer) in C:\Source\Repos\AsyncFriendlyStackTrace\src\AsyncFriendlyStackTrace.Test\Program.cs:line 45

Install

Install-Package AsyncFriendlyStackTrace

Usage

To format exceptions, use the extension methods in ExceptionExtensions:

exception.ToAsyncString();

This produces an async-friendly format, as you can see in the examples below. There is also special handling for AggregateExceptions and ReflectionTypeLoadException (which can contain multiple inner exceptions).

The main formatting work is done by the StackTraceExtensions.ToAsyncString extension method. The async-friendly formatting is archieved by:

  • Skipping all awaiter frames (all methods in types implementing INotifyCompletion) and ExceptionDispatchInfo frames.
  • Inferring the original method name from the async state machine class (IAsyncStateMachine) and removing the "MoveNext" - currently only for C#.
  • Adding the "async" prefix after "at" on each line for async invocations.
  • Appending "(?)" to the method signature to indicate that parameter information is missing.
  • Removing the "End of stack trace from previous location..." text.

Example outputs

In all the examples, OLD refrers to ToString() output, while NEW is ToAsyncString().

  • Example 1 (code): A simple 3 async method chain.
  • Example 2 (code): Async invocations with a synchronous Wait() in the middle, causing an AggregateException.
  • Example 3 (code): Bad Serialization - When exception is serialized and deserialized, its stack trace is saved as string. So we can't reformat the stack trace. The "new" stack trace is still a bit shorter due to an improved AggregateException formatting (the first inner exception isn't repeated twice).
  • Example 4 (code): Good Serialization - We use the PrepareForAsyncSerialization before serializing the exception. This saves the async-friendly stack trace as a string in the Data dictionary of the exception. This has two downsides:
    • The serialized data will now contain both stack trace formats.
    • When using the DataContractSerializer, you must include exception.Data.GetType() as a known type. This is because its concrete type (ListDictionaryInternal) is internal.

More Repositories

1

RoslynPad

A cross-platform C# editor based on Roslyn and AvalonEdit
C#
1,445
star
2

IgnoresAccessChecksToGenerator

Generates reference assemblies where all the internal types & members become public, and applies the IgnoresAccessChecksTo attribute
C#
165
star
3

WPFContrib

A collection of WPF controls and utility classes I've accumulated over the years.
C#
127
star
4

ConfigureAwaitChecker

ConfigureAwait Checker for ReSharper and Rider
C#
102
star
5

spanify

Span-ify your code: write high performance memory aware C#
C#
63
star
6

RawInputProcessor

Provides a way to distinguish input from multiple keyboards
C#
42
star
7

FasterReflection

Utilizes System.Reflection.Metadata to read type information very fast and without locking assembly files.
C#
31
star
8

PerformanceMonitor

Windows Performance Monitor - Reimagined
C#
12
star
9

snoopx

SnoopX - The .NET Spy Utility
C#
5
star
10

EventSourceExtensions

A library for generating EventSource classes from interfaces at run-time
C#
4
star
11

vsts-service-fabric

Service Fabric Tools for VSTS
PowerShell
4
star
12

ResourceDictionaryMerge

C#
2
star
13

samples-servicefabric-reliablecollectionquery

Provides a way to get reliable collection data as JSON and dump them into files
C#
2
star
14

azure-pipelines-template-parser

Azure DevOps pipelines yaml parser
C#
1
star
15

WPFToolkit

Fork of WPF Toolkit for .NET 4.5
C#
1
star
16

LoggerCodeGeneration

Microsoft.Extensions.Logging Code Generator
C#
1
star
17

service-fabric-test-collections

Test implementations of Service Fabric Reliable Collections
C#
1
star
18

kestrel-aks-kv-rotation

PowerShell
1
star