/// <summary>
        /// Initializes a new instance of the <see cref="AzureTableStorageErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public AzureTableStorageErrorLog(IDictionary config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var connectionString = ElmahHelper.GetConnectionString(config);

            //
            // If there is no connection string to use then throw an
            // exception to abort construction.
            //

            if (connectionString.Length == 0)
            {
                throw new ApplicationException("Connection string is missing for the Azure Table Storage error log.");
            }

            //
            // Get custom table name for storage and validate
            //

            var tableName = ElmahHelper.GetTableName(config);

            if (tableName.Length == 0)
            {
                tableName = DefaultTableName;
            }

            if (!Regex.IsMatch(tableName, TableValidationRegex))
            {
                throw new ApplicationException("Name for table in Azure Table Storage is not a valid name.");
            }

            var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
            var tableClient         = cloudStorageAccount.CreateCloudTableClient();

            _cloudTable = tableClient.GetTableReference(tableName);
            _cloudTable.CreateIfNotExists();

            //
            // Set the application name as this implementation provides
            // per-application isolation over a single store.
            //

            var appName = ElmahHelper.GetApplicationName(config);

            if (appName.Length > MaxAppNameLength)
            {
                throw new ApplicationException(string.Format(
                                                   "Application name is too long. Maximum length allowed is {0} characters.",
                                                   MaxAppNameLength.ToString("N0")));
            }

            ApplicationName = appName;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureTableStorageErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public AzureTableStorageErrorLog(IDictionary config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var connectionString = ElmahHelper.GetConnectionString(config);

            //
            // If there is no connection string to use then throw an
            // exception to abort construction.
            //
            if (connectionString.Length == 0)
            {
                throw new ApplicationException("Connection string is missing for the Azure Table Storage error log.");
            }

            this.storageAccount = CloudStorageAccount.Parse(connectionString);
            var tableClient = storageAccount.CreateCloudTableClient();

            _cloudTable = tableClient.GetTableReference(this.GetElmahLogTableName());
            _cloudTable.CreateIfNotExists();

            //
            // Set the application name as this implementation provides
            // per-application isolation over a single store.
            //

            var appName = config.Find("applicationName", string.Empty);

            if (appName.Length > MaxAppNameLength)
            {
                throw new ApplicationException(string.Format(
                                                   "Application name is too long. Maximum length allowed is {0} characters.",
                                                   MaxAppNameLength.ToString("N0")));
            }

            ApplicationName = appName;
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureTableStorageErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public AzureTableStorageErrorLog(IDictionary config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            #region Read properties of ErrorLog config item

            //
            // Set the application name as this implementation provides
            // per-application isolation over a single store.
            //

            var appName = config.Find("applicationName", string.Empty);

            //
            // Set the table name. This implementation provides
            // table-per-application-config isolation over a single store.
            //

            var tableName = config.Find("tableName", string.Empty);

            #endregion Read properties of ErrorLog config item

            #region Read properties of ErrorLog connection string

            var cfgBuilder = new System.Data.Common.DbConnectionStringBuilder();
            var originalConnectionString = ElmahHelper.GetConnectionString(config);
            cfgBuilder.ConnectionString = originalConnectionString;

            // Try to override ApplicationName from connection string
            if (cfgBuilder.ContainsKey("ApplicationName"))
            {
                appName = (string)cfgBuilder["ApplicationName"];
                cfgBuilder.Remove("ApplicationName");
            }

            // Try to override TableName from connection string
            if (cfgBuilder.ContainsKey("TableName"))
            {
                tableName = (string)cfgBuilder["TableName"];
                cfgBuilder.Remove("TableName");
            }

            #endregion Read properties of ErrorLog connection string

            if (appName.Length > MaxAppNameLength)
            {
                throw new ApplicationException(string.Format(
                                                   "Application name is too long. Maximum length allowed is {0} characters.",
                                                   MaxAppNameLength.ToString("N0")));
            }

            if (tableName.Length > MaxTableNameLength)
            {
                throw new ApplicationException(string.Format(
                                                   "Table name is too long. Maximum length allowed is {0} characters.",
                                                   MaxTableNameLength.ToString("N0")));
            }

            if (!string.IsNullOrWhiteSpace(appName))
            {
                ApplicationName = appName;
            }

            if (!string.IsNullOrWhiteSpace(tableName))
            {
                TableName = tableName;
            }

            var newConnectionString        = string.Join(";", originalConnectionString.Split(';').Where(item => !item.StartsWith("ApplicationName=", StringComparison.OrdinalIgnoreCase) && !item.StartsWith("TableName=", StringComparison.OrdinalIgnoreCase)));
            var newConnectionStringBuilder = new System.Data.Common.DbConnectionStringBuilder();
            newConnectionStringBuilder.ConnectionString = newConnectionString;

            if (!cfgBuilder.EquivalentTo(newConnectionStringBuilder))
            {
                throw new ApplicationException("Connection string contains invalid parameters.");
            }

            //
            // If there is no connection string to use then throw an
            // exception to abort construction.
            //

            if (cfgBuilder.ConnectionString.Length == 0)
            {
                throw new ApplicationException("Connection string is missing for the Azure Table Storage error log.");
            }

            var cloudStorageAccount = CloudStorageAccount.Parse(newConnectionString);
            var tableClient         = cloudStorageAccount.CreateCloudTableClient();
            _cloudTable = tableClient.GetTableReference(TableName);
            _cloudTable.CreateIfNotExists();
        }