473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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

Dec 24 '06 #1
4 2119
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,Me thodBase.GetCurrentMethod().Name);

--
Tim Van Wassenhove <url:http://www.timvw.be/>
Dec 24 '06 #2
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
Dec 24 '06 #3
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
Dec 24 '06 #4
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
Dec 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: Jim Bancroft | last post by:
Hi all, I'm writing an exception handler for one of my VB.Net methods and wondered how best to dynamically put the class and method name in my message string. My code looks like this...
5
by: Terry Olsen | last post by:
Can I have an Event & Method with the same name in the same class? I want to have a "Close" method to close a socket and a "Close" event to be raised when the socket is closed.
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
4
by: mfc | last post by:
How do i get the type info in a static method? for instance in the code below is it possible for the Method to get the type to know what type was used to call Method? thanks class C {
21
by: John Henry | last post by:
Hi list, I have a need to create class methods on the fly. For example, if I do: class Dummy: def __init__(self): exec '''def method_dynamic(self):\n\treturn self.method_static("it's...
0
by: Emile van Sebille | last post by:
mercado mercado wrote: Well, the class name is in __class__ so changing 'print x' to 'print __class__' will show foo in both cases, which is in fact the name of the class which is calling it....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.