Exemplo n.º 1
0
        /// <summary>
        /// Forward traces to the telemetry abstractions based on the legacy configuration.
        /// This functionality will be opt-in in the future, as we will not connect our tracing to the telemetry producer by default.
        /// </summary>
        /// <param name="traceConfiguration">The legacy trace configuration.</param>
        /// <param name="telemetryManager">The <see cref="TelemetryManager" /> to use.</param>
        private static void InitializeLegacyTraceTelemetryConsumersConfiguration(ITraceConfiguration traceConfiguration, TelemetryManager telemetryManager)
        {
            if (traceConfiguration == null)
            {
                return;
            }

            var newConsumers = new List <ITelemetryConsumer>();

            if (traceConfiguration.TraceToConsole && !telemetryManager.TelemetryConsumers.OfType <ConsoleTelemetryConsumer>().Any())
            {
                newConsumers.Add(new ConsoleTelemetryConsumer());
            }

            if (!string.IsNullOrEmpty(traceConfiguration.TraceFileName))
            {
                try
                {
                    if (!telemetryManager.TelemetryConsumers.OfType <FileTelemetryConsumer>().Any())
                    {
                        newConsumers.Add(new FileTelemetryConsumer(traceConfiguration.TraceFileName));
                    }
                }
                catch (Exception exc)
                {
                    Trace.Listeners.Add(new DefaultTraceListener());
                    Trace.TraceError("Error opening trace file {0} -- Using DefaultTraceListener instead -- Exception={1}", traceConfiguration.TraceFileName, exc);
                }
            }

            if (Trace.Listeners.Count > 0)
            {
                if (!telemetryManager.TelemetryConsumers.OfType <TraceTelemetryConsumer>().Any())
                {
                    newConsumers.Add(new TraceTelemetryConsumer());
                }
            }

            if (newConsumers.Count > 0)
            {
                telemetryManager.AddConsumers(newConsumers);
                LogConsumers.Add(new TelemetryLogConsumer(telemetryManager));
            }
        }
Exemplo n.º 2
0
        public static void Initialize(ITraceConfiguration config, bool configChange = false)
        {
            lock (lockable)
            {
                if (IsInitialized && !configChange)
                {
                    return;                                 // Already initialized
                }
                loggerStoreInternCache = new Interner <string, TraceLogger>(LOGGER_INTERN_CACHE_INITIAL_SIZE, loggerInternCacheCleanupInterval);

                BulkMessageLimit  = config.BulkMessageLimit;
                runtimeTraceLevel = config.DefaultTraceLevel;
                appTraceLevel     = config.DefaultTraceLevel;
                SetTraceLevelOverrides(config.TraceLevelOverrides);
                Message.WriteMessagingTraces            = config.WriteMessagingTraces;
                Message.LargeMessageSizeThreshold       = config.LargeMessageWarningThreshold;
                SerializationManager.LARGE_OBJECT_LIMIT = config.LargeMessageWarningThreshold;
                RequestContext.PropagateActivityId      = config.PropagateActivityId;
                defaultModificationCounter++;
                if (configChange)
                {
                    return; // code below should only apply during creation
                }

                /*
                 * Encountered a null reference exception during load testing using the test adapter
                 * The issue appears to be related to the closing of the trace listeners, commenting out the following lines appears to "fix" the issue
                 * R.E. 10/12/2010
                 *
                 * foreach (TraceListener l in Trace.Listeners)
                 * {
                 *  l.Close();
                 * }
                 * Trace.Listeners.Clear();
                 *
                 * // We need the default listener so that Debug.Assert and Debug.Fail work properly
                 * Trace.Listeners.Add(new DefaultTraceListener());
                 */
                if (config.TraceToConsole)
                {
                    bool containsConsoleListener = false;
                    foreach (TraceListener l in Trace.Listeners)
                    {
                        if (l.GetType() != typeof(ConsoleTraceListener))
                        {
                            continue;
                        }

                        containsConsoleListener = true;
                        break;
                    }
                    if (!containsConsoleListener)
                    {
                        LogConsumers.Add(new LogWriterToConsole());
                    }
                }
                if (!string.IsNullOrEmpty(config.TraceFileName))
                {
                    try
                    {
                        logOutputFile = new FileInfo(config.TraceFileName);
                        var l = new LogWriterToFile(logOutputFile);
                        LogConsumers.Add(l);
                    }
                    catch (Exception exc)
                    {
                        Trace.Listeners.Add(new DefaultTraceListener());
                        Trace.TraceError("Error opening trace file {0} -- Using DefaultTraceListener instead -- Exception={1}", logOutputFile, exc);
                    }
                }

                if (Trace.Listeners.Count > 0)
                {
                    // Plumb in log consumer to write to Trace listeners
                    var traceLogConsumer = new LogWriterToTrace();
                    LogConsumers.Add(traceLogConsumer);
                }

                IsInitialized = true;
            }
        }