/// <summary>
        /// Initializes the telemetry module and starts tracing EventSources specified via <see cref="Sources"/> property.
        /// </summary>
        /// <param name="configuration">Module configuration.</param>
        public void Initialize(TelemetryConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            this.client = new TelemetryClient(configuration);
            this.client.Context.GetInternalContext().SdkVersion = SdkVersionUtils.GetSdkVersion("evl:");

            if (this.Sources.Count == 0)
            {
                EventSourceListenerEventSource.Log.NoSourcesConfigured(nameof(EventSourceListener.EventSourceTelemetryModule));
                // Continue--we need to be prepared for handling disabled sources.
            }

            try
            {
                if (this.initialized)
                {
                    // Source listening requests might have changed between initializations. Let's start from a clean slate
                    EventSource enabledEventSource = null;
                    while (this.enabledEventSources.TryDequeue(out enabledEventSource))
                    {
                        this.DisableEvents(enabledEventSource);
                    }
                }

                // Special case: because of .NET bug https://github.com/dotnet/coreclr/issues/14434, using Microsoft-ApplicationInsights-Data will result in infinite loop.
                // So we will disable it by default, unless there is explicit configuration for this EventSource.
                bool hasExplicitConfigForAiDataSource =
                    this.Sources.Any(req => req.Name?.StartsWith(AppInsightsDataEventSource, StringComparison.Ordinal) ?? false) ||
                    this.DisabledSources.Any(req => req.Name?.StartsWith(AppInsightsDataEventSource, StringComparison.Ordinal) ?? false);
                if (!hasExplicitConfigForAiDataSource)
                {
                    this.DisabledSources.Add(new DisableEventSourceRequest {
                        Name = AppInsightsDataEventSource
                    });
                }

                // Set the initialized flag now to ensure that we do not miss any sources that came online as we are executing the initialization
                // (OnEventSourceCreated() might have been called on a separate thread). Worst case we will attempt to enable the same source twice
                // (with same settings), but that is OK, as the semantics of EnableEvents() is really "update what is being tracked", so it is fine
                // to call it multiple times for the same source.
                this.initialized = true;

                if (this.appDomainEventSources != null)
                {
                    // Decide if there is disable event source listening requests
                    if (this.DisabledSources.Any())
                    {
                        this.enabledOrDisabledEventSourceTestResultCache = new ConcurrentDictionary <string, bool>();
                        this.eventWrittenHandlerPicker = this.OnEventWrittenIfSourceNotDisabled;
                    }
                    else
                    {
                        this.eventWrittenHandlerPicker = this.onEventWrittenHandler;
                    }

                    // Enumeration over concurrent queue is thread-safe.
                    foreach (EventSource eventSourceToEnable in this.appDomainEventSources)
                    {
                        this.EnableAsNecessary(eventSourceToEnable);
                    }
                }
            }
            finally
            {
                // No matter what problems we encounter with enabling EventSources, we should note that we have been initialized.
                this.initialized = true;
            }
        }
Beispiel #2
0
        public DiagnosticSourceListenerSubscription(string listenerName, TelemetryClient telemetryClient, OnEventWrittenHandler onEventWrittenHandler)
        {
            if (listenerName == null)
            {
                throw new ArgumentNullException(nameof(listenerName));
            }

            if (telemetryClient == null)
            {
                throw new ArgumentNullException(nameof(telemetryClient));
            }

            if (onEventWrittenHandler == null)
            {
                throw new ArgumentNullException(nameof(onEventWrittenHandler));
            }

            this.listenerName          = listenerName;
            this.telemetryClient       = telemetryClient;
            this.onEventWrittenHandler = onEventWrittenHandler;
        }