Skip to content

Instantly share code, notes, and snippets.

@bozhink
Created August 10, 2017 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bozhink/0b6159bd1767aae2aeb9e432ca39704c to your computer and use it in GitHub Desktop.
Save bozhink/0b6159bd1767aae2aeb9e432ca39704c to your computer and use it in GitHub Desktop.
Get calling mathod name C#
internal class Helpers
{
internal string GetCallingMethodName()
{
string result = "unknown";
StackTrace trace = new StackTrace(false);
for (int i = 0; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
MethodBase method = frame.GetMethod();
Type dt = method.DeclaringType;
if (!typeof(ILogger).IsAssignableFrom(dt) && method.DeclaringType.Namespace != "DiagnosticsLibrary")
{
result = string.Concat(method.DeclaringType.FullName, ".", method.Name);
break;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment