简体   繁体   中英

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 messages 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: ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM