Getting event/method name

G

Guest

is there any object available in c#/asp.net which can give me event name in
that eevnt only. Like if I am in Page_load event, i should get Page_Load or
if I am in Button_click event I should get Button_Click. SO that I can logg
it in my cutom log file with a generic logic.

Thanks
 
E

Eliyahu Goldin

Look at the TraceEventCache.Callstack property and the StackTrace class.
From the MSDN:

The Callstack property gets the call stack from the StackTrace property of
the Environment class. The property value lists method calls in reverse
chronological order. That is, the most recent method call is described
first. One line of stack trace information is listed for each method call on
the stack. For more information, see StackTrace.


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
G

Guest

Hi,
If you're writing a tracing and logging component you may well want to pass
the current method name to your logging component. Rather than hard code it
(then forget to change it when you change the method name) you can use
reflection and the MethodBase class to retrieve the name.

System.Reflection.MethodBase currentMethod =
System.Reflection.MethodBase.GetCurrentMethod();
System.Diagnostics.Debug.WriteLine(currentMethod.Name);
System.Diagnostics.Debug.WriteLine(currentMethod.DeclaringType.Name);
System.Diagnostics.Debug.WriteLine(currentMethod.DeclaringType.Namespace);

ref:http://idunno.org/archive/2004/12/03/163.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top