public void CanHandleInvalidCachedServerConfig()
        {
            using (var dir = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                dir.WriteFile(CONFIG_FILENAME, "sadf<sdf>");

                Assert.True(dir.FileExists(CONFIG_FILENAME));

                var client = new ExceptionlessClient();
                ClientConfiguration config = ClientConfiguration.Create(client);

                // file should get deleted if it's invalid
                Assert.False(dir.FileExists(CONFIG_FILENAME));

                Assert.NotNull(config);

                Assert.True(config.ContainsKey("AttributeOnly"));
                Assert.Equal(config["AttributeOnly"], "Attribute");

                Assert.True(config.ContainsKey("UserNamespaces"));
                Assert.Equal(config["UserNamespaces"], "Exceptionless,FromConfig");

                Assert.True(config.ContainsKey("ConfigAndAttribute"));
                Assert.Equal(config["ConfigAndAttribute"], "Config");

                Assert.True(config.ContainsKey("AppConfigOnly"));
                Assert.Equal(config["AppConfigOnly"], "Config");
            }
        }
        public void CanEnableLoggingFromConfig()
        {
            var client = new ExceptionlessClient();
            ClientConfiguration config = ClientConfiguration.Create(client);
            string logPath             = config.LogPath;

            Assert.NotNull(config);

            Assert.True(config.EnableLogging);
            //Assert.Equal(logPath, config.LogPath); // TODO: Should this even be set to a value?? It's Isolated storage?
            Assert.NotNull(client.Log);
            Assert.NotNull(client);

            Assert.Equal(typeof(SafeExceptionlessLog), client.Log.GetType());

            client.ProcessQueue();
            client.Shutdown();

            var dir = new IsolatedStorageDirectory(DEFAULT_STORE);

            Assert.True(dir.FileExists(logPath));
            string content = dir.ReadFileAsString(logPath);

            Assert.True(content.Length > 10);
        }
Exemplo n.º 3
0
 private void DeleteConfig(string storeId = DEFAULT_STORE)
 {
     using (var dir = new IsolatedStorageDirectory(storeId)) {
         if (dir.FileExists(CONFIG_FILENAME))
         {
             dir.DeleteFile(CONFIG_FILENAME);
         }
     }
 }
Exemplo n.º 4
0
        public void CreateNewConfiguration() {
            DeleteConfig();

            var client = new ExceptionlessClient();
            LocalConfigurationDictionary localConfiguration = LocalConfigurationDictionary.Create(DEFAULT_STORE, client);
            Assert.NotNull(localConfiguration);
            Assert.False(localConfiguration.IsDirty);
            Assert.NotEqual(Guid.Empty, localConfiguration.InstallIdentifier);

            using (var store = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                Assert.True(store.FileExists(CONFIG_FILENAME));
                Console.WriteLine(store.ReadFileAsString(CONFIG_FILENAME));
            }
        }
        internal static LocalConfigurationDictionary Create(string storeId, IExceptionlessLogAccessor logAccessor)
        {
            var localStorage = new LocalConfigurationDictionary {
                LogAccessor = logAccessor,
                StoreId     = storeId,
                CurrentConfigurationVersion = 0,
                NextConfigurationUpdate     = DateTime.MinValue
            };

            try {
                using (new SingleGlobalInstance(String.Concat(storeId, FileName).GetHashCode().ToString(), 500)) {
                    // retry loop
                    for (int retry = 0; retry < 2; retry++)
                    {
                        using (var dir = new IsolatedStorageDirectory(storeId)) {
                            try {
                                if (!dir.FileExists(FileName))
                                {
                                    break;
                                }

                                var config = dir.ReadFile <LocalConfigurationDictionary>(FileName);
                                foreach (string key in config.Keys)
                                {
                                    localStorage[key] = config[key];
                                }

                                localStorage.IsDirty = false;
                                break;
                            } catch (Exception ex) {
                                // File is being used by another process or thread or the file does not exist.
                                logAccessor.Log.FormattedError(typeof(LocalConfigurationDictionary), ex, "Unable to read data from local storage: {0}", ex.Message);
                                Thread.Sleep(50);
                            }
                        } // using stream
                    }     // retry

                    // TODO: Why are we doing this even if the configuration couldn't be read or didn't exist??
                    localStorage.LoadDefaults();
                    localStorage.Save();

                    return(localStorage);
                } // lock
            } catch (Exception ex) {
                logAccessor.Log.Error(ex, "An error occurred while saving local configuration");
                localStorage.LoadDefaults();
                return(localStorage);
            }
        }
Exemplo n.º 6
0
        public void CreateNewConfiguration()
        {
            DeleteConfig();

            var client = new ExceptionlessClient();
            LocalConfigurationDictionary localConfiguration = LocalConfigurationDictionary.Create(DEFAULT_STORE, client);

            Assert.NotNull(localConfiguration);
            Assert.False(localConfiguration.IsDirty);
            Assert.NotEqual(Guid.Empty, localConfiguration.InstallIdentifier);

            using (var store = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                Assert.True(store.FileExists(CONFIG_FILENAME));
                Console.WriteLine(store.ReadFileAsString(CONFIG_FILENAME));
            }
        }
Exemplo n.º 7
0
        public void MultiThreadedSaveLocalConfiguration()
        {
            DeleteConfig();
            var client = new ExceptionlessClient();

            Parallel.For(0, 200, i => {
                Exception exception = Record.Exception(() => {
                    LocalConfigurationDictionary localConfiguration = LocalConfigurationDictionary.Create(DEFAULT_STORE, client);
                    Assert.NotNull(localConfiguration);

                    localConfiguration.IsDirty            = true;
                    localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i).ToString();
                    localConfiguration.StartCount++;
                    Task.Factory.StartNew(() => localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i + 1).ToString());
                    localConfiguration.Save();

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));

                    localConfiguration.IsDirty            = true;
                    localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i).ToString();
                    Task.Factory.StartNew(() => localConfiguration.Remove("ExpireTokenDate"));
                    localConfiguration.Save();

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));

                    localConfiguration.StartCount++;
                    localConfiguration.IsDirty = true;
                    Assert.True(localConfiguration.Save(), "Saved");

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));
                });

                Assert.Null(exception);
            });

            using (var store = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                Console.WriteLine(store.GetFullPath(CONFIG_FILENAME));
                Assert.True(store.FileExists(CONFIG_FILENAME));
                Console.WriteLine(store.ReadFileAsString(CONFIG_FILENAME));
            }
        }
        public void CanEnableLoggingFromConfig() {
            var client = new ExceptionlessClient();
            ClientConfiguration config = ClientConfiguration.Create(client);
            string logPath = config.LogPath;
            Assert.NotNull(config);

            Assert.True(config.EnableLogging);
            //Assert.Equal(logPath, config.LogPath); // TODO: Should this even be set to a value?? It's Isolated storage?
            Assert.NotNull(client.Log);
            Assert.NotNull(client);

            Assert.Equal(typeof(SafeExceptionlessLog), client.Log.GetType());

            client.ProcessQueue();
            client.Shutdown();

            var dir = new IsolatedStorageDirectory(DEFAULT_STORE);
            Assert.True(dir.FileExists(logPath));
            string content = dir.ReadFileAsString(logPath);
            Assert.True(content.Length > 10);
        }
Exemplo n.º 9
0
        private static void ReadSavedConfiguration(ClientConfiguration configuration, IExceptionlessLogAccessor logAccessor)
        {
            try {
                for (int retry = 0; retry < 2; retry++)
                {
                    using (var dir = new IsolatedStorageDirectory(configuration.StoreId)) {
                        try {
                            if (!dir.FileExists(ClientConfiguration.CachedServerConfigFile))
                            {
                                return;
                            }

                            var savedConfig = dir.ReadFile <Dictionary <string, string> >(ClientConfiguration.CachedServerConfigFile);
                            if (savedConfig == null)
                            {
                                return;
                            }

                            foreach (string key in savedConfig.Keys)
                            {
                                configuration[key] = savedConfig[key];
                            }

                            return;
                        } catch (JsonReaderException) {
                            // try deleting the invalid config file so we don't keep trying to read it.
                            try {
                                dir.DeleteFile(ClientConfiguration.CachedServerConfigFile);
                            } catch {}
                        } catch (Exception ex) {
                            // File is being used by another process or thread or the file does not exist.
                            logAccessor.Log.FormattedError(typeof(ClientConfigurationReader), ex, "Error while reading settings from the configuration file: {0}", ex.Message);
                            Thread.Sleep(50);
                        }
                    } // storage
                }     // retry
            } catch (Exception ex) {
                logAccessor.Log.FormattedError(typeof(ClientConfigurationReader), ex, "Error while reading settings from the configuration file: {0}", ex.Message);
            }
        }
        private static void ReadSavedConfiguration(ClientConfiguration configuration, IExceptionlessLogAccessor logAccessor) {
            try {
                for (int retry = 0; retry < 2; retry++) {
                    using (var dir = new IsolatedStorageDirectory(configuration.StoreId)) {
                        try {
                            if (!dir.FileExists(ClientConfiguration.CachedServerConfigFile))
                                return;

                            var savedConfig = dir.ReadFile<Dictionary<string, string>>(ClientConfiguration.CachedServerConfigFile);
                            if (savedConfig == null)
                                return;

                            foreach (string key in savedConfig.Keys)
                                configuration[key] = savedConfig[key];

                            return;
                        } catch (JsonReaderException) {
                            // try deleting the invalid config file so we don't keep trying to read it.
                            try {
                                dir.DeleteFile(ClientConfiguration.CachedServerConfigFile);
                            } catch {}
                        } catch (Exception ex) {
                            // File is being used by another process or thread or the file does not exist.
                            logAccessor.Log.FormattedError(typeof(ClientConfigurationReader), ex, "Error while reading settings from the configuration file: {0}", ex.Message);
                            Thread.Sleep(50);
                        }
                    } // storage
                } // retry
            } catch (Exception ex) {
                logAccessor.Log.FormattedError(typeof(ClientConfigurationReader), ex, "Error while reading settings from the configuration file: {0}", ex.Message);
            }
        }
Exemplo n.º 11
0
        public void MultiThreadedSaveLocalConfiguration() {
            DeleteConfig();
            var client = new ExceptionlessClient();

            Parallel.For(0, 200, i => {
                Exception exception = Record.Exception(() => {
                    LocalConfigurationDictionary localConfiguration = LocalConfigurationDictionary.Create(DEFAULT_STORE, client);
                    Assert.NotNull(localConfiguration);

                    localConfiguration.IsDirty = true;
                    localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i).ToString();
                    localConfiguration.StartCount++;
                    Task.Factory.StartNew(() => localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i + 1).ToString());
                    localConfiguration.Save();

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));

                    localConfiguration.IsDirty = true;
                    localConfiguration["ExpireTokenDate"] = DateTime.Now.AddMinutes(i).ToString();
                    Task.Factory.StartNew(() => localConfiguration.Remove("ExpireTokenDate"));
                    localConfiguration.Save();

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));

                    localConfiguration.StartCount++;
                    localConfiguration.IsDirty = true;
                    Assert.True(localConfiguration.Save(), "Saved");

                    Assert.NotNull(LocalConfigurationDictionary.Create(DEFAULT_STORE, client));
                });

                Assert.Null(exception);
            });

            using (var store = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                Console.WriteLine(store.GetFullPath(CONFIG_FILENAME));
                Assert.True(store.FileExists(CONFIG_FILENAME));
                Console.WriteLine(store.ReadFileAsString(CONFIG_FILENAME));
            }
        }
Exemplo n.º 12
0
 private void DeleteConfig(string storeId = DEFAULT_STORE) {
     using (var dir = new IsolatedStorageDirectory(storeId)) {
         if (dir.FileExists(CONFIG_FILENAME))
             dir.DeleteFile(CONFIG_FILENAME);
     }
 }
 protected override bool LogExists(string path = LOG_FILE)
 {
     return(_directory.FileExists(path));
 }
        public void CanHandleInvalidCachedServerConfig() {
            using (var dir = new IsolatedStorageDirectory(DEFAULT_STORE)) {
                dir.WriteFile(CONFIG_FILENAME, "sadf<sdf>");

                Assert.True(dir.FileExists(CONFIG_FILENAME));

                var client = new ExceptionlessClient();
                ClientConfiguration config = ClientConfiguration.Create(client);

                // file should get deleted if it's invalid
                Assert.False(dir.FileExists(CONFIG_FILENAME));

                Assert.NotNull(config);

                Assert.True(config.ContainsKey("AttributeOnly"));
                Assert.Equal(config["AttributeOnly"], "Attribute");

                Assert.True(config.ContainsKey("UserNamespaces"));
                Assert.Equal(config["UserNamespaces"], "Exceptionless,FromConfig");

                Assert.True(config.ContainsKey("ConfigAndAttribute"));
                Assert.Equal(config["ConfigAndAttribute"], "Config");

                Assert.True(config.ContainsKey("AppConfigOnly"));
                Assert.Equal(config["AppConfigOnly"], "Config");
            }
        }