exilon / QuickLogger

Powerful and flexible library for logging on files, console, memory, email, rest, eventlog, syslog, slack, telegram, redis, logstash, elasticsearch, influxdb, graylog, Sentry, Twilio, ide debug messages and throw events for Delphi/Firemonkey/freepascal/.NET (Windows/Linux/OSX/IOS/Android).
Apache License 2.0
380 stars 84 forks source link

Using logging in COM dll hangs when application is closed #63

Open JWBokx opened 2 years ago

JWBokx commented 2 years ago

Hello all,

I'm new here and landed here searching a solution for my problem. I use the quick library and logger already in standard VCL programs with no problems at all.

Now my colleague and I are building a plugin for Outlook and Word. For this we are using the components of add-in express. So we're building a COM plugin for local instance of office. The problem is when closing Word, It hangs. In outlook this isn't a problem, so just word.

Debugging it I found the point it stops responding. In Quick,Logger.pas at line 1315:

  fThreadProviderLog.Terminate;
  fThreadProviderLog.WaitFor;
  fThreadProviderLog.Free;
  //if Assigned(fProviders) then fProviders.Free;
  FreeAndNil( fProviders );
  fCustomTags := nil;
  FreeAndNil( fLogQueue ); 

The .WaitFor is never returning... I also had a breakpoint in the thread that has to stop, the while loop is ended correctly. So it seems the thread is terminatied after the calling of .Terminate method.

Kind regards,

Jan

casagrandeenrico commented 2 years ago

Hi, have you found any workaround?

JWBokx commented 2 years ago

I have kind of workaround, but I don't like it. I have remarked the line around 1320 fThreadProviderLog.WaitFor; in Quick.Logger. Maybe the line number is not correct in your source because of the changes in my source. Also changed most of the .free calls to FreeAndNil() Some of these had a if assigned(x) but only a free. If the code is somehow executed twice or more the if assigned wil return true, while this is not the case.

The deadlock is fixed with this, only an exception in during debugging I can not fix. Debugging the shutdown code in Word or Excel is quit dramatically. If a breakpoint is set, the execption is raised somewhere else. Without the debugger the exception seems not to occur, or it is and it's not visible.

So it's a workaround, but not one that feels good. I'm running out of time to continue debugging, so I have to leave it this way for now.

exilon commented 2 years ago

Could you try to replace this line with: if not IsLibrary then fThreadProviderLog.WaitFor;

JWBokx commented 2 years ago

Oh good one. I will try it on Monday, I have a day off now.

JWBokx commented 2 years ago

I have the FreeAndNil( fThreadProviderLog) also in the if included. It results in a few memory leaks report, but Word/Excel is closing fine now without an acc.violation. The shutdown proces of Word/Excel is difficult to debug. With the if, it also works in normal programs and can use the same source.

I do think changing the .Free calls to a FreeAndNil call can solve some strange behavings. But that's up to you. It seems the logger is not in use within a COM dll for Office... So I'm the only one with this strange behaviour.

exilon commented 2 years ago

Without these FreeAndNil changes, does Word/Excel hang when closing? Can you send me a minimum sample so I can reproduce it?

JWBokx commented 2 years ago

A minimal example is not possible. We use a closed source component as base for the com dll. This is to prevent we had all the com registration code for office to figure out. But this component is not free, an there is no trial version, only a 30 day money back. So for testing no t fun

exilon commented 2 years ago

Oh good one. I will try it on Monday, I have a day off now.

Did you try this?

JWBokx commented 2 years ago

Yes I did, sorry forgot to mention... It does help within Outlook, but not within Word.... Excel not tested yet. At the moment no plugin is needed for excel. Very strange, and the only way is now not to use the logging in Word.

All I can see the closing down of Outlook is different from Word. So in Outlook the finalization en destructors are called and can be debugged. But Word just kils the plugin and comes in a kind of deadlock in one of the destructors. The debugger is hanging too and no way to break the execution....

otomazeli commented 1 year ago

Does anyone have an example on how to use the QuickLogger.dll using GreyLog as provider?

Evanchl commented 11 months ago

I also encountered this issue, how do I solve it?