C# Logging Data Technique
The last few weeks I have been having to conduct some C# code reviews. While doing so, I came across a great, flexible C# logging data technique using just String.Format(), a List<string>(), and a Dictionary<string,string>() object.
C# logging data code I typically come across is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//How the C# logging code was private static string UpdateCommentsStatus(Dictionary<string,string> sourceItem) { return string.Format( @"==================================== The following information is retained: ==================================== Title: {0} ------------------------------------ Location: {1} ------------------------------------ Distance: {2} ------------------------------------ Miscellaneous Info: {3} ==================================== End retained information. ==================================== ", sourceItem["Title"], sourceItem["Location"], sourceItem["Distance"], sourceItem["Miscellaneous Info"]); } |
The above code presents challenges. For instance:
- What if there’s a requirement to not display the “Distance:” section if there’s no data?
- What if from one part of the code the “Title:” section has to be called something else like “Caption:”? There’s no easy way around that without some conditional logic in the current model.
- What if the ordering needs to be dynamic based on another set of conditions?
After the code review, here’s the technique that was implemented using List<string>. Ready?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//--------------------------------------------- //Here is an improved method of logging the same information. //This code is adapted from Marat Bakirov: //http://blogs.msdn.com/mbakirov private static string UpdateCommentsStatus(Dictionary<string,string> sourceItem) { const string template = @"==================================== The following information is retained: ==================================== {0} ==================================== End retained information. ==================================== "; string separator = @" --------- "; string itemFormat = @"{0}: {1}"; List<string> results = new List<string>(); results.Add(string.Format(itemFormat, "Title:", sourceItem["Title"])); results.Add(string.Format(itemFormat, "Location:", sourceItem["Location"])); results.Add(string.Format(itemFormat, "Distance:", sourceItem["Distance"])); results.Add(string.Format(itemFormat, "Miscellaneous Info:", sourceItem["Miscellaneous Info"])); var resultString = string.Format(template, string.Join(separator, results)); return resultString; } |
Look at the difference in code, have a stop, and think as to why the latter would be preferred before reading further.
Ready? There are spoilers from this point!
Some of the advantages to using the latter C# logging data technique:
-
You can change the heading text dynamically, more easily:
results.Add(string.Format(itemFormat, (sourceItem["Title"].ToLower() != "new" ? "Locations:" : "Location:"), sourceItem["Location"]));
- You can have parts not displayed if there’s no data:
if (!String.IsNullOrWhiteSpace(sourceItem["Title"]))
{
results.Add(string.Format(itemFormat, "Title:", sourceItem["Title"]));
}
- You have a flexible way to reorder the output sections just by changing the calling order of results.Add()
Winning! 🙂
Do you have any tips or techniques to share? Leave them in the comments below or send me a message so we can let others know. 🙂