1

i am trying to output a log of methods and their actions if taken and results into a "MyAppLog_ListView" the columnsHeaders of the lisftview Are

Record#______MethodName__________MethodsOutput

1      ______GetCurrentTime______success(21:10)
2      ______DoSplitString_______faild(not in right Format)
3......................................etc'...

this helps me debuging my program as it's over 1500 lines of code and atleast for me it's getting a little too complex , the question is what is the right way to store methods name in a string

public void MyMethod()
{
      do stuff 

       if (comleted) MyAppLog_ListView_AddRecord(string for the methods name, outputSucces)
       else if (faild) MyAppLog_ListView_AddRecord(string for the methods name, outputFail)


}

how do i get MyMethod().Name to store in a string ?

  • ReEdit :

    As to SidAhmeds Answer :

     public void MyMethodName()
    
        {
              do stuff 
    
             string Mnam = MethodBase.GetCurrentMethod().Name;
               if (comleted) MyAppLog_ListView_AddRecord(Mnam , outputSucces);
               else if (faild) MyAppLog_ListView_AddRecord(Mnam , outputFail);
    
    
        }
    
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • Take a look at this post, it may help : http://stackoverflow.com/questions/2652460/c-sharp-how-to-get-the-name-of-the-current-method-from-code – SidAhmed Aug 28 '12 at 07:00

2 Answers2

2

Or you could use reflection to do it :

System.Reflection.MethodBase.GetCurrentMethod().Name;
SidAhmed
  • 2,332
  • 2
  • 25
  • 46
  • Thanks A Lot . i was looking at the link then after 4 minutes i red it all knew what the answer was then you made second post , it was good to read too , but some times when you want to just get ahead of things , this simple answer is Just perfect . – LoneXcoder Aug 28 '12 at 07:11
1
StackTrace st = new StackTrace();
var methodName = st.GetFrame(0).GetMethod().Name;
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30