Get the calling type

P

Peter K

Hi

is there a way to determine "the calling type" in c#?

I have found several places on the web with examples using StackTrace, and
just as many rebuttals of the presented method - stating that it can't be
relied on to be 100% accurate due to possible optimisations at runtime.

I have also looked at the StackFrame class, but I guess the same
limitations exist here.

For example:
StackFrame stFrame = new StackFrame(1, false)
gives me the previous stack frame.

I am using .net 3.5.

The reason I want to do this is involved with the "logging application
block". I want to make a simple wrapper for this, to ensure that whenever
we try to log we use a consistent "catetory name" - and in this case we
always want the category name to be the namespace of the class wishing to
log. (BTW any good links to information about the logging application block
would be appreciated (especially code to wrap it to make it easier to use).
I am used to, and extremely happy with, log4net - but in the project I am
working on we must use microsoft's logging).


Thanks,
Peter
 
L

Lasse Vågsæther Karlsen

Peter said:
Hi

is there a way to determine "the calling type" in c#?

I have found several places on the web with examples using StackTrace, and
just as many rebuttals of the presented method - stating that it can't be
relied on to be 100% accurate due to possible optimisations at runtime.

I have also looked at the StackFrame class, but I guess the same
limitations exist here.

For example:
StackFrame stFrame = new StackFrame(1, false)
gives me the previous stack frame.

I am using .net 3.5.

The reason I want to do this is involved with the "logging application
block". I want to make a simple wrapper for this, to ensure that whenever
we try to log we use a consistent "catetory name" - and in this case we
always want the category name to be the namespace of the class wishing to
log. (BTW any good links to information about the logging application block
would be appreciated (especially code to wrap it to make it easier to use).
I am used to, and extremely happy with, log4net - but in the project I am
working on we must use microsoft's logging).


Thanks,
Peter

Could you integrate logging methods into the base class for your other
classes, and that this method uses GetType() to get hold of type
information for the class you're in when you call the logging methods?
 
F

Family Tree Mike

Peter K said:
Hi

is there a way to determine "the calling type" in c#?

I have found several places on the web with examples using StackTrace, and
just as many rebuttals of the presented method - stating that it can't be
relied on to be 100% accurate due to possible optimisations at runtime.

I have also looked at the StackFrame class, but I guess the same
limitations exist here.

For example:
StackFrame stFrame = new StackFrame(1, false)
gives me the previous stack frame.

I am using .net 3.5.

The reason I want to do this is involved with the "logging application
block". I want to make a simple wrapper for this, to ensure that whenever
we try to log we use a consistent "catetory name" - and in this case we
always want the category name to be the namespace of the class wishing to
log. (BTW any good links to information about the logging application block
would be appreciated (especially code to wrap it to make it easier to use).
I am used to, and extremely happy with, log4net - but in the project I am
working on we must use microsoft's logging).


Thanks,
Peter

I cannot imagine optimization changing the class calling tree down to your
routine. Surely lines are reordered, but from what I have seen, the line
numbers are accurate, as are the class and method names in the stack trace.
 
J

Jon Skeet [C# MVP]

Family Tree Mike said:
I cannot imagine optimization changing the class calling tree down to your
routine. Surely lines are reordered, but from what I have seen, the line
numbers are accurate, as are the class and method names in the stack trace.

Suppose we have the following call stack:

Logger.Log(...)
SmallMethods.DoSomething(...)
OtherThings.BigStuff(...)

If the JIT compiler inlines SmallMethods.DoSomething(...) then the
stack trace will miss out that entry.
 
F

Family Tree Mike

Jon Skeet said:
Suppose we have the following call stack:

Logger.Log(...)
SmallMethods.DoSomething(...)
OtherThings.BigStuff(...)

If the JIT compiler inlines SmallMethods.DoSomething(...) then the
stack trace will miss out that entry.

Interesting, and thanks!
 
A

Arne Vajhøj

Peter said:
is there a way to determine "the calling type" in c#?

I have found several places on the web with examples using StackTrace, and
just as many rebuttals of the presented method - stating that it can't be
relied on to be 100% accurate due to possible optimisations at runtime.

I have also looked at the StackFrame class, but I guess the same
limitations exist here.

For example:
StackFrame stFrame = new StackFrame(1, false)
gives me the previous stack frame.

I am using .net 3.5.

The reason I want to do this is involved with the "logging application
block". I want to make a simple wrapper for this, to ensure that whenever
we try to log we use a consistent "catetory name" - and in this case we
always want the category name to be the namespace of the class wishing to
log.

I belive there are only two ways:
- the StackFrame/StackTrace based methods
- using some kind of AOP to inject information

If you use a single static constructor per namespace, then
performance should not be a problem for StackFrame/StackTrace.

You still have the risk of optimization, but I would say that if
N1.C1.M2 get inlined in N2.C2.M2, then it is running in the N2
context and instructions are likely so reordered that it will be
hard to say what is M1 and what is M2.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top