// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

package com.microsoft.signalr;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;

class TestLogger implements AutoCloseable {
    private Logger logger;
    private ListAppender<ILoggingEvent> appender;

    public TestLogger() {
        this("com.microsoft.signalr.HubConnection");
    }

    public TestLogger(String category) {
        this.logger = (Logger)LoggerFactory.getLogger(category);
        this.appender = new ListAppender<ILoggingEvent>();
        this.appender.start();
        this.logger.addAppender(this.appender);
    }

    public ILoggingEvent assertLog(String logMessage) {
        for (ILoggingEvent log : appender.list) {
            if (log.getFormattedMessage().startsWith(logMessage)) {
                return log;
            }
        }

        assertTrue(false, String.format("Log message '%s' not found", logMessage));
        return null;
    }

    @Override
    public void close() {
        this.logger.detachAppender(this.appender);
    }

}
@@ -253,16 +253,157 @@
 tTrue;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReentrantLock;%0A%0A
 import o
@@ -545,25 +545,53 @@
 ore.
-read.List
+Appender;%0Aimport ch.qos.logback.core.
 Appender
 ;%0A%0Ac
@@ -586,16 +586,20 @@
 Appender
+Base
 ;%0A%0Aclass
@@ -677,19 +677,131 @@
 rivate L
-ist
+ock lock = new ReentrantLock();%0A    private List%3CILoggingEvent%3E list = new ArrayList%3CILoggingEvent%3E();%0A    private 
 Appender
@@ -1051,20 +1051,16 @@
 new 
-List
 Appender
 %3CILo
@@ -1047,32 +1047,36 @@
 r = new Appender
+Base
 %3CILoggingEvent%3E(
@@ -1080,151 +1080,588 @@
 t%3E()
-;%0A        this.appender.start();%0A        this.logger.addAppender(this.appender);%0A    %7D%0A%0A    public ILoggingEvent assertLog(String logMessage) %7B
+ %7B%0A            public void append(ILoggingEvent event) %7B%0A                lock.lock();%0A                try %7B%0A                    list.add(event);%0A                %7D finally %7B%0A                    lock.unlock();%0A                %7D%0A            %7D%0A        %7D;%0A        this.appender.start();%0A        this.logger.addAppender(this.appender);%0A    %7D%0A%0A    public ILoggingEvent assertLog(String logMessage) %7B%0A        ILoggingEvent%5B%5D localList;%0A        lock.lock();%0A        try %7B%0A            localList = list.toArray(new ILoggingEvent%5B0%5D);%0A        %7D finally %7B%0A            lock.unlock();%0A        %7D%0A
 %0A   
@@ -1694,18 +1694,14 @@
 g : 
-appender.l
+localL
 ist)
@@ -2033,10 +2033,9 @@
 ;%0A    %7D%0A
-%0A
 %7D

Fix concurrent access to TestLogger (#26204)