Provides helper functions for exceptions to get all exceptions in the InnerException hierarchy and to build full messages out of it.
Note: The AggregateException which have multiple InnerExceptions will be handled too
Install DCCS.ExceptionHelpers.NetStandard with NuGet:
Install-Package DCCS.ExceptionHelpers.NetStandard
Or via the .NET Core command line interface:
dotnet add package DCCS.ExceptionHelpers.NetStandard
Either commands, from Package Manager Console or .NET Core CLI, will download and install DCCS.ExceptionHelpers.NetStandard.
Available extension methods in this package:
// Return the message of the exception and all inner exceptions.
public static string BuildCompleteMessage(this Exception exception, bool includeCallstack, string separator = null)
// Returns the exception and all inner exceptions
public static IEnumerable<Exception> GetAllExceptionsInHierarchy(this Exception exception)
Sample usage of the GetRecursiveMessage message:
// Add the namespace to your file to get the extension methods
using DCCS.ExceptionHelpers.NetStandard;
class Program
{
public void Main()
{
try
{
ReadConfig(); // Will throw an exception
}
catch (Exception e)
{
// Trace all exceptions in the stack including the call stack
Trace.WriteLine(e.BuildCompleteMessage(true));
// Write all exception messages to the console (without the call stack)
Console.WriteLine(e.BuildCompleteMessage(false));
}
}
void ReadConfig()
{
try
{
// This will raise an FileNotFound exception for demonstation
using (var configFile = File.OpenRead(Path.Combine(Path.GetTempPath(), @"NOTEXISTINGCONFIGFILE.XXX")))
{
}
}
catch (Exception e)
{
throw new Exception("Read configuration failed", e);
}
}
}
All contributors are welcome to improve this project. The best way would be to start a bug or feature request and discuss the way you want find a solution with us. After this process, please create a fork of the repository and open a pull request with your changes. Of course, if your changes are small, feel free to immediately start your pull request without previous discussion.