1

I'm using this helper to resolve the name of the method that is currently being executed for logging purposes.

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static string GetCurrentMethod()
    {
        StackTrace st = new StackTrace();
        StackFrame sf = st.GetFrame(1);

        return sf.GetMethod().Name;
    }

This is the string that is returned <Frequency>b__46

" What does the b__46 mean? And is there a way to just retrieve the word "Frequency?"

This is calling the helper.

    return ProxyCallWrapper.Execute<bool, IBackendJob>((backend, header) =>
    {
        header.CorrelationID = CorrelationID;
        logger.LogInfo(string.Format("### BSL CALL from {0} by {1} : CorrelationID: {2}", this.ToString(), GetCurrentMethod() ,header.CorrelationID));
        return backend.AddJob(header, jobId);
    });
g5insider
  • 109
  • 9

2 Answers2

4

The method is probably being called from a lambda expression. The C# compiler actually converts lambdas to hidden methods inside your class. These methods have special compiler-generated names, like the <Frequency>b__46 you're seeing. I think you'll find if you look at GetFrame(2) you'll see the name you expect. Your function could ignore lambdas by looping up the stack until it finds a valid method name (you can check the method descriptor's IsSpecialName property for that).

The compiler also generates hidden methods with special names for auto property getters and setters, event add/remove handlers, and some other cases (you won't encounter these since these auto-generated methods can't call your GetCurrentMethod()). But note also that manual property getters and setters have "special" names like get_PropertyName(), and you may see those if you have a property whose code calls GetCurrentMethod().

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • I edited my question to include the code that calls the helper. It is a lambda statement. I will try GetFrame(2) and tell you what I find. A brief explanation of what the numbers inside of GetFrame() would be appreciated. – g5insider Feb 04 '14 at 22:30
  • @g5insider The argument to `GetFrame()` is just the index of the frame you want. A value of 1 means you're getting the caller's stack frame; 2 means you're getting the caller's caller's frame, and so on, all the way back to `main()` (or the thread / managed entry point). – TypeIA Feb 04 '14 at 22:32
  • @g5insider Because you're passing the lambda to `ProxyCallWrapper.Execute()`. This is the nature of lambda expressions; they aren't invoked immediately, they're just objects. Your code's calling `Execute()` which is calling your lambda which is calling `GetCurrentMethod()`. – TypeIA Feb 04 '14 at 22:38
  • Let my try GetFrame(3) – g5insider Feb 04 '14 at 22:40
  • GetFrame(3) is what I'm looking for. I will have to unit test this but I think it will do. – g5insider Feb 04 '14 at 22:43
  • 1
    GetFrame(3) will only work in the specific scenario in your question. Do you understand that? – Blorgbeard Feb 05 '14 at 00:33
  • I've just manually added the names instead of using this method of retrieving it from the stack trace. GetFrame(3) as @Biorgbeard stated will only work in some cases and not all. I appreciate the help from everyone. Thanks! – g5insider Feb 12 '14 at 14:56
-1

<Frequency>b__46 is the name of the method that called GetCurrentMethod().

Since you are calling the method from a lambda, you have to accept that the name of the lambda is beyond your control.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490