/// <summary> /// Add data to the current correlation /// </summary> /// <remarks>If there is no current correlation, starts a new correlation</remarks> /// <param name="key">key (name) of the correlation</param> /// <param name="value">value of the added correlation data</param> public void CorrelationAdd(string key, string value) { Code.ExpectsNotNullOrWhiteSpaceArgument(key, nameof(key), TaggingUtilities.ReserveTag(0x2381771e /* tag_96x24 */)); Code.ExpectsNotNullOrWhiteSpaceArgument(value, nameof(value), TaggingUtilities.ReserveTag(0x2381771f /* tag_96x25 */)); CorrelationData data = CurrentCorrelation; if (data == null) { CorrelationStart(null); data = CurrentCorrelation; } if (data != null) { string oldData = data.Data(key); CorrelationHandler.CorrelationAdd(key, value, data); EventHandler <CorrelationEventArgs> correlationDataAdded = CorrelationDataAdded; if (correlationDataAdded != null) { correlationDataAdded(this, new CorrelationEventArgs(data, key, oldData)); } } }
/// <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); }