473,854 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("callingCla ss.CallingMetho d", 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.Refl ection.Blah.Cur rentMethod, someData, someLogCode);

Merry Christmas!

SSG

Dec 24 '06 #1
4 2149
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("callingCla ss.CallingMetho d", 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.Refl ection.Blah.Cur rentMethod, someData, someLogCode);
Log(MethodBase. GetCurrentMetho d().DeclaringTy pe,MethodBase.G etCurrentMethod ().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("callingCla ss.CallingMetho d", 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.Refl ection.Blah.Cur rentMethod, 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()).G etFrame(1).GetM ethod();
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()).G etFrame(1).GetM ethod();
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
2764
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__ is a special case ). The closest to this I could find was the 2002-12-04 posting 'metaclasses and static methods' by Michele Simionato. The posting's example metaclass uses the method's name. I present my own example of automatic method kind...
5
15386
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 the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
5
3447
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
2
1296
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 currently: Catch ex as Exception
5
1333
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
4763
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
4
2888
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
2406
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 me")''' return
0
1114
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. Of course, what you want is the name of the thing chained in front of that. There are and are not ways of getting to that. Dig into the
0
9899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11024
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10740
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.