I am catching an exception and logging it with ELMAH.
If I do not catch the exception and let the app YSOD, ELMAH logs the full stack trace. However when I catch and use ErrorSignal.Raise(ex)
, I do not get the stack trace.
I also tried to no avail:
ErrorSignal.FromCurrentContext().Raise(n开发者_运维问答ew System.ApplicationException(exception.ToString(), exception));
Whassup?
You're using ErrorSignal.Raise
with an unthrown exception that you've just new
'd up. The stack trace gets populated when you throw the exception, so your unthrown exception won't have a stack trace. ELMAH, on the other hand, will catch the exception instance you're wrapping here -- which, if you look at it in the debugger, will in fact contain the stack trace, unlike the wrapping exception.
Instead of wrapping in a System.ApplicationException (which, by the way you should originally have inherited from and nowadays is considered obsolete by the framework designers), you should just raise the original exception you have caught.
As an interesting side note: Since the stack trace is populated when you throw an exception, this:
catch (Exception ex) {
throw ex;
}
will destroy the original stack trace, whereas
catch (Exception ex) {
throw;
}
will not (the latter bit will emit the CIL rethrow
opcode which preserves the original stack trace).
This'll do it:
ErrorSignal.FromCurrentContext().Raise(
new System.ApplicationException(
exception.Message + exception.StackTrace, exception));
精彩评论