コード例 #1
0
        /// <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="exception"/> or
        /// <paramref name="transaction"/> are null (Nothing in VB) references.</exception>
        protected override 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);
            }
        }
コード例 #2
0
        /// <summary>Saves the event to database.</summary>
        /// <param name="transaction">The transaction.</param>
        /// <param name="severity">The event severity.</param>
        /// <param name="message">The message.</param>
        /// <param name="source">The source.</param>
        /// <returns>The database's primary key of the saved event.</returns>
        protected override int SaveEventToDatabase(SqlTransaction transaction, LoggingEventType severity,
                                                   string message, string source)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }

            var requestLogData =
                new RequestLogData(HttpContext.Current, this.logQueryString, this.logFormData);

            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);

                SqlLoggingHelper.AddParameter(command, "MachineName", SqlDbType.NVarChar, Environment.MachineName);
                SqlLoggingHelper.AddParameter(command, "ApplicationName", SqlDbType.NVarChar, this.ApplicationName);
                SqlLoggingHelper.AddParameter(command, "UserName", SqlDbType.NVarChar, this.GetCurrentUserName());
                SqlLoggingHelper.AddParameter(command, "IpAddress", SqlDbType.NVarChar, requestLogData.IpAddress);
                SqlLoggingHelper.AddParameter(command, "QueryString", SqlDbType.NText, requestLogData.QueryString);
                SqlLoggingHelper.AddParameter(command, "FormData", SqlDbType.NText, requestLogData.Form);

                object eventId = command.ExecuteScalar();

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

                return((int)eventId);
            }
        }