views:

127

answers:

2

I need a Log4net wrapper - to be exposed to a number of different components in a large app. I obviously want to retain the class and method name when logging but I would keep away of passing down type etc to my wrapper.

I had a look at this question which is very similar to mine, but it didn't help.

I've seen it done in this other question with smt like the following:

MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
this.log.Debug(methodBase.Name + " : " + message);

This is not ideal since it's not using the out-of-the-box Log4Net functionality.

I'd like to get an idea of how people are doing this before I go for the tangent and come up with something very complicated. Any pointers (links/resources/samples) appreciated!

+1  A: 

I usually add something this... (it's all the functionality I need)

MethodBase.GetCurrentMethod().Name

you could always create a wrapper for Log4Net that takes any params you need (like MethodBase)?

bluevoodoo1
Thanks - I would like to avoid passing down type and methodName.
JohnIdol
A: 

Log4net allows you to access method name for instance like this %method. This is not the fastest operation but if you need to debug something you may very well use it. I believe your question is about the fact that log4net will not output the correct method name if you use a wrapper.

To solve that problem you have to look at how log4net wrote the ILog implementation. This is basically a wrapper around the internal log4net Logger and therefore the very same problem applies to the ILog implementation, too. I did basically the following:

// define a field such as this
private static readonly Type ThisDeclaringType = typeof(MyLogWrapper);

// in constructor. Note: I use the internal Logger!
this.Logger = LogManager.GetLogger(name).Logger;

// I created a WriteLog method that calls the internal logger like this
// you will need to translate to the internal log levels
// message and ex are the items I want to log (ex can be null)
this.Logger.Log(MyLogWrapper.ThisDeclaringType, log4netLevel, message, ex);

Obviously there are some things to do yet, but the important points are mentioned.

Stefan Egli