Get the name of the currently executing method in dotnet core

14,080

Solution 1

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

Solution 2

Simplest way is to use :

System.Reflection.MethodBase.GetCurrentMethod().Name

Solution 3

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

MethodBase.GetCurrentMethod().Name
Share:
14,080

Related videos on Youtube

undefined
Author by

undefined

I have been increasingly saddened and disheartened by the recent goings on at stack exchange. It is clear to me that we the community are not valued, or given the dignity due our contributions to the success of their network, instead we are given platitudes and non apologies. When the community showed strong opposition they simply deleted the question. Its pretty clear to me we are not their people, we are not loved. As a result I have decided to stop contributing, voting, using jobs, and viewing ads across stack exchange. I do not want to support SE in any way as a company. I am also actively looking for alternative communities in which to participate. If you think that you have a good alternative, email me at staticvoid.co.nz. Why must you destroy those who speak out against you? כִּי רִיב לַיהוָה, עִם-יוֹשְׁבֵי הָאָרֶץ--כִּי אֵין-אֱמֶת וְאֵין-חֶסֶד וְאֵין-דַּעַת אֱלֹהִים, בָּאָרֶץ אָלֹה וְכַחֵשׁ, וְרָצֹחַ וְגָנֹב וְנָאֹף; פָּרָצוּ, וְדָמִים בְּדָמִים נָגָעוּ

Updated on June 04, 2022

Comments

  • undefined
    undefined almost 2 years

    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?

    • Scott Chamberlain
      Scott Chamberlain over 7 years
      Why not just use nameof(YourCurrentMethod)?
    • Scott Chamberlain
      Scott Chamberlain over 7 years
      Also, a more relevent GitHub issue would be github.com/dotnet/corefx/issues/12496
    • undefined
      undefined over 7 years
      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
    • Scott Chamberlain
      Scott Chamberlain over 7 years
      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)
    • Michael Freidgeim
      Michael Freidgeim almost 3 years
  • Lance Larsen - Microsoft MVP
    Lance Larsen - Microsoft MVP over 4 years
    Used to work - but does not work in asp.net core :(
  • CHarmon
    CHarmon about 4 years
    It does not, but this does: System.Reflection.MethodBase.GetCurrentMethod().DeclaringTyp‌​e.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 "<GETMEMBERSASYNC>D__18".
  • Marc Levesque
    Marc Levesque over 2 years
    Doesn't work in an async method, returns "MoveNext".