Hi Ron,
Have you seen any examples on how to do that?
Here's a function I wrote for our applications, which I use in conjunction
with an error-handling utility. Remarks follow:
private static void GetExceptionDetails(Exception ex, ref StringBuilder
sb)
{
int i;
sb.Append(sb.Length == 0 ? "" : nl);
Type t = ex.GetType();
Type[] types = new Type[] {
typeof(WebException),
typeof(SqlException),
typeof(SmtpException),
typeof(SmtpFailedRecipientException),
typeof(SmtpFailedRecipientsException),
typeof(ConfigurationException),
typeof(FileNotFoundException),
typeof(ProtocolViolationException)
};
string TypeName = t.ToString();
// Get type of Exception
for (i = 0; i < types.Length; i++)
{
if (t.Equals(types
))
break;
}
sb.Append("Exception of Type \"" + TypeName + "\"" + nl);
// Handle with the appropriate handler
switch (i)
{
case 0: // WebException
GetWebException((WebException)ex, ref sb);
break;
case 1: // SqlException
GetSqlException((SqlException)ex, ref sb);
break;
case 2: // SmtpException
GetSmtpException((SmtpException)ex, ref sb);
break;
case 3: // SmtpFailedRecipientException
GetSmtpFailedRecipientException(
(SmtpFailedRecipientException)ex, ref sb);
break;
case 4: // SmtpFailedRecipientsException
GetSmtpFailedRecipientsException(
(SmtpFailedRecipientsException)ex, ref sb);
break;
case 5: // System.Configuration.ConfigurationException
GetConfigurationException(
(ConfigurationException)ex, ref sb);
break;
case 6:
GetFileNotFoundException(
(FileNotFoundException)ex, ref sb);
break;
case 7:
GetProtocolViolationException(
(ProtocolViolationException)ex, ref sb);
break;
default:
GetSystemException(ex, ref sb);
break;
}
if (ex.Data != null && ex.Data.Count > 0)
{
sb.Append(nl + "Data:");
foreach (DictionaryEntry d in ex.Data)
sb.Append(nl + "Key: " + d.Key.ToString() +
", Value: " + d.Value.ToString());
}
if (ex.InnerException != null)
{
sb.Append(nl + "InnerException: " + nl);
GetExceptionDetails(ex.InnerException, ref sb);
}
sb.Append(nl + "StackTrace: " + nl + ex.StackTrace);
if (ex.HelpLink != null && ex.HelpLink != "")
sb.Append(nl + "HelpLink: " + ex.HelpLink);
}
Basically, this method is passed a Stringbuilder by reference. It then
looks at the type of the Exception, and calls any of several overloads,
depending upon the Exception's type. It then looks at the InnerException,
and if any exists, it calls itself recursively for the InnerException.
What it returns is a Stringbuilder with a formatted string that can be
logged in any way.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Show me your certification without works,
and I'll show my certification
*by* my works.
RonL said:
Thanks for the links. I noticed one of the links it talked about using
reflection to retrieve the data from Exception object. Have you seen any
examples on how to do that?
Thanks,
Ron