Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct logging within thread without dialog with Eurekalog

I have a Delphi 10 project using the latest version of EurekaLog. I'm currently using EurekaLog to help me debug problems in my production clients.

I noticed that EurekaLog wasn't registering errors that happened within threads. After I started reading up on it, I found that I need to change from TThread to TThreadEx, and add the following code at the start of my Execute overriden method.

SetEurekaLogStateInThread(ThreadID, true);

Despite this, when an error happens, it does not generate an event in the EL file.

If I add ExceptionManager.StandardEurekaError('TThrdSincArquivos.Execute => ' + ex.Message); on the try..except, it does log. But the stack trace is displayed as if the error occurred on the line where I call StandardEurekaLog(), not on the line where the error actually occurred. This defeats the purpose of the whole thing.

Another problem is that it displays a dialog box, which I don't want, since the error occurred inside a background thread. I just want it logged. I should get a dialog only with errors on the main thread.

How can I achieve theses results within the thread?

  • Actually log the error with the correct stack.

  • When on the main thread, display the dialog, but within a thread, just log with no dialog.

EDIT

Below is my EurekaLog Muti-threading configuration

Call Stack option Multi-threading option

Here is my thread declaration:

unit ThrdSincArquivos;

interface

uses
  System.Classes, System.SysUtils, System.Generics.Collections, REST.Client, REST.Types,
  System.JSON, Data.DB, Datasnap.DBClient, FireDAC.Comp.Client, FireDAC.Stan.Param, System.SyncObjs, EBase, EExceptionManager, EClasses;

type
  TThrdSincArquivos = class(TThreadEx)
  private

My thread's Create

constructor TThrdSincArquivos.Create(pPrimeiraExec: boolean; tipoSincParam: TTipoSinc);
begin
  inherited Create(true);

  NameThreadForDebugging('TThrdSincArquivos');
  primeiraExec := pPrimeiraExec;
  tipoSinc := tipoSincParam;
  executadoThreadSinc := false;
  FreeOnTerminate := true
end;

The start of my Execute

procedure TThrdSincArquivos.Execute;
var
  contador: Integer;
begin
  inherited;

  try

and the end of the Execute

  except
    on ex: Exception do
    begin
      oLog.GravarLog(ex, 'TThrdSincArquivos.Execute => FIM');
    end;
  end;
end;

It refuses to log any exception to the Elf file. I tried to add a raise after my own log routine, but it still didn't help. It should log, but it isn't, unless I explicitly call the StandardEurekaError, but I get the stack wrong, and I get the dialog.

like image 835
Pascal Avatar asked Oct 06 '17 23:10

Pascal


Video Answer


1 Answers

When you are using TThread class - it saves thread exception to .FatalException property, which you are supposed to handle in some way. Either from thread event, or from other (caller) thread. EurekaLog does not break this behaviour. E.g. your previosly written code will not change its behaviour when you enable EurekaLog. That way your properly written code would work correctly both with and without EurekaLog.

How your code is currently handling thread exceptions? Are you doing something like ShowMessage or custom logging? This obviosly would not work with EurekaLog, it does not know that you are processing exceptions with ShowMessage or your own custom logging code. You probably want something like Application.ShowException or re-raise in caller thread.

If you can not use default RTL/VCL processing (which is hooked by EurekaLog) for some reason - then you need to tell EurekaLog that you want to handle this particular exception. For example, from docs: you can use (for example) HandleException(E); from EBase unit:

Thread.WaitFor;
if Assigned(Thread.FatalException) then
begin
  // Your old code is here
  // Do your own thing: show message, log, etc.

  // Tell EurekaLog to do its thing:
  HandleException(Thread.FatalException);
end;

You would probably want to set exception filter or use events to disable dialogs for thread exceptions, because presumably you have already processed exception yourself (e.g. already showed message).

There is A LOT more ways to handle exception in threads, and EurekaLog's docs illustrate each thread case (like BeginThread, TThread, thread pools, etc.) with several possible options. It is just not reasonable to pack all this information into a single answer.

If, for some reason, you do not have code that processes .FatalException property of TThread - then you can use TThreadEx class and its .AutoHandleException property to handle exceptions automatically when thread exits, as described here:

type
  TMyThread = class(TThreadEx)
  protected
    procedure Execute; override;
  end;

procedure TMyThread.Execute;
begin
  // ... your code ...
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Thread: TMyThread;
begin
  Thread := TMyThread.Create(True, 'My thread');
  Thread.AutoHandleException := True; // <- added
  Thread.FreeOnTerminate := True;
  Thread.Start;
  Thread := nil; // never access thread var with FreeOnTerminate after Start
end;

However, be aware that you code will not work properly (e.g. will ignore exceptions) if you decide to disable EurekaLog in the future. Because if you remove EurekaLog from your project - then your project will have no code to handle thread exceptions!

P.S.

I need to change from TThread to TThreadEx, and add the following code at the start of my Execute overriden method.

SetEurekaLogStateInThread(ThreadID, true);

That is slightly incorrect: you can do either one or another, but not both. And there are other ways to tell EurekaLog that it should hook exceptions in this thread.

Basically, exception life has two stages: raise and handle. EurekaLog hooks both stages when they are implemented in default RTL/VCL code. You need to explicitly indicate which threads you want to hook, because you probably want to ignore system / 3rd party threads, which you have no control over. And it so happens that default processing for TThread does not exist in RTL/VCL. That is why there is nothing to hook.

like image 81
Alex Avatar answered Sep 21 '22 04:09

Alex