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'?

Re: Getting current method info for logging by Morten

Morten
Sat Aug 18 08:52:39 CDT 2007

On Sat, 18 Aug 2007 15:21:22 +0200, ssg31415926 <newsjunkmail@gmail.com>=
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 =3D new StackFrame(1);
MessageBox.Show(frame.GetMethod().Name); // Shows the name of the callin=
g method

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


-- =

Happy coding!
Morten Wennevik [C# MVP]

Re: Getting current method info for logging by GlennDoten

GlennDoten
Sat Aug 18 09:09:53 CDT 2007

Morten Wennevik [C# MVP] wrote:
> On Sat, 18 Aug 2007 15:21:22 +0200, ssg31415926 <newsjunkmail@gmail.com> 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-

Re: Getting current method info for logging by yaron

yaron
Mon Aug 20 05:38:01 CDT 2007

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 <newsjunkmail@gmail.com> 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-
>