Logging is very important and in many cases not given as much attention as it deserves.
When connecting to external datasources, logging becomes crucial.
Whenever data provided by an external source is not as expected, you would like to know what went wrong.

When objects are dumped to the log, a common use is to override the ToString method

    public class Phone
    {
        public Guid Id { get; set; }

        public string Brand { get; set; }

        public override string ToString()
        {
            return string.Format("Phone with ID {0} and Brand {1}", Id, Brand);
        }
    }

This works fine for objects with not a lot of properties.
When the object becomes complex, overriding the ToString method becomes unclear and a lot of work.
Instead, we want to see the data of the entire object without having to do lots of work for this.
Let’s create an interface for dumping objects to string, which we can use for logging purposes.

public interface IObjectDumper
{
    string WriteToString(object objectToDump);
}

Objects can be dumped in several formats. You can pick whatever format you prefer, think of json, xml, ….
I prefer json. I think it is an easy to understand and readable format.
Asp.Net has its own json serializer (JavaScriptSerializer), but I prefer the one from Newtonsoft Json (this package can be downloaded from NuGet):

public class JsonObjectDumper : IObjectDumper
{
   public string WriteToString(object objectToDump)
   {
       return JsonConvert.SerializeObject(objectToDump, Formatting.Indented);
   }
}

The Formatting.Indented makes sure the output is indented.
Dumping a phone object looks as follows:

{
  "Id": "d768bbc4-a1d5-441f-ae14-ebb5bef92b41",
  "Brand": "Google"
}

Using the object dumper with logging would look something like this:

    public class PhoneRepository : IPhoneRepository
    {
        private readonly IObjectDumper _objectDumper;
        private readonly IConnector _connter;
        private readonly IPhonesParser _phonesParser;
        public PhoneRepository(IObjectDumper objectDumper, IConnector connector, IPhonesParser phoneParser)
        {
            _objectDumper = objectDumper;
        }

        public IEnumerable<Phone> GetPhones()
        {
            var phones = new List<Phone>();
            try
            {
                var result = _connector.GetPhones();
                phones = _phonesParser.Parse(result);
            }
            catch (ParseException e)
            {
                Logging.Error(string.Format("Error parsing result for GetPhones with data: {0}", _objectDumper.WriteToString(result)), e);
                throw;
            }
            catch (Exception e)
            {
                Logging.Error("Error retrieving data for GetPhones.", e);
                throw;
            }
            return phones;
        }
    }
The following two tabs change content below.
I'm a software developer from Utrecht. Interested in DDD, continuous delivery, new technologies & frameworks.

Latest posts by Vincent Keizer (see all)