예제 #1
0
        public void AttachRemoteLogger(
            IRemoteLoggerPrx?prx,
            LogMessageType[] types,
            string[] categories,
            int messageMax,
            Current current)
        {
            if (prx == null)
            {
                return; // can't send this null RemoteLogger anything!
            }

            var messageTypes    = new HashSet <LogMessageType>(types);
            var traceCategories = new HashSet <string>(categories);

            LinkedList <LogMessage>?initLogMessages = null;
            LogForwarder            logForwarder;

            lock (_mutex)
            {
                if (_destroyed)
                {
                    throw new ObjectNotExistException(current);
                }

                _sendLogCommunicator ??= CreateSendLogCommunicator(current.Adapter.Communicator, _logger.LocalLogger);

                Identity remoteLoggerId = prx.Identity;

                if (_logForwarderMap.ContainsKey(remoteLoggerId))
                {
                    if (TraceLevel > 0)
                    {
                        _logger.Trace(TraceCategory, @$ "rejecting `{prx}' with RemoteLoggerAlreadyAttachedException");
                    }

                    throw new RemoteLoggerAlreadyAttachedException();
                }

                logForwarder = new LogForwarder(
                    this,
                    ChangeCommunicator(prx, _sendLogCommunicator),
                    messageTypes,
                    traceCategories);
                _logForwarderMap.Add(remoteLoggerId, logForwarder);

                if (messageMax != 0)
                {
                    initLogMessages = new LinkedList <LogMessage>(_queue); // copy
                }
                else
                {
                    initLogMessages = new LinkedList <LogMessage>();
                }
            }

            if (TraceLevel > 0)
            {
                _logger.Trace(TraceCategory, $"attached `{prx}'");
            }

            if (initLogMessages.Count > 0)
            {
                FilterLogMessages(initLogMessages, messageTypes, traceCategories, messageMax);
            }

            logForwarder.Queue("init", _logger, prx => prx.InitAsync(_logger.Prefix, initLogMessages.ToArray()));
        }