Class decorator to declare static member (e.g., for log4net)?
Posted
by Ken
on Stack Overflow
See other posts from Stack Overflow
or by Ken
Published on 2010-05-17T21:24:06Z
Indexed on
2010/05/17
21:30 UTC
Read the original article
Hit count: 170
I'm using log4net, and we have a lot of this in our code:
public class Foo {
private static readonly ILog log = LogManager.GetLogger(typeof(Foo));
....
}
One downside is that it means we're pasting this 10-word section all over, and every now and then somebody forgets to change the class name. The log4net FAQ also mentions this alternative possibility, which is even more verbose:
public class Foo {
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
...
}
Is it possible to write a decorator to define this? I'd really like to say simply:
[LogMe] // or perhaps: [LogMe("log")]
public class Foo {
...
}
I've done similar things in other languages, but never a statically-compiled language like C#. Can I define class members from a decorator?
© Stack Overflow or respective owner