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);
}
Labels: .Net, Exceptions
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home