How to log assert messages with a custom `TraceListener`
I have implemented a custom TraceListener
which both logs the messages sent to method Write
and outputs them to the Win32 trace, using the Win32 API function OutputDebugString
. Whenever my application uses System.Diagnostics.Debug.Write
or System.Diagnostics.Trace.Write
, the messa开发者_如何学编程ges get properly handled by the trace listener.
However, if my application runs into an assertion such as System.Diagnostics.Debug.Assert
I would like to intercept what is sent to the debug output. Somehow, the trace listener does not get called with the messages produced by the assert. Is this possible? Or do I have to provide my own implementation of Assert
in order for this to work?
Are you building in Release mode? Debug.Assert has the ConditionalAttribute for the DEBUG symbol defined so the calls are removed by compiler in Release mode.
Debug.Assert calls the Fail method on your trace listener. Is that implemented correctly? If you haven't overridden it, then it defaults to calling WriteLine prepended with "Fail: ".
精彩评论