470,631 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

Get the calling type

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
Dec 20 '07 #1
1 2028
Peter K wrote:
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?

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Dec 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nick Flandry | last post: by
7 posts views Thread by Christian Wilhelm | last post: by
3 posts views Thread by Mike | last post: by
3 posts views Thread by Jerome Cohen | last post: by
15 posts views Thread by =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
4 posts views Thread by raghuvendra | last post: by
6 posts views Thread by Ole Nielsby | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.