Example #1
0
        /// <summary>
        /// Clone the correlation
        /// </summary>
        /// <param name="existingCorrelation">existing correlation</param>
        /// <returns>cloned correlation, null if existing correlation is null</returns>
        /// <remarks>Added as a extension method instead of a method on the
        /// object to allow for cloning of the data when it is null, i.e.
        /// CorrelationData data = null; data.Clone(); will not throw exception.</remarks>
        public static CorrelationData Clone(this CorrelationData existingCorrelation)
        {
            if (existingCorrelation == null)
            {
                return(null);
            }

            CorrelationData newCorrelation = new CorrelationData(existingCorrelation.CachedUlsEventsReplayed);

            newCorrelation.ShouldLogDirectly    = existingCorrelation.ShouldLogDirectly;
            newCorrelation.ShouldReplayUls      = existingCorrelation.ShouldReplayUls;
            newCorrelation.VisibleId            = existingCorrelation.VisibleId;
            newCorrelation.CallDepth            = existingCorrelation.CallDepth;
            newCorrelation.UserHash             = existingCorrelation.UserHash;
            newCorrelation.EventSequenceNumber  = existingCorrelation.EventSequenceNumber;
            newCorrelation.IsFallbackCall       = existingCorrelation.IsFallbackCall;
            newCorrelation.TransactionContextId = existingCorrelation.TransactionContextId;
            newCorrelation.TransactionId        = existingCorrelation.TransactionId;
            newCorrelation.TransactionStep      = existingCorrelation.TransactionStep;

            if (existingCorrelation.HasData)
            {
                bool copiedSuccessfully = SpinWait.SpinUntil(() =>
                {
                    bool success = false;
                    try
                    {
                        foreach (object key in existingCorrelation.Keys)
                        {
                            string keystring = (string)key;
                            newCorrelation.AddData(keystring, existingCorrelation.Data(keystring));
                        }

                        success = true;
                    }
                    catch (InvalidOperationException)
                    {
                    }

                    return(success);
                }, 10);

                if (!copiedSuccessfully)
                {
                    // Add a marker to the correlation data indicating it is not complete
                    newCorrelation.AddData("Error", "Failed to clone correlation data.");
                }
            }

            return(newCorrelation);
        }
 /// <summary>
 /// Add correlation data
 /// </summary>
 /// <param name="key">key</param>
 /// <param name="value">value</param>
 /// <param name="data">existing correlation data</param>
 public void CorrelationAdd(string key, string value, CorrelationData data)
 {
     if (data != null)
     {
         data.AddData(key, value);
     }
 }
Example #3
0
        /// <summary>
        /// Constructs the timed scope correlation data
        /// </summary>
        /// <returns>Correlation data</returns>
        private CorrelationData ConstructCorrelationDataEntries(IMachineInformation machineInformation)
        {
            CorrelationData correlationData = TimedScopeData;

            CorrelationData scopeData = TimedScopeData.Clone();

            scopeData.AddData(TimedScopeDataKeys.InternalOnly.ScopeName, Name);
            scopeData.AddData(TimedScopeDataKeys.InternalOnly.InstanceId, InstanceId.ToString());
            scopeData.AddData(TimedScopeDataKeys.InternalOnly.IsSuccessful, IsSuccessful.HasValue ? IsSuccessful.Value.ToString() : bool.FalseString);
            scopeData.AddData(TimedScopeDataKeys.InternalOnly.IsRoot, IsRoot.ToString());
            scopeData.AddData(TimedScopeDataKeys.InternalOnly.ScopeResult, Result.ToString());

            bool isFailed = !IsSuccessful ?? false;

            if (isFailed && FailureDescription != null)
            {
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.FailureDescription, FailureDescription.ToString());
            }

            scopeData.AddData(TimedScopeDataKeys.InternalOnly.Duration, DurationInMilliseconds.ToString(CultureInfo.InvariantCulture));
            long sequenceNumber = correlationData == null ? 0 : correlationData.NextEventSequenceNumber();

            scopeData.AddData(TimedScopeDataKeys.InternalOnly.SequenceNumber, sequenceNumber.ToString(CultureInfo.InvariantCulture));
            scopeData.AddData(TimedScopeDataKeys.InternalOnly.CallDepth, correlationData == null ? "0" : correlationData.CallDepth.ToString(CultureInfo.InvariantCulture));

            IMachineInformation machineInfo = machineInformation;

            if (machineInfo != null)
            {
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.MachineId, machineInfo.MachineId);
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.MachineCluster, machineInfo.MachineCluster);
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.MachineRole, machineInfo.MachineRole);
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.AgentName, machineInfo.AgentName);
            }

            // if the user hash has been set, add it to the scope data
            if (!string.IsNullOrWhiteSpace(m_userHashOverride))
            {
                ULSLogging.LogTraceTag(0x23817500 /* tag_96xua */, Categories.TimingGeneral, Levels.Verbose,
                                       "Overriding user hash metadata in the Timed Scope '{0}' with value '{1}'", Name, m_userHashOverride);
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.UserHash, m_userHashOverride);
            }
            else if (correlationData != null && !string.IsNullOrWhiteSpace(correlationData.UserHash))
            {
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.UserHash, correlationData.UserHash);
            }

            // capture performance metrics
            if (PerfDiagnostics != null && PerfDiagnostics.LastStatus)
            {
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.CpuCycles,
                                  PerfDiagnostics.CyclesUsed.ToString(CultureInfo.InvariantCulture));
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.UserModeDuration,
                                  PerfDiagnostics.UserModeMilliseconds.ToString(CultureInfo.InvariantCulture));
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.KernelModeDuration,
                                  PerfDiagnostics.KernelModeMilliseconds.ToString(CultureInfo.InvariantCulture));
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.HttpRequestCount,
                                  PerfDiagnostics.HttpRequestCount.ToString(CultureInfo.InvariantCulture));
                scopeData.AddData(TimedScopeDataKeys.InternalOnly.ServiceCallCount,
                                  PerfDiagnostics.ServiceCallCount.ToString(CultureInfo.InvariantCulture));
            }

            return(scopeData);
        }