public IReadOnlyCollection BuildEventPipeProviders()

in src/ServiceProfiler.EventPipe.Otel/Microsoft.ApplicationInsights.Profiler.Shared/Contracts/DiagnosticsClientTraceConfigurationBase.cs [46:110]


    public IReadOnlyCollection<EventPipeProvider> BuildEventPipeProviders()
    {
        // Avoid repeat computing.
        if (_eventPipeProvidersCache.Any())
        {
            return _eventPipeProvidersCache;
        }

        IReadOnlyCollection<EventPipeProvider>? result = null;

        // Built in providers with provider name as key, case sensitive.
        Dictionary<string, EventPipeProvider> providerHolder = BuildPredefinedServiceProfilerProviders().ToDictionary(item => item.Name, StringComparer.Ordinal);

        // Appending custom providers when there's any.
        EventPipeProviderItem[]? customProviders = _userConfiguration.CustomEventPipeProviders?.ToArray();
        try
        {
            if (customProviders is not null && customProviders.Length > 0)
            {
                _logger.LogDebug("There's custom providers configured. Count: {customProviderCount}", customProviders.Length);
                int newProviderCount = 0;
                int replacedProviderCount = 0;
                foreach (EventPipeProviderItem providerInfo in customProviders)
                {
                    if (providerHolder.ContainsKey(providerInfo.Name))
                    {
                        // Replace existing provider
                        _logger.LogTrace("Replacing existing provider: {providerName}", providerInfo.Name);
                        providerHolder[providerInfo.Name] = providerInfo.ToProvider();
                        replacedProviderCount++;
                    }
                    else
                    {
                        // Append new provider
                        _logger.LogTrace("Appending new provider: {providerName}", providerInfo.Name);
                        providerHolder.Add(providerInfo.Name, providerInfo.ToProvider());
                        newProviderCount++;
                    }
                }

                _logger.LogInformation("Custom providers configured. New: {newCount}, replaced: {replacedCount}, total: {totalProviderCount}.", newProviderCount, replacedProviderCount, providerHolder.Count);
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected exception configuring custom EventPipe providers. Please refer to https://aka.ms/ep-sp/custom-providers for instructions.");
            _logger.LogTrace(ex, "Full Error: {fullError}", ex.ToString());
        }
        finally
        {
            result = [.. providerHolder.Values];
        }

        // Potentially heavy serialization, run it only when debugging / tracing.
        if (_logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("EventPipe provider list:");
            foreach (EventPipeProvider provider in result)
            {
                _logger.LogInformation("{provider}", JsonSerializer.Serialize(provider));
            }
        }

        return result ?? [];
    }