Anyone have a good way to output an Exception in C# to a string that handles inner exceptions and the such? I am sure every developer has her own implimentation of this, but I can't find my old one and want to see what better ones are out there.
Here is one without recursion for inner exceptions as an example:
var Message = String.Format("Exception Message: \n{0}\n\nStackTrace: \n{1}", ex.Message, ex.StackTrace);
To log a handled exceptionCreate the method that will generate the exception information. Use a Try...Catch block to catch the exception. Put the code that could generate an exception in the Try block. Uncomment the Dim and MsgBox lines to cause a NullReferenceException exception.
Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger.
Log it when you handle it So, better only log the exception when you handle it. Like in the following code snippet. The doSomething method throws the exception. The doMore method just specifies it because the developer doesn't have enough information to handle it.
Therefore, you should log a stacktrace if, and only if, and always if, the exception indicates a bug in the program. However, that does not always indicate that a method you write should catch and log the exception.
Simply method ToString is the best:
ex.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With