How To: Obtain Method Name Programmatically For Tracing
I am not a fan of hard-coding method names in exception or trace messages. Here is a utility method to allow access to method name at runtime:
public static void TraceContext(string messageFormat) { Trace.WriteLine(string.Format(messageFormat, new System.Diagnostics.StackFrame(1).GetMethod().Name)); }
If I call the method above from inside another method:
protected void Application_AuthorizeRequest(object sender, EventArgs e) { Tools.TraceContext("Inside of {0} event handler"); }
"Inside of Application_AuthorizeRequest event handler"