Salix.StackTracer 1.1.0

dotnet add package Salix.StackTracer --version 1.1.0
NuGet\Install-Package Salix.StackTracer -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Salix.StackTracer" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Salix.StackTracer --version 1.1.0
#r "nuget: Salix.StackTracer, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Salix.StackTracer as a Cake Addin
#addin nuget:?package=Salix.StackTracer&version=1.1.0

// Install Salix.StackTracer as a Cake Tool
#tool nuget:?package=Salix.StackTracer&version=1.1.0

Salix.StackTracer

Extensions methods to StackTrace and Exception (any derived exception, too) objects to get more simplified and usable stack trace frame list, wrapped in custom object list.

// omitted for brevity
catch (Exception ex)
{
    var simpleFrames = ex.ParseStackTrace(); // <---- This is the extension method
    logger.LogError(simpleFrames[0].ToString()); // <--- oversimplified usage
}

StackTrace frames can be filtered with several filter approaches to leave only frames from own code and drop everything passed through .Net framework and external package functionalities, where normally developers does not have direct control over (and 99% times is not cause for thrown exception).

Overriden ToString() for returned StackTraceFrame objects can be used to show cleaned up and more readable stack trace output (for logging).

Salix.StackTracer.Tests\TestableMethods.cs; Throwing(); Line:30 (Col:33)
Salix.StackTracer.Tests\TestableMethods.cs; WithNullableParameters(Nullable<int> optionalId, Nullable<bool> triState, string optionalName); Line:23 (Col:9)
Salix.StackTracer.Tests\ParserTests.cs; Method_NullParameters(); Line:128 (Col:13)

If you want to format or colorize outout yourself, StackTraceFrame has properties containing parts of this output as separate types/strings/objects.

StackTraceFrame contains:

  • Method name
    • Collection of parameters of the method (empty if method does not have parameters)
    • Collection of generic types, if method is generic one.
  • Containing type (Class, which defines this method)
  • Code file path as string (shortened, relative)
  • Line number
    • Column number

StackTraceFrame does not provide values, passed into method as parameters, as this information is not available in .Net framework Stack Trace.

Filtering

If you are interested only in your own code to be shown in stack trace (skip all .Net code and external dependencies code), there are options to be set when ParseStackTrace() extensions methods are invoked.

SkipFramesWithoutLineNumber

// omitted for brevity
catch (Exception ex)
{
    var simpleFrames = ex.ParseStackTrace(new StackTracerOptions { SkipFramesWithoutLineNumber = true });
    // -OR -
    var simpleFrames = ex.ParseStackTrace(o => o.SkipFramesWithoutLineNumber = true);
}

This will skip all stack frames where line number is not retrieved. It happens for assemblies which does not have PDB files available (normally framework and added NuGet packages) and is the simplest way to filter out these dependencies.

NOTE: If you forcibly remove PDB files in your deployed application for your own code, if might return empty collection of stack frames, as your own code will not have line number, too.

ShowOnlyFramesWithNamespace

// omitted for brevity
catch (Exception ex)
{
    var simpleFrames = ex.ParseStackTrace(new StackTracerOptions { ShowOnlyFramesWithNamespace = new HashSet<string> { "MyApp." } });
    // -OR -
    var simpleFrames = ex.ParseStackTrace(o => o.ShowOnlyFramesWithNamespace = new HashSet<string> { "MyApp." });
}

This will skip (whitelist) all stack frames where method's containing type does not contain one of given strings in options.

SkipFramesContaining

// omitted for brevity
catch (Exception ex)
{
    var simpleFrames = ex.ParseStackTrace(new StackTracerOptions
    {
        ShowOnlyFramesWithNamespace = new HashSet<string> { "MyApp." },
        SkipFramesContaining = new HashSet<string> { "ButBlackistedExact" }
    });

    // - OR -

    var simpleFrames = exc.ParseStackTrace(opts =>
    {
        opts.ShowOnlyFramesWithNamespace = new HashSet<string> { "MyApp." };
        opts.SkipFramesContaining = new HashSet<string> { "ButBlackistedExact" };
    });
}

This will skip (blacklist) all stack frames where either namespace of method's containing type or method name contains one of given strings in options. Can be used to skip general filters, middleware etc. which usually cannot be cause for exception, but is part of your own code.

Cleanup original StackTrace

Exception has an extension method to get back a list of StackFrames (.Net objects) which are filtered based on options and file paths shortened.

This can come handy in case there is a need to have .Net StackTrace in their original object types.

// omitted for brevity
catch (Exception ex)
{
    var filteredFrames = ex.FilteredStackTrace(new StackTracerOptions { SkipFramesWithoutLineNumber = true });
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Salix.StackTracer:

Package Downloads
Salix.AspNetCore.JsonExceptionHandler

Provides Global Error/Exception Handler (Middleware). Responds with error description and data as Json object. Better StackTrace, details on exception, possibility to control response state (500+, 400+).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 577 1/11/2024
1.0.1 195 11/19/2023
1.0.0 267 11/26/2022

Added extension method for exception to get filtered original .Net StackFrames (as shortened StackTrace).
Added options setting action to use instead of options object create beforehand.