Using Exception.Data

How have you used the Exception.Data property in C# projects that you've worked on?

I'd like answers that suggest a pattern, rather than those that are very specific to your app.


The exception logger I use has been tweaked to write out all the items in the Data collection. Then for every exception we encounter that we cannot diagnose from the exception stack, we add in all the data in that function's scope, send out a new build, and wait for it to reoccur.

I guess we're optimists in that we don't put it in every function, but we are pessimists in that we don't take it out once we fix the issue.


Since none of the answers include any code. Something that might useful as an addition to this question is how to actually look at the .Data dictionary. Since it is not a generic dictionary and only returns IDictionary

foreach(var kvp in exception.Data) the type of kvp will actually be object unhelpfully. However from the MSDN there's an easy way to iterate this dictionary:

foreach (DictionaryEntry de in e.Data)
    Console.WriteLine("    Key: {0,-20}      Value: {1}", 
                             "'" + de.Key.ToString() + "'", de.Value);

I don't really know what the format argument , -20 would mean, maybe Take(20)? Digressing... this code can be very helpful in a common error logger to unwind this data. A more complete usage would be similar to:

var messageBuilder = new StringBuilder();

do
{                
    foreach (DictionaryEntry kvp in exception.Data)
        messageBuilder.AppendFormat("{0} : {1}\n", kvp.Key, kvp.Value);

    messageBuilder.AppendLine(exception.Message);


} while ((exception = exception.InnerException) != null);

return messageBuilder.ToString();