28

I want to get the name of the currently executing method in a dotnet core application.

There are lots of examples of how to do this with regular c# eg

However the apis for both methods appear not to be there in core yet (see https://github.com/dotnet/corefx/issues/1420)

Is there another way I can get the executing method name in .net core?

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • 1
    Why not just use `nameof(YourCurrentMethod)`? – Scott Chamberlain Dec 13 '16 at 01:34
  • Also, a more relevent GitHub issue would be https://github.com/dotnet/corefx/issues/12496 – Scott Chamberlain Dec 13 '16 at 01:35
  • `nameof(YourCurrentMethod)` isn't much better than just using a string, thanks for the issue link looks like that method is also due in a future release. Maybe the answer is just not possible atm – undefined Dec 13 '16 at 01:43
  • 9
    No, its a lot better than using a string. If you rename the function the `nameof(YourCurrentMethod)` gets updated too if you use the rename tools (and a compiler error to remind you to fix it if you don't) – Scott Chamberlain Dec 13 '16 at 02:05
  • Does this answer your question? [Can you use reflection to find the name of the currently executing method?](https://stackoverflow.com/questions/44153/can-you-use-reflection-to-find-the-name-of-the-currently-executing-method) – Michael Freidgeim Jun 21 '21 at 07:35

4 Answers4

37

CallerMemberNameAttribute Allows you to obtain the method or property name of the caller to the method.

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
//  message: Something happened.
//  member name: DoProcessing
//  source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
//  source line number: 31
feiyun0112
  • 761
  • 5
  • 7
4

Simplest way is to use :

System.Reflection.MethodBase.GetCurrentMethod().Name

Akarsha
  • 163
  • 1
  • 7
  • 2
    Used to work - but does not work in asp.net core :( – Lance Larsen - Microsoft MVP Sep 11 '19 at 19:53
  • 3
    It does not, but this does: System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name . I should note it does not return the exact method name, but it's pretty close and for my needs, sufficiently unique. For example, I have a method called GetMembersAsync() it will return a value of "D__18". – CHarmon Apr 17 '20 at 18:40
2

In .Net Core 3.1, to get the current method name, use:

MethodBase.GetCurrentMethod().Name
Rohin Tak
  • 489
  • 4
  • 8
1

Tested in NetCore 6, works for Async and sync methods:

var FullName = methodBase.ReflectedType.FullName;
string LoggedMethodName;

if (FullName.Contains("async", StringComparison.InvariantCultureIgnoreCase))
{
    LoggedMethodName = FullName.Replace("+<", ".").Substring(0, FullName.IndexOf(">") - 1);
}
else
{
    LoggedMethodName = FullName + "." + methodBase.Name;
}

So if I have a Class in a given Namespace1 exposing MethodX, I'll get:

"Namespace1.Class.MethodX"
Francesco B.
  • 2,729
  • 4
  • 25
  • 37