CleanStackTrace 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package CleanStackTrace --version 1.1.3
                    
NuGet\Install-Package CleanStackTrace -Version 1.1.3
                    
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="CleanStackTrace" Version="1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanStackTrace" Version="1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="CleanStackTrace" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CleanStackTrace --version 1.1.3
                    
#r "nuget: CleanStackTrace, 1.1.3"
                    
#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.
#:package CleanStackTrace@1.1.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CleanStackTrace&version=1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=CleanStackTrace&version=1.1.3
                    
Install as a Cake Tool

CleanStackTrace

CleanStackTrace is a lightweight .NET library that simplifies and cleans up stack traces, making them more readable and developer-friendly.

By reducing long namespaces, compiler-generated method names, and unnecessary noise, CleanStackTrace helps you focus on what really matters: understanding where the error happened.


โœจ Features

  • ๐Ÿš€ Remove unnecessary namespaces and keep only the essential class and method names;
  • ๐Ÿงน clean up compiler-generated artifacts (e.g. <>c.<...>b__...) that pollute stack traces;
  • ๐Ÿ” keep useful information like parameters and line numbers;
  • ๐Ÿ›  works seamlessly with exceptions and inner exceptions;
  • ๐Ÿ”ง easy to plug into existing logging systems.

๐Ÿ“ฆ Installation

The package is available on NuGet:

dotnet add package 

๐Ÿ”ง Usage

try
{
    throw new ApplicationException("Something went wrong");
}
catch (Exception ex)
{
    string cleaned = ex.CleanStackTrace();
    Console.WriteLine(cleaned);
}

Instead of

CleanStackTrace.Demo.CustomStackTraceException: A wrong situation
 ---> System.ApplicationException: Error in the application.
   --- End of inner exception stack trace ---
System.NullReferenceException: Object reference not set to an instance of an object.
at Services.ProjectA.API.Controllers.UserController.GetUser(Guid id) in C:\Users\UserName\Repositories\Client\Services\ProjectA\API\Controllers\UserController.cs:line 27
at lambda_method4(Closure, Object)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Services.ProjectA.API.Extensions.WebApplicationExtensions.<>c.<<AutoOptionsMiddleware>b__2_0>d.MoveNext() in C:\Users\UserName\Repositories\Client\Servicesoa\progetto\Extensions\WebApplicationExtensions.cs:line 40
--- End of stack trace from previous location ---
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpcontext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Output (example):

CustomStackTraceException: A wrong situation
  ApplicationException: Error in the application.
    NullReferenceException: Object reference not set to an instance of an object.
    UserController.GetUser(Guid id) line 27
    WebApplicationExtensions.AutoOptionsMiddleware() line 40

You can also obtain the colored version using the method GetColoredCleanStackTrace(), in this version:

  • Exceptions are red;
  • classes are blue;
  • methods are cyan;
  • and lines of code are yellow.

alt text

You can also define your own transformers with the overload GetColoredCleanStackTrace(linestransformers, linetransformers). See the following section for more information.


โš™๏ธ Custom Clean Stack Traces

The GetColoredCleanStackTrace(linestransformers, linetransformers) method allows you to create customized colored stack traces using your own transformers. This is useful when you want specific output. Example:

// Create your custom color transformers
var customTransformers = new List<IStackTraceLineTransformer>
{
    new HighlightClassTransformer(),    // Blue for classes
    new HighlightExceptionTransformer(), // Red for exceptions
    new HighlightFunctionsTransformer()  // Cyan for methods
};

// Apply custom coloring
string coloredStackTrace = exception.GetCleanStackTrace(
    linesTransformers: TransformerCollections.StandardLinesTransformers,
    lineTransformers: customTransformers
);

This example colors the stack trace, indents it, but does not apply any alterations to the lines.

You can create your own color transformer this way:

public class CustomHighlightTransformer : IStackTraceLineTransformer
{
    private const string ColorStart = "\u001b[35m"; // Purple
    private const string ColorEnd = "\u001b[0m";

    public string? Apply(string line)
    {
        // Highlight specific patterns in purple
        return Regex.Replace(line, @"(Microsoft|System)", $"{ColorStart}$1{ColorEnd}");
    }
}

๐Ÿค Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions, improvements, or bug fixes.


๐Ÿ“œ License

MIT License, see LICENSE file for more info.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.4 221 9/14/2025
1.1.4 183 9/7/2025
1.1.3 142 9/6/2025
1.1.2 139 9/6/2025
1.1.1 166 9/1/2025
0.1.1 201 8/31/2025
0.0.1 194 8/31/2025