Exemplo n.º 1
0
        /// <summary>
        /// Logs an error to the database.
        /// </summary>
        /// <remarks>
        /// Use the stored procedure called by this implementation to set a
        /// policy on how long errors are kept in the log. The default
        /// implementation stores all errors for an indefinite time.
        /// </remarks>

        public override string Log(Error error)
        {
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            string errorXml = ErrorXml.EncodeString(error);

            using (VistaDBConnection connection = new VistaDBConnection(this.ConnectionString))
                using (VistaDBCommand command = connection.CreateCommand())
                {
                    connection.Open();
                    command.CommandText = @"INSERT INTO ELMAH_Error
                                            (Application, Host, Type, Source, 
                                            Message, [User], AllXml, StatusCode, TimeUtc)
                                        VALUES
                                            (@Application, @Host, @Type, @Source,
                                            @Message, @User, @AllXml, @StatusCode, @TimeUtc);

                                        SELECT @@IDENTITY";
                    command.CommandType = CommandType.Text;

                    VistaDBParameterCollection parameters = command.Parameters;
                    parameters.Add("@Application", VistaDBType.NVarChar, _maxAppNameLength).Value = ApplicationName;
                    parameters.Add("@Host", VistaDBType.NVarChar, 30).Value     = error.HostName;
                    parameters.Add("@Type", VistaDBType.NVarChar, 100).Value    = error.Type;
                    parameters.Add("@Source", VistaDBType.NVarChar, 60).Value   = error.Source;
                    parameters.Add("@Message", VistaDBType.NVarChar, 500).Value = error.Message;
                    parameters.Add("@User", VistaDBType.NVarChar, 50).Value     = error.User;
                    parameters.Add("@AllXml", VistaDBType.NText).Value          = errorXml;
                    parameters.Add("@StatusCode", VistaDBType.Int).Value        = error.StatusCode;
                    parameters.Add("@TimeUtc", VistaDBType.DateTime).Value      = error.Time.ToUniversalTime();

                    return(Convert.ToString(command.ExecuteScalar(), CultureInfo.InvariantCulture));
                }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the specified error from the database, or null
        /// if it does not exist.
        /// </summary>

        public override ErrorLogEntry GetError(string id)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            if (id.Length == 0)
            {
                throw new ArgumentException(null, "id");
            }

            int errorId;

            try
            {
                errorId = int.Parse(id, CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                throw new ArgumentException(e.Message, "id", e);
            }
            catch (OverflowException e)
            {
                throw new ArgumentException(e.Message, "id", e);
            }

            string errorXml;

            using (VistaDBConnection connection = new VistaDBConnection(this.ConnectionString))
                using (VistaDBCommand command = connection.CreateCommand())
                {
                    command.CommandText = @"SELECT  AllXml
                                        FROM    ELMAH_Error
                                        WHERE   ErrorId = @ErrorId";
                    command.CommandType = CommandType.Text;

                    VistaDBParameterCollection parameters = command.Parameters;
                    parameters.Add("@ErrorId", VistaDBType.Int).Value = errorId;

                    connection.Open();

                    // NB this has been deliberately done like this as command.ExecuteScalar
                    // is not exhibiting the expected behaviour in VistaDB at the moment
                    using (VistaDBDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            errorXml = dr[0] as string;
                        }
                        else
                        {
                            errorXml = null;
                        }
                    }
                }

            if (errorXml == null)
            {
                return(null);
            }

            Error error = ErrorXml.DecodeString(errorXml);

            return(new ErrorLogEntry(this, id, error));
        }