Does System.Exception have a built-in thrown times

2019-02-17 22:56发布

问题:

Is there a way to extract when an exception was thrown from the exception itself, or will I need to manually add this information to the exception?

回答1:

There is no built-in Timestamp property for the Exception object. You can create a new class that inherits from Exception and add your own property.

If you're using your exception as a parameter of a log framework, you might want to handle the timestamp directly in the log framework (most probably using DateTime.Now / UtcNow).



回答2:

When an exception occurs, and IF you have handled it, then the control will immediately go to the catch block thats exactly the time when an exception occured

So the first line in catch block could get you exactly when using date object (DateTime.Now.Time etc)