views:

34

answers:

2

I usually have methods returning IList. I need to log the result coming back in this list.

Is there a better way than looping on the items inside the list and printing them one by one like this?

foreach(string productName in productsNames)
            {
                prodsNames.Append(productName + " ");
            }
Logger.Debug("Loading data for Producers: " + prodsNames);

is there some way of making this easier?

+1  A: 

I would write a wrapper for log4net that exposes a method like this:

void Debug(string message, IList elements);

The implementation could look be as follows (some details are omitted):

public Debug(string message, IList elements)
{
    // this.Logger returns a reference to ILog object
    if (this.Logger.IsDebugEnabled)
    {            
        string logMessage;
        // build the string that you want to write to the log
        this.Logger.Debug(logMessage);      
    }
}

That is just an example: You can design your interface the way that is best suited for you.

Here you can find some ideas what a wrapper might look like.

Stefan Egli
maybe also consider this, if you write a wrapper: http://stackoverflow.com/questions/2049992/when-using-wrapper-how-to-preserve-class-and-method-name-for-log4net-to-log/3448439#3448439
Stefan Egli
A: 

Serialize the list to XML, log the XML string.

Ashley