Esempio n. 1
0
        public async override Task <object> GetOrCreateAsync(string logIdentifier, CancellationToken cancellationToken)
        {
            await _initializationSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (_currentLogWriter != null)
                {
                    // Dispose last log writer so we can start a new session. Technically only one should only ever be active at a time.
                    _currentLogWriter.Dispose();
                }

                var logInstanceNumber = Interlocked.Increment(ref s_logHubSessionId);
                var traceSource       = await _traceProvider.InitializeTraceAsync(logIdentifier, logInstanceNumber, cancellationToken).ConfigureAwait(false);

                _currentLogWriter = new DefaultLogHubLogWriter(traceSource !);
                var provider = new LogHubLoggerProvider(_currentLogWriter);

                return(provider);
            }
            finally
            {
                _initializationSemaphore.Release();
            }
        }
        // Virtual for testing
        public virtual async Task InitializeLoggerAsync(CancellationToken cancellationToken)
        {
            if (_loggerProvider is not null)
            {
                return;
            }

            await _initializationSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (_loggerProvider is null)
                {
                    _loggerProvider = (LogHubLoggerProvider)await _loggerFactory.GetOrCreateAsync(LogFileIdentifier, cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                _initializationSemaphore.Release();
            }
        }