Exception properties by:Ray Cassick \(Home\)
|
I am trying to create a generic logging component that will accept an exception object and create a string containing all the properties and their values available for a specific exception.
My intent here is try to create a generic function that will be able to:
- Take a an object of type Exception... - Iterate through all the properties the object has... - Add the property names to a stringbuilder... - Call each of the properties and get the values... - Add each of the values to the string builder.
I can get all the basic properties that System.Exception provides (Message, Source, Inner Exception, Target Site, Stack Trace, etc...) but I would also like to be bale to get any customer properties provided by the more specific exceptions and also print them out.
Can anyone point me to a piece of code that will help? I would also like to be able to print the specifics on custom exceptions that are not part of the framework but I am thinking that I am not going to be able to get much deeper than the standard properties provided by the base exception the custom exception inherits from.
Any ideas out there?
|
| | Reply: by:Chad Z. Hower aka Kudzu
|
| | Look in MSDN for "reflection". -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
Develop ASP.NET applications easier and in less time: http://www.atozed.com/IntraWeb/
|
| | Reply: by:Jay B. Harlow [MVP - Outlook]
|
| | Ray, I would expect custom Exceptions to override the Exception.ToString method to include this information in a readable & sensible format for you.
All the exception that I have used from the Framework do this for you, I would expect any one implementing their own exceptions to also do the same! Either by overriding Exception.ToString itself or overriding Exception.Message (which ever made the most sense for the use of the custom Exception).
Hope this helps Jay
|
| | Reply: by:Ray Cassick \(Home\)
|
| | Yeah, I know about that property. I was just trying to get something wrapped up that would be a bit more standardized and not depend on the person that designed the exception class.
|
| | Reply: by:Jay B. Harlow [MVP - Outlook]
|
| | Ray, As Chad stated, you can use Reflection to do what you want. IMHO doing the exception correctly is the "standardized" part! The person designing the exception would/should design the exception per standards!
Also won't you be logging duplicate information in most cases? (As most exception classes are standardized, then you have your code to catch this info for non-standardized exceptions...)
Or missing information in cases where there was an InnerException?
The following are a couple of articles on Exception Standards:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconErrorRaisingHandlingGuidelines.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp
Hope this helps Jay
|
0 Comments:
Post a Comment