Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get stacktrace inside c# method

I want to implement universal logger which help me to see call stack of the methods.

I know there are some methods from System.Diagnostic but they were introduced in .net 4.0 and I'm afraid it will not work on xamarin or .net core or something like that. So I want to have more universal solution.

Another problem is async\await which introduce some mess.

I through about passing additional parameter in each method which store some context and help me to determinate call stack, but this solution is a bit complex.

Also I can read thread stack memory using unsafe code and check call stack by myself but it is not reliable.

Are there any other solution ?

like image 384
Neir0 Avatar asked May 08 '16 17:05

Neir0


People also ask

How to get the current stack trace information using environment class?

To get current stack trace information using the Environment class – we use the StackTrace property. This property returns a string value that contains current stack trace information. The source code to get the current stack trace information using Environment class is given below. The given program is compiled and executed successfully.

How do I get a stack trace in Visual Studio?

Use the following line of code to obtain a stack trace at any point in your application: The variable currentStackTrace now contains the stack trace at the location where this line of code was executed. A good use of the Solution is tracking down stack overflow problems.

What is a stacktrace in C++?

A stacktrace is an approximate representation of an invocation sequence and consists of stacktrace entries. A stacktrace entry represents an evaluation in a stacktrace. It is represented by std::stacktrace_entry in the C++ standard library. An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory.

What is currentstacktrace in Visual Studio?

The variable currentStackTrace now contains the stack trace at the location where this line of code was executed. A good use of the Solution is tracking down stack overflow problems. You can obtain the current stack trace at various points in your application and then calculate the stack depth.


1 Answers

You could just use Environment.StackTrace. That has been part of the Framework since the very beginning.

Environment.StackTrace will return the complete stacktrace (including the call to Environment.StackTrance itself) as a line separated string.

Something like this:

at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at WpfApplication2.MainWindow.GetStack(Int32 removeLines)
at WpfApplication2.MainWindow.Button_Click(Object sender, RoutedEventArgs e)
...
at System.Threading.ThreadHelper.ThreadStart()

All you need to do is split/parse/format it, whatever you want to do with it.

Since you'll be using this from within your own classes, remember to remove the newest X lines.

This code should work everywhere since it's deliberately low-level.

private static string[] GetStack(int removeLines)
{
    string[] stack = Environment.StackTrace.Split(
        new string[] {Environment.NewLine},
        StringSplitOptions.RemoveEmptyEntries);

    if(stack.Length <= removeLines)
        return new string[0];

    string[] actualResult = new string[stack.Length - removeLines];
    for (int i = removeLines; i < stack.Length; i++)
        // Remove 6 characters (e.g. "  at ") from the beginning of the line
        // This might be different for other languages and platforms
        actualResult[i - removeLines] = stack[i].Substring(6);

    return actualResult;
}
like image 80
Manfred Radlwimmer Avatar answered Nov 15 '22 06:11

Manfred Radlwimmer