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

后端 未结 8 1415
刺人心
刺人心 2021-01-31 13:13

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?

相关标签:
8条回答
  • 2021-01-31 13:33

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

    Console.WriteLine(myException.StackTrace);

    0 讨论(0)
  • 2021-01-31 13:36

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

    0 讨论(0)
  • 2021-01-31 13:43

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

    Console.WriteLine(System.Environment.StackTrace);
    
    0 讨论(0)
  • 2021-01-31 13:48

    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.

    0 讨论(0)
  • 2021-01-31 13:48

    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

    0 讨论(0)
  • 2021-01-31 13:50
      catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
    
    0 讨论(0)
提交回复
热议问题