示例#1
0
        private async Task <string> LoadInstrumentationKey()
        {
            //if the AI key is specified in the Telimena Portal, it should override the local one
            //however, checking if it is specified in Telimena will take time, and we are inside a constructor here - we don't want to block
            string key = null;

            try
            {
                using (HttpClient client = new HttpClient()
                {
                    BaseAddress = this.telimenaProperties.TelemetryApiBaseUrl
                })
                {
                    HttpResponseMessage response = await client.GetAsync(ApiRoutes.GetInstrumentationKey(this.telimenaProperties.TelemetryKey));

                    key = await response.Content.ReadAsStringAsync();

                    response.EnsureSuccessStatusCode();
                    return(key?.Trim('"'));
                }
            }
            catch (Exception ex)
            {
                TelemetryDebugWriter.WriteError($"Error while loading instrumentation key. Error: {ex}. Response: {key}");
                return(null);
            }
        }
        public async Task <string> GetUserTrackingSettings(Guid telemetryKey)
        {
            string stringified = null;

            try
            {
                using (HttpClient client = new HttpClient()
                {
                    BaseAddress = this.baseUrl
                })
                {
                    HttpResponseMessage response = await client.GetAsync(ApiRoutes.GetTelemetrySettings(telemetryKey));

                    stringified = await response.Content.ReadAsStringAsync();

                    response.EnsureSuccessStatusCode();
                    return(stringified);
                }
            }
            catch (Exception ex)
            {
                TelemetryDebugWriter.WriteError($"Error while loading instrumentation key. Error: {ex}. Response: {stringified}");
                return(null);
            }
        }
示例#3
0
        internal Telimena(ITelimenaStartupInfo startupInfo)
        {
            try
            {
                this.SetConnectionSecurityProtocol();

                this.propertiesInternal     = new TelimenaProperties(startupInfo);
                this.Locator                = new Locator(this.Properties.StaticProgramInfo);
                this.userTrackingController = new UserTrackingController(this.propertiesInternal, this.Locator, this.Serializer, startupInfo.UserInfo);

                this.httpClient = new TelimenaHttpClient(new HttpClient
                {
                    BaseAddress = this.propertiesInternal.StartupInfo.TelemetryApiBaseUrl
                });
                this.Messenger = new Messenger(this.Serializer, this.httpClient);

                this.telemetryModule = new TelemetryModule(this.propertiesInternal, this.userTrackingController);

                this.updates = new UpdatesModule(this);

                this.telemetryModule.InitializeTelemetryClient();
                if (startupInfo.RegisterUnhandledExceptionsTracking)
                {
                    AppDomain.CurrentDomain.UnhandledException += this.CurrentDomain_UnhandledException;
                }
            }
            catch (Exception e)
            {
                TelemetryDebugWriter.WriteError($"Error while initializing {nameof(Telimena)}. Error: {e}");
                //above all, we don't want to throw errors in client apps.
                //No telemetry is better than boom.
                throw;
            }
        }
示例#4
0
        private void StoreSettings(UserTrackingSettings settings)
        {
            try
            {
                string stringified = this.serializer.Serialize(settings);

                string path = Path.Combine(this.locator.GetWorkingDirectory().FullName, TrackingSettingsFileName);
                File.WriteAllText(path, stringified);
            }
            catch (Exception ex)
            {
                TelemetryDebugWriter.WriteError("Error while storing user tracking settings: " + ex);
            }
        }
示例#5
0
 private UserTrackingSettings Deserialize(string serializedSettings)
 {
     if (string.IsNullOrEmpty(serializedSettings))
     {
         return(null);
     }
     try
     {
         return(this.serializer.Deserialize <UserTrackingSettings>(serializedSettings));
     }
     catch (Exception ex)
     {
         TelemetryDebugWriter.WriteError("Error while deserializing user tracking settings: " + ex);
         return(null);
     }
 }
示例#6
0
 private UserTrackingSettings GetStoredSettings()
 {
     try
     {
         string path = Path.Combine(this.locator.GetWorkingDirectory().FullName, TrackingSettingsFileName);
         if (File.Exists(path))
         {
             var stringified = File.ReadAllText(path);
             return(this.serializer.Deserialize <UserTrackingSettings>(stringified));
         }
     }
     catch (Exception ex)
     {
         TelemetryDebugWriter.WriteError("Error while restoring user tracking settings: " + ex);
     }
     return(null);
 }
示例#7
0
        private static Uri GetTelemetryUriFromConfig(Uri telemetryApiBaseUrl)
        {
            var setting = ConfigurationManager.AppSettings.Get(TelimenaUrlSettingsKey);

            if (!string.IsNullOrEmpty(setting))
            {
                try
                {
                    var uri = new Uri(setting);
                    return(uri);
                }
                catch (Exception ex)
                {
                    TelemetryDebugWriter.WriteLine($"ERROR - Cannot convert AppSetting [{setting}] to URI. Telimena will NOT WORK. Error: {ex}");
                }
            }
            var path = Path.Combine(Directory.GetCurrentDirectory(), TelimenaUrlSettingsKey);

            if (File.Exists(path))
            {
                var text = File.ReadAllText(path);
                try
                {
                    var uri = new Uri(text);
                    return(uri);
                }
                catch (Exception ex)
                {
                    TelemetryDebugWriter.WriteError($"ERROR - Cannot convert content of file {path} - [{text}] to URI. Telimena will NOT WORK. Error: {ex}");
                    return(null);
                }
            }

            if (telemetryApiBaseUrl == null)
            {
                string message = $"ERROR - Telimena URL not specified. " +
                                 $"Either add AppSetting [{TelimenaUrlSettingsKey}] or create a [{TelimenaUrlSettingsKey}] file in your app working directory." +
                                 $"The setting value/file content should be JUST THE BASE URL to Telimena instance.";
                TelemetryDebugWriter.WriteError(message);
            }


            return(null);
        }
示例#8
0
        private void SetUserIdentifier(UserInfo value, string fileName, bool shared)
        {
            var directory = this.locator.GetWorkingDirectory();

            if (shared)
            {
                directory = directory.Parent;
            }
            try
            {
                string filePath = Path.Combine(directory.FullName, fileName);
                var    lines    = new string[] { value.UserIdentifier, value.MachineName };

                File.WriteAllLines(filePath, lines);
            }
            catch (Exception e)
            {
                //thats bad, but maybe it will succeed next time. Impact is smaller than throwing an error.
                TelemetryDebugWriter.WriteError($"Error while storing user identifier. Error: {e}");
            }
        }
示例#9
0
        /// <summary>
        /// Initializes/Adds operation id to the existing telemetry item.
        /// </summary>
        /// <param name="telemetryItem">Target telemetry item to add operation id.</param>
        public void Initialize(ITelemetry telemetryItem)
        {
            try
            {
                var  itemContext         = telemetryItem.Context.Operation;
                var  telemetryProp       = telemetryItem as ISupportProperties;
                bool isActivityAvailable = false;
                isActivityAvailable = ActivityExtensions.TryRun(() =>
                {
                    var currentActivity = Activity.Current;
                    if (currentActivity != null)
                    {
                        if (string.IsNullOrEmpty(itemContext.Id))
                        {
                            itemContext.Id = currentActivity.RootId;

                            if (string.IsNullOrEmpty(itemContext.ParentId))
                            {
                                itemContext.ParentId = currentActivity.Id;
                            }

                            foreach (var baggage in currentActivity.Baggage)
                            {
                                if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(baggage.Key))
                                {
                                    telemetryProp.Properties.Add(baggage);
                                }
                            }
                        }

                        string operationName = currentActivity.GetOperationName();

                        if (string.IsNullOrEmpty(itemContext.Name) && !string.IsNullOrEmpty(operationName))
                        {
                            itemContext.Name = operationName;
                        }
                    }
                });

                if (!isActivityAvailable)
                {
                    if (string.IsNullOrEmpty(itemContext.ParentId) || string.IsNullOrEmpty(itemContext.Id) ||
                        string.IsNullOrEmpty(itemContext.Name))
                    {
                        var parentContext = CallContextHelpers.GetCurrentOperationContext();
                        if (parentContext != null)
                        {
                            if (string.IsNullOrEmpty(itemContext.ParentId) &&
                                !string.IsNullOrEmpty(parentContext.ParentOperationId))
                            {
                                itemContext.ParentId = parentContext.ParentOperationId;
                            }

                            if (string.IsNullOrEmpty(itemContext.Id) &&
                                !string.IsNullOrEmpty(parentContext.RootOperationId))
                            {
                                itemContext.Id = parentContext.RootOperationId;
                            }

                            if (string.IsNullOrEmpty(itemContext.Name) &&
                                !string.IsNullOrEmpty(parentContext.RootOperationName))
                            {
                                itemContext.Name = parentContext.RootOperationName;
                            }

                            if (parentContext.CorrelationContext != null)
                            {
                                foreach (var item in parentContext.CorrelationContext)
                                {
                                    if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(item.Key))
                                    {
                                        telemetryProp.Properties.Add(item);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TelemetryDebugWriter.WriteError(
                    "Something went wrong when initializing [" + this.GetType().Name + "]. This initializer will be ignored." + ex);
            }
        }