diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs new file mode 100644 index 00000000..44d68ddb --- /dev/null +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// +using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.FeatureManagement.FeatureFilters; + +namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights +{ + /// + /// Provides extension methods for tracking events with TargetingContext. + /// + public static class TelemetryClientExtensions + { + /// + /// Extension method to track an event with . + /// + public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary properties = null, IDictionary metrics = null) + { + ValidateTargetingContext(targetingContext); + + if (properties == null) + { + properties = new Dictionary(); + } + + properties["TargetingId"] = targetingContext.UserId; + + telemetryClient.TrackEvent(eventName, properties, metrics); + } + + /// + /// Extension method to track an with . + /// + public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext) + { + ValidateTargetingContext(targetingContext); + + if (telemetry == null) + { + telemetry = new EventTelemetry(); + } + + telemetry.Properties["TargetingId"] = targetingContext.UserId; + + telemetryClient.TrackEvent(telemetry); + } + + private static void ValidateTargetingContext(TargetingContext targetingContext) + { + if (targetingContext == null) + { + throw new ArgumentNullException(nameof(targetingContext)); + } + } + } +}