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:
127 public override string Message
128 {
129 get
130 {
131 StringBuilder messageText = new StringBuilder(base.Message);
132
133 if ((this.m_ExpectedFormat != null) && (this.m_ExpectedFormat.Length != 0))
134 {
135 messageText.Append(Environment.NewLine);
136 messageText.AppendFormat(CultureInfo.CurrentCulture, StringResources.ParameterExpectedFormat, m_ExpectedFormat);
137 }
138
139 if ((this.m_ActualValue != null) && (this.m_ActualValue.Length != 0))
140 {
141 messageText.Append(Environment.NewLine);
142 messageText.AppendFormat(CultureInfo.CurrentCulture, StringResources.ParameterActualValue, m_ActualValue);
143 }
144
145 return messageText.ToString();
146 }
147 }
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