System.Diagnostics.Trace - the correct way to register exceptions

I am using the Trace class from the Azure Worker role.

I want to write exceptions in such a way as to print all the information about the exception, which is usually:

  • Exception message
  • Stacktrace exception
  • Print internal exceptions recursively

I see only the Trace.TraceError method, which receives the string and arguments. Isn't there something that is equivalent to Java registration frameworks that receive an exception and know how to register it? (yes, I am taking the first steps in the MS world ...)

+5
source share
1 answer

No no. But you can write an extension method for the Exception class that does this so you can call

someException.Trace(); 

The ToString method of the Exception class probably returns everything you want to just track this. You can add additional information (repeat internal exceptions if the stack trace is insufficient) process ID, user ID, thread ID, etc. In the same way.

 public static class ExceptionExtensions { public static void Trace(this Exception _this) { Trace.TraceError("{0:HH:mm:ss.fff} Exception {1}", DateTime.Now, _this); } } 
+6
source

Source: https://habr.com/ru/post/1209874/


All Articles