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?
Edit: Heh. I'm a Lisp programmer. I appreciate the suggestions to switch languages, but really, if I was going to switch languages for better metaprogramming capabilities, I'd go all the way to Lisp instead of being half-assed about it. Unfortunately using a different language isn't an option on this project.