You are not logged in.
Pages: 1
Hello,
I need same help with logging. I want to log same values inside a thread. Each thread should have it's own logfile. So I create a simple demo:
var
TThreadLogger : TSynLogClass = TSynLog;
procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
with TThreadLogger.Family do begin
Level := LOG_VERBOSE;
PerThreadLog := ptOneFilePerThread;
DestinationPath := 'Threads/';
end;
for I := 0 to 10 do
TMyThread.Create;
end;
procedure TMyThread.Execute;
var
log : ISynLog;
i : Integer;
begin
log := TThreadLogger.Enter(self);
for i := 0 to 10 do begin
Sleep(Random(1000));
log.Log(sllInfo, 'Hello from Thread %',[i]);
end;
end;
Every thread get his own logfile - but after the threads have finshed the logfile is still locked.
How can I close the logfile and free the instance of TSynLog without closing my program?
Oliver
Offline
There is no such feature directly available.
You can try to execute a newly introduced method in Execute method:
procedure TMyThread.Execute;
...
log := nil; // to force logging end of method
TThreadLogger.SynLog.CloseLogFile;
end;
Offline
Thank you for the change - this fixed the locked file problem.
But the instance of TSynLog is still alive and each run of my thread cost 70 kb of heap memory.
After same investigation I found a solution in calling SynLogFile.Remove(self) at the end of CloseLogFile
procedure TSynLog.CloseLogFile;
begin
if fWriter=nil then
exit;
EnterCriticalSection(fThreadLock);
try
fWriter.Flush;
FreeAndNil(fWriterStream);
FreeAndNil(fWriter);
SynLogFile.Remove(self);
finally
LeaveCriticalSection(fThreadLock);
end;
end;
Maybe there is a better place for calling this function but this frees the TSynLock object and no memory is lost.
Offline
As this, we may face a threading race condition in some border cases.
I add to protect the global list of TSynLog instance in order to let it work properly.
I added a TSynLog.Release method for your exact purpose.
See http://synopse.info/fossil/info/e2420ad793
In all cases, just a question: why did you not use a thread pool, or a re-usable thread (see TSynBackgroundThreadAbstract in SynCommons.pas)?
Offline
The example with the thread was just for trying. In my particular case I need more loggers within the same thread - but I must be able to close them. The thread was the easiest way to demonstrate this.
Offline
Pages: 1