4

Spring4D 的日志记录机制是线程安全的吗?(单位 Spring.Logging.*,我想)

例如,多个线程可以解析并使用ILogger来自以下容器的实现吗?

uses
  System.SysUtils,
  Spring.Container,
  Spring.Logging,
  Spring.Logging.Appenders,
  Spring.Logging.Controller,
  Spring.Logging.Loggers;

var
  Container: TContainer;
  TextLogAppender: TTextLogAppender;
  FileLogAppender: TFileLogAppender;
  LoggerController: TLoggerController;
  LoggerInThread1: ILogger;
  LoggerInThread2: ILogger;
begin
  TextLogAppender := TTextLogAppender.Create;
  FileLogAppender := TFileLogAppender.Create;
  FileLogAppender.FileName := 'Log.txt';
  LoggerController := TLoggerController.Create;
  LoggerController.AddAppender(TextLogAppender as ILogAppender);
  LoggerController.AddAppender(FileLogAppender as ILogAppender);

  Container := TContainer.Create;
  try
    Container.RegisterInstance<ILoggerController>(LoggerController);
    Container.RegisterType<ILogger, TLogger>;

    Container.Build;

    LoggerInThread1 := Container.Resolve<ILogger>;
    LoggerInThread2 := Container.Resolve<ILogger>;

    {$Region 'threaded code'}
    LoggerInThread1.Info('Hello from Thread 1!');
    LoggerInThread2.Info('Hello from Thread 2!');
    {$EndRegion}
  finally
    Container.Free;
  end;
end;

使用分支release/1.2(当前为 33e3232...)。

4

1 回答 1

3

TFileLogAppender可以同时从多个线程中使用,因为它派生自which在其方法中TStreamLogAppender使用 a以避免与多线程相关的问题。TCriticalSectionDoSend

TTextLogAppender TCriticalSection在其实现中使用 的实例DoSend。所以我认为它不应该同时在多个线程中工作。

于 2017-01-11T14:17:56.647 回答