PC Review


Reply
Thread Tools Rate Thread

Can a method know the name of the calling class & method?

 
 
ssg31415926
Guest
Posts: n/a
 
      24th Dec 2006
I want to write a logging method. I want it to log the name of the
calling class and method. Is there any way to do this? I presume
it'll use Reflection but it's not an area I've used much.a

Alternatively, is there a piece of code I can use to pass in the name
of the method, so that I can copy and paste the calling code and don't
have to remember to change a hard coded method name.

I could code this:

Log("callingClass.CallingMethod", someData, someLogCode);

but I'd rather code this:

Log(someData, someLogCode); and have it work out what the calling
method was.

or at least, this:

Log(System.Reflection.Blah.CurrentMethod, someData, someLogCode);

Merry Christmas!

SSG

 
Reply With Quote
 
 
 
 
Tim Van Wassenhove
Guest
Posts: n/a
 
      24th Dec 2006
ssg31415926 schreef:
> I want to write a logging method. I want it to log the name of the
> calling class and method. Is there any way to do this? I presume
> it'll use Reflection but it's not an area I've used much.a
>
> Alternatively, is there a piece of code I can use to pass in the name
> of the method, so that I can copy and paste the calling code and don't
> have to remember to change a hard coded method name.
>
> I could code this:
>
> Log("callingClass.CallingMethod", someData, someLogCode);
>
> but I'd rather code this:
>
> Log(someData, someLogCode); and have it work out what the calling
> method was.
>
> or at least, this:
>
> Log(System.Reflection.Blah.CurrentMethod, someData, someLogCode);


Log(MethodBase.GetCurrentMethod().DeclaringType,MethodBase.GetCurrentMethod().Name);



--
Tim Van Wassenhove <url:http://www.timvw.be/>
 
Reply With Quote
 
Duggi
Guest
Posts: n/a
 
      24th Dec 2006
Use log4Net :-) Its a well known log utility. (open source)

Once you use it, you will never try to write a log class.

Thanks
-Srinivas.


ssg31415926 wrote:
> I want to write a logging method. I want it to log the name of the
> calling class and method. Is there any way to do this? I presume
> it'll use Reflection but it's not an area I've used much.a
>
> Alternatively, is there a piece of code I can use to pass in the name
> of the method, so that I can copy and paste the calling code and don't
> have to remember to change a hard coded method name.
>
> I could code this:
>
> Log("callingClass.CallingMethod", someData, someLogCode);
>
> but I'd rather code this:
>
> Log(someData, someLogCode); and have it work out what the calling
> method was.
>
> or at least, this:
>
> Log(System.Reflection.Blah.CurrentMethod, someData, someLogCode);
>
> Merry Christmas!
>
> SSG


 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      24th Dec 2006
ssg31415926 wrote:
> I want to write a logging method. I want it to log the name of the
> calling class and method. Is there any way to do this? I presume
> it'll use Reflection but it's not an area I've used much.a


> but I'd rather code this:
>
> Log(someData, someLogCode); and have it work out what the calling
> method was.


MethodBase m = (new StackTrace()).GetFrame(1).GetMethod();
string classname = m.DeclaringType.Name;
string methodname = m.Name;

but I have serious doubt that performance will be good.

Arne
 
Reply With Quote
 
ssg31415926
Guest
Posts: n/a
 
      24th Dec 2006
Thank you all for your suggestions. I'll have a look at each and see
which fits best.

Regards

SSG

Arne Vajhøj wrote:
> ssg31415926 wrote:
> > I want to write a logging method. I want it to log the name of the
> > calling class and method. Is there any way to do this? I presume
> > it'll use Reflection but it's not an area I've used much.a

>
> > but I'd rather code this:
> >
> > Log(someData, someLogCode); and have it work out what the calling
> > method was.

>
> MethodBase m = (new StackTrace()).GetFrame(1).GetMethod();
> string classname = m.DeclaringType.Name;
> string methodname = m.Name;
>
> but I have serious doubt that performance will be good.
>
> Arne


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling derived class method =?Utf-8?B?TWlrZQ==?= Microsoft Dot NET 2 2nd Dec 2005 09:36 PM
How to call a method in the calling class? Lakesider Microsoft C# .NET 3 7th Nov 2005 12:58 PM
Calling a parent class method =?Utf-8?B?LSB2aGFubmFr?= Microsoft C# .NET 1 29th Jun 2005 06:01 AM
calling class method =?Utf-8?B?aHV6eg==?= Microsoft ASP .NET 5 5th Nov 2004 10:28 AM
calling class method =?Utf-8?B?aHV6eg==?= Microsoft ASP .NET 4 25th Oct 2004 04:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:56 PM.