I have an ASP.NET MVC application and would like to log the names of all the involved methods, for certain user flows on the website.
The following code does that, but it stops at the root/first triggered method:
public string GetStackTraceMethods()
{
List<string> stackTraceMethods = new List<string>();
StackTrace stackTrace = new StackTrace();
string methodName = string.Empty;
// These are the default/framework methods that I do not want to log. So, excluding them.
List<string> methodNamesToIgnore = new List<string> {
"GetStackTraceMethods", "MoveNext", "Start",
"lambda_method", "BuildProduct", "BuildProducts",
"b__2", ".ctor", "ToList"
};
foreach (StackFrame frame in stackTrace.GetFrames())
{
methodName = frame.GetMethod().Name;
if (methodName == "InvokeActionMethod")
{
break;
}
else if (methodNamesToIgnore.Any(x => x == methodName))
{
continue;
}
else
{
stackTraceMethods.Add(frame.GetMethod().Name);
}
}
return string.Join(" < ", stackTraceMethods);
}
This returns a result like this:
Some_Service_Method_2 < Some_Service_Method_1 < Controller_Method
Is it possible to get the controller's name also from the stack trace, so that I can include that in the list?
UPDATE: the GetStackTraceMethods()
method is placed in the bottom level of the flow. i.e it is placed in the same class as Some_Service_Method_2
@Chetan's comment worked. It gives the class name of that method.