threadsafelog TacThreadSafeLog component
Hierarchy Properties Methods Events
Return to Introduction  Previous page  Next page
The acThreadSafeLog is the log-writter component, which can be safely accessed from various different threads simultaneously, without additional synchronization.  
It can write the logged text both to specified file or "memo" control, and automatically inserts the specified prefix before each line. The prefix of each record can contain the current time, thread identifier and number of CPU ticks passed from application startup.  
Also the acThreadSafeLog can automatically send email message with last N log entries to administrator in case of fatal error.  

How to use?
In general, all that you need is to specify the file to which the log should be written to the FileName property, then call Log method anywhere its required, without worries about synchronization, lost text, and sudden access violatios even if you call the Log method "simultaneously" from separate threads of your application.  
Every time when your program calls Log method, it appends a new line of specified text to the log file, and automatically adds a special prefix, which can contain current time (with format specified in DateTimeFormat property), the identifier of thread which calls the Log method, and number of CPU ticks passed from the application startup. The values which you would like to see before each line of logged text, can be configured in Options property. Alternatively you can write OnCustomPrefix event handler to specify your own prefix before each line of log file dynamically, before the text will be actually written to the log file.  
In case if you would like to direct the output of the log file to some "memo" control (TCustomMemo or any its descendant) — point the corresponding control to the Memo property and set LogToMemo property to True.  
new! If machine which runs your application have limited disk space and you should limit the maximum size of log file — you can specify this limit in MaxSize property. When the size of the log file outgrows the limit, it calls OnMaxSizeReached event and automatically clears the log.  
new! Also the acThreadSafeLog can send email message with last N log entries to administrator in case of fatal error.  

tip If the file specified in FileName property is not exists, the component will automatically create it.  
iiwarning Note that the component locks that specified file at run-time, so operating system will prevent to append or modify information in that file by other programs. It just opens the file when program starts, and close it when program terminates. Unfortunately the application can not use the same log file from its several running instances, so use acOnlyOne component to allow to run only one instance of your application.  

See also
acThread and acOnlyOne components.