Dave Craggs 7 Posted January 25, 2023 How do you get a StackTrace when an exception occurs? I have implement a logger inheriting from TDUnitXNullLogger Got this in OnTestError procedure TJPANUllLogger.OnTestError(const threadId: TThreadID; const Error: ITestError); begin inherited; FCurrentTestItem.Error := True; FCurrentTestItem.StackTrace := Error.StackTrace; Codesite.SendFmtMsg('StackTrace %s',[Error.StackTrace]); Codesite.SendFmtMsg('Message %s',[Error.ExceptionMessage]); Codesite.SendFmtMsg('LocationInfo %s',[Error.ExceptionLocationInfo]); Codesite.SendFmtMsg('AddressInfo %s',[Error.ExceptionAddressInfo]); end; ExceptionMessage is coming through, but not StackTrace Share this post Link to post
Der schöne Günther 316 Posted January 25, 2023 (edited) The StackTrace property is just a placeholder, it is always empty. There is no built-in way to get the stacktrace. Hard to believe, but here is what the official documentation says: System.SysUtils.Exception.StackTrace - RAD Studio API Documentation (embarcadero.com) Quote By default, StackTrace is always an empty string. (...) In order to use the StackTrace property to obtain the stack trace for exceptions, you have to use (or implement) a stack trace provider. There are a number of third-party solutions, both commercial and free. Some of the stack trace providers are: JEDI Code Library (the JclDebug and the JCLHoohExcept unit). EurekaLog madExcept For more information on how to use some of the above third-party stack trace providers, see the following blog posts: (...) A few years ago, I used the Jedi Code library. After that, I completely removed the JCL from our products and since then, I am just using the "FastMM_FullDebugMode.dll" which exports a few handy methods that will give you the stack trace as a string list. Edited January 25, 2023 by Der schöne Günther Share this post Link to post
Lars Fosdal 1793 Posted January 25, 2023 Or, you invest in EurekaLog or MADExcept. Share this post Link to post
aehimself 399 Posted January 25, 2023 There are also open-source alternatives: https://github.com/MahdiSafsafi/DebugEngine https://github.com/Fr0sT-Brutal/Delphi_StackTraces I never used JCL as I only wanted stack traces and I had to import half of the library just to achieve that. Share this post Link to post
Dave Craggs 7 Posted January 25, 2023 I have been using MadExcept for years. So how do I plug it into DunitX? Share this post Link to post
Dave Craggs 7 Posted January 25, 2023 Hmm - there is a unit DUnitX.StackTrace.MadExcept5; Share this post Link to post
Dave Craggs 7 Posted January 25, 2023 Think I am losing it. Was also discussed here - no resolution though. Share this post Link to post
Dave Craggs 7 Posted January 25, 2023 OK got it working. Needed to include {$I DUnitX.Stacktrace.inc} Needed to be edited to enable MadExcept5 Also needed to add DUnitX.StackTrace.MadExcept5 to the project. 1 Share this post Link to post