public void AddOrUpdateContextProperty_TelemetryIsEnabled_DoesNothing() { using (ShimsContext.Create()) { const TelemetryProperty expectedProperty = TelemetryProperty.Comment; const string expectedValue = "carrot"; string actualProperty = null; string actualValue = null; StubITelemetry telemetry = new StubITelemetry { AddOrUpdateContextPropertyStringString = (property, value) => { actualProperty = property; actualValue = value; } }; ShimLogger.IsEnabledGet = () => true; ShimLogger.TelemetryGet = () => telemetry; Logger.AddOrUpdateContextProperty(expectedProperty, expectedValue); Assert.AreEqual(expectedProperty.ToString(), actualProperty); Assert.AreEqual(expectedValue, actualValue); } }
public void PublishTelemetryEvent_SingleProperty_TelemteryIsEnabled_EnablesAndChainsThrough() { using (ShimsContext.Create()) { // TelemetryAction used here is arbitrary const TelemetryAction action = TelemetryAction.ColorContrast_Click_Eyedropper; const TelemetryProperty property = TelemetryProperty.Comment; const string value = "Friday"; string actualName = null; IReadOnlyDictionary <string, string> actualTelemetryPropertyBag = null; ITelemetry telemetry = new StubITelemetry { PublishEventStringIReadOnlyDictionaryOfStringString = (name, telemetryPropertyBag) => { actualName = name; actualTelemetryPropertyBag = telemetryPropertyBag; } }; ShimLogger.IsEnabledGet = () => true; ShimLogger.TelemetryGet = () => telemetry; Logger.PublishTelemetryEvent(action, property, value); Assert.AreEqual(action.ToString(), actualName); Assert.AreEqual(1, actualTelemetryPropertyBag.Count); Assert.AreEqual(value, actualTelemetryPropertyBag[property.ToString()]); } }
public void AddOrUpdateContextProperty_TelemetryIsEnabled_DoesNothing() { using (ShimsContext.Create()) { // Specific value of TelemetryProperty is unimportant for this test const TelemetryProperty expectedProperty = (TelemetryProperty)9; const string expectedValue = "carrot"; string actualProperty = null; string actualValue = null; StubITelemetry telemetry = new StubITelemetry { AddOrUpdateContextPropertyStringString = (property, value) => { actualProperty = property; actualValue = value; } }; TelemetrySink.IsTelemetryAllowed = true; ShimTelemetrySink.TelemetryGet = () => telemetry; Logger.AddOrUpdateContextProperty(expectedProperty, expectedValue); Assert.AreEqual(expectedProperty.ToString(), actualProperty); Assert.AreEqual(expectedValue, actualValue); } }
public void PublishTelemetryEventContainer_TelemetryIsEnabled_EnablesAndChainsThrough() { using (ShimsContext.Create()) { // Specific values of TelemetryAction and TelemetryProperty are unimportant for this test TelemetryAction action = (TelemetryAction)6; TelemetryProperty property = (TelemetryProperty)7; var fakeId = "id"; var fakeEvent = new TelemetryEvent(action, new Dictionary <TelemetryProperty, string> { { property, fakeId }, }); string actualName = null; IReadOnlyDictionary <string, string> actualTelemetryPropertyBag = null; ITelemetry telemetry = new StubITelemetry { PublishEventStringIReadOnlyDictionaryOfStringString = (name, telemetryPropertyBag) => { actualName = name; actualTelemetryPropertyBag = telemetryPropertyBag; } }; TelemetrySink.IsTelemetryAllowed = true; ShimTelemetrySink.TelemetryGet = () => telemetry; Logger.PublishTelemetryEvent(fakeEvent); Assert.AreEqual(fakeEvent.Action.ToString(), actualName); Assert.AreEqual(1, actualTelemetryPropertyBag.Count); Assert.AreEqual(fakeId, actualTelemetryPropertyBag[property.ToString()]); } }
public void PublishTelemetryEvent_SingleProperty_TelemteryIsEnabled_EnablesAndChainsThrough() { using (ShimsContext.Create()) { // Specific values of TelemetryAction and TelemetryProperty are unimportant for this test const TelemetryAction action = (TelemetryAction)4; const TelemetryProperty property = (TelemetryProperty)5; const string value = "Friday"; string actualName = null; IReadOnlyDictionary <string, string> actualTelemetryPropertyBag = null; ITelemetry telemetry = new StubITelemetry { PublishEventStringIReadOnlyDictionaryOfStringString = (name, telemetryPropertyBag) => { actualName = name; actualTelemetryPropertyBag = telemetryPropertyBag; } }; TelemetrySink.IsTelemetryAllowed = true; ShimTelemetrySink.TelemetryGet = () => telemetry; Logger.PublishTelemetryEvent(action, property, value); Assert.AreEqual(action.ToString(), actualName); Assert.AreEqual(1, actualTelemetryPropertyBag.Count); Assert.AreEqual(value, actualTelemetryPropertyBag[property.ToString()]); } }
/// <summary> /// Explicitly updates context properties to be appended to future calls to the current telemetry pipeline /// </summary> /// <param name="property"></param> /// <param name="value"></param> public static void AddOrUpdateContextProperty(TelemetryProperty property, string value) { if (!IsEnabled) { return; } TelemetrySink.AddOrUpdateContextProperty(property.ToString(), value); }
/// <summary> /// Publishes event with single property/value pair to the current telemetry pipeline /// </summary> /// <param name="action"></param> /// <param name="property"></param> /// <param name="value"></param> public static void PublishTelemetryEvent(TelemetryAction action, TelemetryProperty property, string value) { // Conversions to strings are expensive, so skip it if possible if (!IsEnabled) { return; } TelemetrySink.PublishTelemetryEvent(action.ToString(), property.ToString(), value); }
public void SetProperty(TelemetryProperty property) { if (this.ContainsKey(property.Item1)) { this[property.Item1] = property.Item2; } else { Add(property.Item1, property.Item2); } }
/// <summary> /// Publishes event with single property/value pair to the current telemetry pipeline /// </summary> /// <param name="action"></param> /// <param name="property"></param> /// <param name="value"></param> public static void PublishTelemetryEvent(TelemetryAction action, TelemetryProperty property, string value) { // Check IsEnabled because ToString on enums is expensive if (!IsEnabled) { return; } PublishTelemetryEvent(action, new Dictionary <TelemetryProperty, string> { { property, value } }); }
public void PublishTelemetryEvent_MultiProperty_TelemteryIsEnabled_EnablesAndChainsThrough() { using (ShimsContext.Create()) { // TelemetryAction used here is arbitrary const TelemetryAction action = TelemetryAction.ColorContrast_Click_Eyedropper; const TelemetryProperty property = TelemetryProperty.Error; const string value = "Saturday"; string actualName = null; IReadOnlyDictionary <TelemetryProperty, string> actualConverterInput = null; IReadOnlyDictionary <string, string> actualTelemetryPropertyBag = null; ITelemetry telemetry = new StubITelemetry { PublishEventStringIReadOnlyDictionaryOfStringString = (name, telemetryPropertyBag) => { actualName = name; actualTelemetryPropertyBag = telemetryPropertyBag; } }; ShimLogger.IsEnabledGet = () => true; ShimLogger.TelemetryGet = () => telemetry; Dictionary <string, string> expectedConverterOutput = new Dictionary <string, string> { { "abc", "def" }, }; ShimLogger.ConvertFromPropertiesIReadOnlyDictionaryOfTelemetryPropertyString = (input) => { actualConverterInput = input; return(expectedConverterOutput); }; Dictionary <TelemetryProperty, string> initalInput = new Dictionary <TelemetryProperty, string> { { property, value }, }; Logger.PublishTelemetryEvent(action, initalInput); Assert.AreEqual(action.ToString(), actualName); Assert.AreEqual(initalInput, actualConverterInput); Assert.AreSame(expectedConverterOutput, actualTelemetryPropertyBag); Assert.AreEqual(action.ToString(), actualName); } }
public void PublishTelemetryEvent_MultiProperty_TelemteryIsEnabled_EnablesAndChainsThrough() { using (ShimsContext.Create()) { // Specific values of TelemetryAction and TelemetryProperty are unimportant for this test const TelemetryAction action = (TelemetryAction)8; const TelemetryProperty property = (TelemetryProperty)9; const string value = "Saturday"; string actualName = null; IReadOnlyDictionary <TelemetryProperty, string> actualConverterInput = null; IReadOnlyDictionary <string, string> actualTelemetryPropertyBag = null; ITelemetry telemetry = new StubITelemetry { PublishEventStringIReadOnlyDictionaryOfStringString = (name, telemetryPropertyBag) => { actualName = name; actualTelemetryPropertyBag = telemetryPropertyBag; } }; TelemetrySink.IsTelemetryAllowed = true; ShimTelemetrySink.TelemetryGet = () => telemetry; Dictionary <string, string> expectedConverterOutput = new Dictionary <string, string> { { "abc", "def" }, }; ShimLogger.ConvertFromPropertiesIReadOnlyDictionaryOfTelemetryPropertyString = (input) => { actualConverterInput = input; return(expectedConverterOutput); }; Dictionary <TelemetryProperty, string> initalInput = new Dictionary <TelemetryProperty, string> { { property, value }, }; Logger.PublishTelemetryEvent(action, initalInput); Assert.AreEqual(action.ToString(), actualName); Assert.AreEqual(initalInput, actualConverterInput); Assert.AreSame(expectedConverterOutput, actualTelemetryPropertyBag); Assert.AreEqual(action.ToString(), actualName); } }
/// <summary> /// Get the property corresponding to a specified key using the local cache of /// properties first and falling back to retrieval from the database and, if /// required, creation of a new property /// </summary> /// <param name="key"></param> /// <returns></returns> private TelemetryProperty GetProperty(string key) { TelemetryProperty property; if (_properties.ContainsKey(key)) { property = _properties[key]; } else { property = _context.Properties.FirstOrDefault(p => p.Name == key); if (property == null) { property = new TelemetryProperty { Name = key }; _context.Properties.Add(property); } _properties.Add(key, property); } return(property); }