C# 等同于 Java 的 Exception.printStackTrace()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/333953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# equivalent to Java's Exception.printStackTrace()?
提问by Epaga
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?
是否有与 Java 等效的 C# 方法,Exception.printStackTrace()
还是我必须自己编写一些东西,通过 InnerExceptions 工作?
采纳答案by Drew Noakes
Try this:
尝试这个:
Console.WriteLine(ex.ToString());
From http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx:
从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.
ToString 的默认实现获取抛出当前异常的类的名称、消息、对内部异常调用 ToString 的结果以及调用 Environment.StackTrace 的结果。如果这些成员中的任何一个为空,则其值不包含在返回的字符串中。
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.
请注意,在上面的代码中,ToString
不需要调用 ,因为有一个直接接受System.Object
和调用的重载ToString
。
回答by JeeBee
Is there no C# Logging API that can take an Exception as an argument and handle everything for you, like Java's Log4J does?
是否没有 C# Logging API 可以将 Exception 作为参数并为您处理所有事情,就像 Java 的 Log4J 那样?
I.e., use Log4NET.
即,使用 Log4NET。
回答by nbevans
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx
Console.WriteLine(myException.StackTrace);
Console.WriteLine(myException.StackTrace);
回答by Steve Brouillard
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
回答by Jon Skeet
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()
回答by nbevans
Also look at Log4Net... its a port of Log4J to .NET.
还要看看 Log4Net...它是 Log4J 到 .NET 的一个端口。
回答by Ryan Cook
I would like to add: If you want to print the stack outside of an exception, you can use:
我想补充一点:如果你想在异常之外打印堆栈,你可以使用:
Console.WriteLine(System.Environment.StackTrace);
回答by gianlucaparadise
Since @ryan-cook answer didn't work for me, if you want to print the stack trace without having an exception, you can use:
由于@ryan-cook 回答对我不起作用,如果您想打印堆栈跟踪而没有异常,您可以使用:
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
不幸的是,这不能在 PCL 或 .NETStandard 库中完成