/// <summary>Saves the event to database.</summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="severity">The severity of the event.</param>
        /// <param name="message">The message.</param>
        /// <param name="source">The source.</param>
        /// <returns>The database's primary key of the saved event.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="transaction"/> is a null
        /// (Nothing in VB) reference.</exception>
        protected virtual int SaveEventToDatabase(SqlTransaction transaction, LoggingEventType severity,
                                                  string message, string source)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            using (var command = new SqlCommand(AddEventProcedure, transaction.Connection, transaction))
            {
                command.CommandType = CommandType.StoredProcedure;

                SqlLoggingHelper.AddParameter(command, "EventTypeId", SqlDbType.Int, (int)severity);
                SqlLoggingHelper.AddParameter(command, "Message", SqlDbType.NText, message);
                SqlLoggingHelper.AddParameter(command, "Source", SqlDbType.NText, source);

                object eventId = command.ExecuteScalar();

                if (!(eventId is int))
                {
                    throw new InvalidOperationException(SR.StoredProcedureReturnedInvalidValue(
                                                            AddEventProcedure, eventId, typeof(int)));
                }

                return((int)eventId);
            }
        }
        /// <summary>Saves the exception to database.</summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="exception">The exception.</param>
        /// <param name="parentEventId">The parent event id.</param>
        /// <param name="parentExceptionId">The parent exception id.</param>
        /// <returns>The database's primary key of the saved exception.</returns>
        /// <exception cref="ArgumentNullException">Thrown when either <paramref name="transaction"/> or
        /// <paramref name="exception"/> are null (Nothing in VB) references.</exception>
        protected virtual int SaveExceptionToDatabase(SqlTransaction transaction, Exception exception,
                                                      int parentEventId, int?parentExceptionId)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            using (var command = new SqlCommand(AddExceptionProcedure, transaction.Connection, transaction))
            {
                command.CommandType = CommandType.StoredProcedure;

                SqlLoggingHelper.AddParameter(command, "EventId", SqlDbType.Int, parentEventId);
                SqlLoggingHelper.AddParameter(command, "ParentExceptionId", SqlDbType.Int, parentExceptionId);
                SqlLoggingHelper.AddParameter(command, "ExceptionType", SqlDbType.NVarChar, exception.GetType().Name);
                SqlLoggingHelper.AddParameter(command, "Message", SqlDbType.NText, exception.Message);
                SqlLoggingHelper.AddParameter(command, "StackTrace", SqlDbType.NText, exception.StackTrace);

                object exceptionId = command.ExecuteScalar();

                if (!(exceptionId is int))
                {
                    throw new InvalidOperationException(SR.StoredProcedureReturnedInvalidValue(
                                                            AddExceptionProcedure, exceptionId, typeof(int)));
                }

                return((int)exceptionId);
            }
        }
        private bool GetInitializeSchemaAttributeFromConfig(NameValueCollection config)
        {
            const string InitializeSchemaAttribute = "initializeSchema";

            string initializeSchema = config[InitializeSchemaAttribute];

            // Remove this attribute from the configuration. This way the provider can spot unrecognized
            // attributes after the initialization process.
            config.Remove(InitializeSchemaAttribute);

            const bool DefaultValueWhenMissing = false;

            return(SqlLoggingHelper.ParseBoolConfigValue(this.Name, InitializeSchemaAttribute,
                                                         initializeSchema, DefaultValueWhenMissing));
        }
        /// <summary>Initializes the database schema.</summary>
        protected virtual void InitializeDatabaseSchema()
        {
            try
            {
                SqlLoggingHelper.ThrowWhenSchemaAlreadyHasBeenInitialized(this);

                string createScript = SR.SqlLoggingProviderSchemaScripts();

                // Split the script in separate operations. SQL Server chokes on the GO statements.
                string[] createScripts = createScript.Split(new string[] { "GO" }, StringSplitOptions.None);

                SqlLoggingHelper.CreateTablesAndStoredProcedures(this, createScripts);
            }
            catch (SqlException ex)
            {
                throw new ProviderException(SR.InitializationOfDatabaseSchemaFailed(this.Name, ex.Message), ex);
            }
        }