C# equivalent to Java's Exception.printStackTrace()?

C#.NetExceptionStack Trace

C# Problem Overview


Is there a C# equivalent method to Java's Exception.printStackTrace() or do I have to write something myself, working my way through the InnerExceptions?

C# Solutions


Solution 1 - C#

Try this:

Console.WriteLine(ex.ToString());

From http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx:

> The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is null, its value is not included in the returned string.

Note that in the above code the call to ToString isn't required as there's an overload that takes System.Object and calls ToString directly.

Solution 2 - C#

I would like to add: If you want to print the stack outside of an exception, you can use:

Console.WriteLine(System.Environment.StackTrace);

Solution 3 - C#

As Drew says, just converting the exception a string does this. For instance, this program:

using System;

class Test
{
    static void Main()
    {
        try
        {
            ThrowException();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    static void ThrowException()
    {
        
        try
        {
            ThrowException2();
        }
        catch (Exception e)
        {
            throw new Exception("Outer", e);
        }
    }

    static void ThrowException2()
    {
        throw new Exception("Inner");
    }
}

Produces this output:

System.Exception: Outer ---> System.Exception: Inner
   at Test.ThrowException2()
   at Test.ThrowException()
   --- End of inner exception stack trace ---
   at Test.ThrowException()
   at Test.Main()

Solution 4 - C#

Solution 5 - C#

  catch (Exception ex)
{
	Console.WriteLine(ex.StackTrace);
}

Solution 6 - C#

Is there no C# Logging API that can take an Exception as an argument and handle everything for you, like Java's Log4J does?

I.e., use Log4NET.

Solution 7 - C#

Since @ryan-cook answer didn't work for me, if you want to print the stack trace without having an exception, you can use:

System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
Console.WriteLine(stackTrace)

Unfortunately, this can't be done in a PCL or in a .NETStandard Library

Solution 8 - C#

Also look at Log4Net... its a port of Log4J to .NET.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEpagaView Question on Stackoverflow
Solution 1 - C#Drew NoakesView Answer on Stackoverflow
Solution 2 - C#Ryan CookView Answer on Stackoverflow
Solution 3 - C#Jon SkeetView Answer on Stackoverflow
Solution 4 - C#nbevansView Answer on Stackoverflow
Solution 5 - C#Steve BrouillardView Answer on Stackoverflow
Solution 6 - C#JeeBeeView Answer on Stackoverflow
Solution 7 - C#gianlucaparadiseView Answer on Stackoverflow
Solution 8 - C#nbevansView Answer on Stackoverflow