Exceptions: how can I get the most information possible?

I have an enterprise application that logs exception information whenever it happens. An application is a combination of C # and C ++. Net.

I want to get as much information as possible during the exception. At the moment, we only print the message and the stack trace (which gives line numbers in debug builds, but does not release).

The goal of any error log is to indicate the error to the best of its ability (as close to it as possible). I'm curious how can I do this next? I want more information.

As for the instance, I know that NullReferenceExceptions and InvalidArguementExceptions contain different amounts of information, but I do not use anything other than the "Message" field.

Do I prefer to use reflection and capture all public members and print them? Or perhaps a chain of TON types of checks and rolls to print them cleanly? As a limitation, my logging function should just accept an argument of type Exception.

+4
source share
2 answers

In general, the answer is log ex.ToString (). In general, an object's ToString method displays what the object wants you to know. In the case of the base class Exception, this includes a message and stack trace, in addition to any InnerException instances. Individual derived Exception classes may display additional information.

Some exceptions, such as SqlException, also contain additional information (for example, stored procedure name and line number). In these cases, it is useful to serialize the entire exception and save it as XML in your logging database. Note that not all exception classes can be serialized, so be prepared for use in the XML part.

Also, if everything you do is registering an exception, then you might want to rebuild the exception so that higher levels can handle it. Of course, this does not apply if you are already at the "top level".

+8
source

Try:

try { //... } catch(Exception ex) { Messagebox.Show(ex.ToString()); } 

It displays a lot of information, including the type of exception , message and stack trace of all exceptions ( InnerException all exceptions ordered).

Additional information on MSDN .

+3
source

All Articles