/// <summary> /// Flattens Extension object on ITelemetry if exists into the properties and measurements. /// </summary> internal static void FlattenIExtensionIfExists(this ITelemetry telemetry) { if (telemetry.Extension != null) { ISupportProperties itemWithProperties = telemetry as ISupportProperties; ISupportMetrics itemWithMetrics = telemetry as ISupportMetrics; // Do not serialize if data cannot be stored on the item if (itemWithProperties != null || itemWithMetrics != null) { DictionarySerializationWriter extensionSerializationWriter = new DictionarySerializationWriter(); telemetry.Extension.Serialize(extensionSerializationWriter); if (itemWithProperties != null) { Utils.CopyDictionary(extensionSerializationWriter.AccumulatedDictionary, itemWithProperties.Properties); } if (itemWithMetrics != null) { Utils.CopyDictionary(extensionSerializationWriter.AccumulatedMeasurements, itemWithMetrics.Metrics); } } } }
private static void SerializeUnknownTelemetryHelper(ITelemetry telemetryItem, JsonSerializationWriter jsonSerializationWriter) { DictionarySerializationWriter dictionarySerializationWriter = new DictionarySerializationWriter(); telemetryItem.SerializeData(dictionarySerializationWriter); // Properties and Measurements are covered as part of Data if present telemetryItem.CopyGlobalPropertiesIfExist(dictionarySerializationWriter.AccumulatedDictionary); if (telemetryItem.Extension != null) { DictionarySerializationWriter extensionSerializationWriter = new DictionarySerializationWriter(); telemetryItem.Extension.Serialize(extensionSerializationWriter); // Extension is supposed to be flattened as well Utils.CopyDictionary(extensionSerializationWriter.AccumulatedDictionary, dictionarySerializationWriter.AccumulatedDictionary); Utils.CopyDictionary(extensionSerializationWriter.AccumulatedMeasurements, dictionarySerializationWriter.AccumulatedMeasurements); } jsonSerializationWriter.WriteProperty("name", telemetryItem.WriteTelemetryName(EventTelemetry.TelemetryName)); telemetryItem.WriteEnvelopeProperties(jsonSerializationWriter); // No need to copy Context - it's serialized here from the original item jsonSerializationWriter.WriteStartObject("data"); jsonSerializationWriter.WriteProperty("baseType", typeof(EventData).Name); jsonSerializationWriter.WriteStartObject("baseData"); jsonSerializationWriter.WriteProperty("ver", 2); jsonSerializationWriter.WriteProperty("name", EventNameForUnknownTelemetry); jsonSerializationWriter.WriteProperty("properties", dictionarySerializationWriter.AccumulatedDictionary); jsonSerializationWriter.WriteProperty("measurements", dictionarySerializationWriter.AccumulatedMeasurements); jsonSerializationWriter.WriteEndObject(); // baseData jsonSerializationWriter.WriteEndObject(); // data }
/// <summary> /// Flattens ITelemetry object into the properties and measurements. /// </summary> /// <returns>EventData containing flattened ITelemetry object.</returns> internal static EventData FlattenTelemetryIntoEventData(this ITelemetry telemetry) { EventData flatTelemetry = new EventData(); DictionarySerializationWriter dictionarySerializationWriter = new DictionarySerializationWriter(); telemetry.SerializeData(dictionarySerializationWriter); // Properties and Measurements are covered as part of Data if present Utils.CopyDictionary(dictionarySerializationWriter.AccumulatedDictionary, flatTelemetry.properties); Utils.CopyDictionary(dictionarySerializationWriter.AccumulatedMeasurements, flatTelemetry.measurements); if (telemetry.Context.GlobalPropertiesValue != null) { Utils.CopyDictionary(telemetry.Context.GlobalProperties, flatTelemetry.properties); } if (telemetry.Extension != null) { DictionarySerializationWriter extensionSerializationWriter = new DictionarySerializationWriter(); telemetry.Extension.Serialize(extensionSerializationWriter); // Extension is supposed to be flattened as well Utils.CopyDictionary(extensionSerializationWriter.AccumulatedDictionary, flatTelemetry.properties); Utils.CopyDictionary(extensionSerializationWriter.AccumulatedMeasurements, flatTelemetry.measurements); } return(flatTelemetry); }