danieleteti / loggerpro

An modern and pluggable logging framework for Delphi
Apache License 2.0
357 stars 91 forks source link

AddAppender raises AccessViolation and duplicates appenders #69

Closed vveliu-csw closed 1 year ago

vveliu-csw commented 1 year ago

Adding appender to LogWriter by AddAppender procedure log twice.

procedure TCustomLogWriter.AddAppender(const aAppender: ILogAppender);
begin
   Self.FLoggerThread.FAppenders.Add(aAppender);
   Self.FLogAppenders.Add(aAppender); 
   Self.FLoggerThread.FAppendersDecorators.Add(TLoggerThread.TAppenderAdapter.Create(aAppender));
end;

Duplicate is caused by the first line of the procedure Self.FLoggerThread.FAppenders.Add(aAppender); because Self.FLogAppenders is passed to Self.FLoggerThread so both are the same List.

Access violation occurs because Self.FLoggerThread.FAppendersDecorators is nil till the first Thread Execute.

Here is the modified version:

procedure TCustomLogWriter.AddAppender(const aAppender: ILogAppender);
begin 
   Self.FLogAppenders.Add(aAppender); 
   if Assigned( Self.FLoggerThread.FAppendersDecorators) then
     Self.FLoggerThread.FAppendersDecorators.Add(TLoggerThread.TAppenderAdapter.Create(aAppender));
end;
danieleteti commented 1 year ago

Fixed in this commit

Thank you

https://github.com/danieleteti/loggerpro/commit/d1e0db6e93724e5fd825f0a8c02151d60450f98f

vveliu-csw commented 1 year ago

@danieleteti U Welcome!

Related to the argument i opened a new Issue #70