I have a project where in my error reporting I want to send a complete error report via email. I’m catching the exception and passing all the information to another method that is responsible for sending an email to support.
I’m adding important custom values to the Exception.Data object. However, I’m unable to figure out what type I need to use in my method for the entire Data object to be passed.
Here is my code:
catch (Exception exc)
{
exc.Data.Add("ID", id);
emailConnector.sendErrorEmail(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), exc.Message, exc.Data, exc.StackTrace);
}
I’m passing exc.Data to the sendErrorEmail method so in the parameter declarations I have:
public void sendErrorEmail(string origin, string error_message, IDictionary<object, object> data, string stack)
But I can’t seem to get the type declaration correct for Exception.Data. I prefer to keep the variables and structure the same (no new variables) because I currently have dozens of methods calling sendErrorEmail in the Catch block.
What type should I be using or is there an easier way to get this Exception.Data passed correctly with the extra pertinent information I included in it?
Thanks
Looking at the help page it seems that your problem is that the type of
exc.DataisIDictionarywhereas you are declaring it asIDictionary<object, object>.You should have had an error message telling you the type that it was having trouble converting between…
Also unless this method is being used for things other than exceptions I would go with the suggestion in comments to pass the whole exception through and disect it afterwards rather than passing through multiple properties.