How to get StackTrace without Exception in Windows

2019-01-26 12:20发布

问题:

I know it was possible to use:

System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();

but that seems to not work anymore, as it needs an Exception-Object.

回答1:

Try Environment.StackTrace. Just remember to be careful with what your logic afterwards is as stacks can change in unpredictable ways. Some discussion here: https://github.com/dotnet/corefx/issues/1420



回答2:

Check out this discussion on .NET Core's github: Where have StackTrace and StackFrame gone?.

This class isn't there in .NET Core, but there's an open task to implement it: Implement System.Diagnostics.StackTrace/StackFrame - just no one actively working on it yet.



回答3:

As a workaround until .Net Core 2.0 will be available you can parse Environment.StackTrace string as suggested in answeres to How to split a stacktrace line into namespace, class, method file and line number? -using RegEx or StackTraceParser