stack trace from the current location in .NET Core

2019-08-10 03:43发布

问题:

How can I constructs a stack trace from the current location?

In .NET 4.5 works this solution:

System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true);
for (int i = 0; i < stackTrace.FrameCount; ++i)
{
    System.Diagnostics.StackFrame frame = stackTrace.GetFrame(i);
    MethodBase callerMethod = frame.GetMethod();
    ...
}

But what is the solution in .NET Core? The constructor public StackTrace(bool fNeedFileInfo) does not exists anymore.

回答1:

UPDATE .NET Standard 2.0 has been released and the constructor is in there as promised.


It seems like this type is at the moment not available. In this post it is mentioned that it will be added to .net standard 2.0.

You could use the System.Diagnostics.StackTrace nuget package which supports .net core 5.0, .net standart 1.3 and more

Or you could use the master branch of .net core.