DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Sat, 18 Aug 2007 06:21:22 -0700,    posted on: microsoft.public.dotnet.framework        back       

Thread Index
  1    ssg31415926
          2    Morten Wennevik [C# MVP]
                 3    GlennDoten
                 4    Yaron Karni


Getting current method info for logging   
Is there any way to get the name of the method that called the current
method?

Also, is there any way to get the values of the parameters passed into
the method, along with the parameter names?

I want to write a method that I can call at the start of each method
for logging purposes.
It'll check if the logging level is high enough, and if so, record the
method name and each parameter name and value.

I realise I can use reflection to get the name of the current method
and the names of the parameters.  How can I get the name of the method
that called the current method?  Or is the only way to do this to pass
System.Reflection.MethodBase.GetCurrentMethod() as a parameter?
And how can I get the values of the parameters?  I was hoping there'd
be a Key-Value collection containing them but can't find anything.
Failing that, is there a way to say 'give me the value of the
parameter named in this string'?
Date:Sat, 18 Aug 2007 06:21:22 -0700   Author:  

Re: Getting current method info for logging   
On Sat, 18 Aug 2007 15:21:22 퍭, ssg31415926  wrote:


> Is there any way to get the name of the method that called the current> method?
>
> Also, is there any way to get the values of the parameters passed into> the method, along with the parameter names?
>
> I want to write a method that I can call at the start of each method
> for logging purposes.
> It'll check if the logging level is high enough, and if so, record the> method name and each parameter name and value.
>
> I realise I can use reflection to get the name of the current method
> and the names of the parameters.  How can I get the name of the method> that called the current method?  Or is the only way to do this to pass> System.Reflection.MethodBase.GetCurrentMethod() as a parameter?
> And how can I get the values of the parameters?  I was hoping there'd
> be a Key-Value collection containing them but can't find anything.
> Failing that, is there a way to say 'give me the value of the
> parameter named in this string'?
>
>


Yes, you can use System.Diagnostics.StackFrame information to find what method called this method, or what method called the last method and so on.

StackFrame frame = new StackFrame(1);
MessageBox.Show(frame.GetMethod().Name); // Shows the name of the calling method

new StackFrame(2).GetMethod() would get the method calling the method calling this method, and so on.


-- Happy coding!
Morten Wennevik [C# MVP]
Date:Sat, 18 Aug 2007 15:52:39 +0200   Author:  

Re: Getting current method info for logging   
Morten Wennevik [C# MVP] wrote:

> On Sat, 18 Aug 2007 15:21:22 +0200, ssg31415926  wrote:
> 
>> Is there any way to get the name of the method that called the current
>> method?
>>
>> Also, is there any way to get the values of the parameters passed into
>> the method, along with the parameter names?
>>
>> I want to write a method that I can call at the start of each method
>> for logging purposes.
>> It'll check if the logging level is high enough, and if so, record the
>> method name and each parameter name and value.
>>
>> I realise I can use reflection to get the name of the current method
>> and the names of the parameters.  How can I get the name of the method
>> that called the current method?  Or is the only way to do this to pass
>> System.Reflection.MethodBase.GetCurrentMethod() as a parameter?
>> And how can I get the values of the parameters?  I was hoping there'd
>> be a Key-Value collection containing them but can't find anything.
>> Failing that, is there a way to say 'give me the value of the
>> parameter named in this string'?
>>
>>
> 
> Yes, you can use System.Diagnostics.StackFrame information to find what method called this method, or what method called the last method and so on.
> 
> StackFrame frame = new StackFrame(1);
> MessageBox.Show(frame.GetMethod().Name); // Shows the name of the calling method
> 
> new StackFrame(2).GetMethod() would get the method calling the method calling this method, and so on.
> 
> 


It can get a little trickier if your actual logging method has 
overloads. Say you have this:

public static void Log(string message)
{
	Log(message, null);
}

public static void Log(string message, Timer t)
{
	// Do the actual logging here. You'll have to
	// "walk up the stack" to a method that is not
	// "Log" to find out who "really" called us.
	// Otherwise, if someone calls the first
	// overload to Log then StackFrame(1) here
	// would tell us Log called us and that's
	// not really what you want to know.
}

I posted some sample code for this just this week:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/041a63f49c624609

HTH.

-- 
-glenn-
Date:Sat, 18 Aug 2007 10:09:53 -0400   Author:  

Re: Getting current method info for logging   
Here a code (for instance ), Get a method name and other data stuff :

// now frameIndex is the first 'user' caller frame
StackFrame aFrame = st.GetFrame(frameIndex);

if (aFrame != null)
{
	System.Reflection.MethodBase method = aFrame.GetMethod();

	if (method != null)
	{
		m_methodName =  method.Name;
		if (method.DeclaringType != null)
		{
			m_className = method.DeclaringType.FullName;
		}
	}
                m_fileName = aFrame.GetFileName();
                m_lineNumber = 
aFrame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo);

-- 
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/


"GlennDoten" wrote:


> Morten Wennevik [C# MVP] wrote:
> > On Sat, 18 Aug 2007 15:21:22 +0200, ssg31415926  wrote:
> > 
> >> Is there any way to get the name of the method that called the current
> >> method?
> >>
> >> Also, is there any way to get the values of the parameters passed into
> >> the method, along with the parameter names?
> >>
> >> I want to write a method that I can call at the start of each method
> >> for logging purposes.
> >> It'll check if the logging level is high enough, and if so, record the
> >> method name and each parameter name and value.
> >>
> >> I realise I can use reflection to get the name of the current method
> >> and the names of the parameters.  How can I get the name of the method
> >> that called the current method?  Or is the only way to do this to pass
> >> System.Reflection.MethodBase.GetCurrentMethod() as a parameter?
> >> And how can I get the values of the parameters?  I was hoping there'd
> >> be a Key-Value collection containing them but can't find anything.
> >> Failing that, is there a way to say 'give me the value of the
> >> parameter named in this string'?
> >>
> >>
> > 
> > Yes, you can use System.Diagnostics.StackFrame information to find what method called this method, or what method called the last method and so on.
> > 
> > StackFrame frame = new StackFrame(1);
> > MessageBox.Show(frame.GetMethod().Name); // Shows the name of the calling method
> > 
> > new StackFrame(2).GetMethod() would get the method calling the method calling this method, and so on.
> > 
> > 
> 
> It can get a little trickier if your actual logging method has 
> overloads. Say you have this:
> 
> public static void Log(string message)
> {
> 	Log(message, null);
> }
> 
> public static void Log(string message, Timer t)
> {
> 	// Do the actual logging here. You'll have to
> 	// "walk up the stack" to a method that is not
> 	// "Log" to find out who "really" called us.
> 	// Otherwise, if someone calls the first
> 	// overload to Log then StackFrame(1) here
> 	// would tell us Log called us and that's
> 	// not really what you want to know.
> }
> 
> I posted some sample code for this just this week:
> 
> http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/041a63f49c624609
> 
> HTH.
> 
> -- 
> -glenn-
> 
Date:Mon, 20 Aug 2007 03:38:01 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us