Mark's Stuff

My Foray Into Weblogging. Using this to store interesting items for later review.

Wednesday, June 24, 2009

Tip: Get the name of the calling method

Very handy for using common logging or exception handling routines.

This code is corrected from original article, and added to also get the calling function.

using System.Diagnostics;

void Log(string eventMessage)
{

   Console.WriteLine("Event logged by " + (new StackTrace().GetFrame(1).GetMethod().Name);
Console.WriteLine("Module: " + (new StackTrace().GetFrame(1).GetMethod().Module.Name)



   Console.WriteLine("Event: " + eventMessage);

}



Tips & Tricks for ASP.NET, IIS, and Visual Web Developer : Tip #83: Did you know... You can get the name of the calling method from the stack using reflection?

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home