I really liked the original exception management block put out by Microsoft. The reason is because it included all the information relating to an exception - not just the Message property. It made production debugging a breeze. Now with ASP.NET 2.0 and Health Monitoring something similar has been created but it doesn't quite cover all the details. I first ran into this in an application where I had created a custom exception, ArgumentFormatException, and passed along two additional properties. After puttering through the underpinnings of the Health Monitoring feature I noticed that the exception information is captured using the Message property of the Exception by the EventLogWebProvider class. In order to beef up the data returned by my exception I put in a Message property which overrides the base version:
public override string Message
{
get
{
StringBuilder message = new StringBuilder(base.Message);
if (!string.IsNullOrEmpty(ExpectedFormat))
{
message.Append(Environment.NewLine);
message.AppendFormat(CultureInfo.CurrentCulture, StringResources.ParameterExpectedFormat, ExpectedFormat);
}
if (!string.IsNullOrEmpty(ActualValue))
{
message.Append(Environment.NewLine);
message.AppendFormat(CultureInfo.CurrentCulture, StringResources.ParameterActualValue, ActualValue);
}
return message.ToString();
}
}
After re-running the tests voila I now have more detailed exceptions:
- Exception information:
- Exception type: ArgumentFormatException
- Exception message: The variable does not match the expected format.
- Parameter name: emailAddress
- Expected Format: ^([a-zA-Z0-9_\-\.']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
- Actual Value: xxxx