Well, the reason is simply to create a generic logging function which
will know (and output) the class/method of its caller to a file or
something.
The method is straightforward, by hooking into the stacktrace.
For non-static classes, getting the classname is even more
straightforward, it is just this.GetType.
However it is not obvious to me how a function in a static class can
work out which class it is in.
Funnily enough the idea of using a static member did cross my mind, but
I really can't be bothered copy/pasting some stupid variable into the
top of all of my static classes unless I really have to. For one,
sooner or later I'll screw it up - believe you me I am *that* bad at
coding; for another there should be a smarter way of doing this.
I am aware that the solution to this problem is likely going to be
something to do with reflection, or maybe using the stack info once
again. But I cannot see how. Any takers?
Jay R. Wren - 31 Jan 2006 18:06 GMT
> Well, the reason is simply to create a generic logging function which
> will know (and output) the class/method of its caller to a file or
[quoted text clipped - 17 lines]
> something to do with reflection, or maybe using the stack info once
> again. But I cannot see how. Any takers?
I think examples of this are found all over the log4net examples page.
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
May be what you want?
I insert
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
at the start of practically all of my classes for use with log4net. I
don't see why this static member should be much different than a static
method.
--
Jay R. Wren