To facilitate logging in the business system, you can create a thread class that is dedicated to logging
Type
Tcustomlogthread = Class (TThread)
In order to ensure the validity and security of the thread logging, a critical section can be added to protect the log, while the high-frequency logging can effectively protect the logging security
Var
Fcs:trtlcriticalsection; --winapi.windows.pas
--Create--release
InitializeCriticalSection (FCS); DeleteCriticalSection (FCS);
--Enter protection--after the log processing is completed, to exit the protection, or else the thread can never log
EnterCriticalSection (FCS); LeaveCriticalSection (FCS);
------------------------------------------------------------------------
In addition, D also provides a class to encapsulate the above
Var
Fcs:tcriticalsection; --System.SyncObjs.pas--Recommended use of this type
--Create--release
FCS: = tcriticalsection.create; Fcs. Destroy;
--Enter--leave
Fcs. Enter; (FCS. Acquire) FCS. Leave; (FCS. Release)
----------------------------------------------------------------------------------------------
Protection operation: A. Access Protection; B. Logging c. exit protection
If you need to log the log level, or the type of log classification, can be handled by the following two ways
A: In the log thread, the attributes that need to be classified are identified for processing the log file or processing the contents of the Log Content node when the log is executed.
B: Another way is to create a new object class, to provide the relevant log thread object in the custom class, to record the logs of different classifications separately
In the case of scenario B, you need to configure the critical polygons in your custom class to ensure that different log threads have security access to the same log resource
If it is scenario a, you can add a critical section to the log thread to ensure security when logging functions are called by the outside world
Logging. 02_ Thread Handling