/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { var logPath = LogPath; if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } var errorId = Guid.NewGuid().ToString(); var timeStamp = (error.Time > DateTime.MinValue ? error.Time : DateTime.Now); var fileName = string.Format(CultureInfo.InvariantCulture, @"error-{0:yyyy-MM-ddHHmmssZ}-{1}.xml", /* 0 */ timeStamp.ToUniversalTime(), /* 1 */ errorId); var path = Path.Combine(logPath, fileName); using (var writer = new XmlTextWriter(path, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } return(errorId); }
/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { string errorId = Guid.NewGuid().ToString(); string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-ddHHmmssZ", CultureInfo.InvariantCulture); string path = Path.Combine(LogPath, string.Format(@"error-{0}-{1}.xml", timeStamp, errorId)); XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8); try { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } finally { writer.Close(); } return(errorId); }
/// <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"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } string errorXml; using (OracleConnection connection = new OracleConnection(this.ConnectionString)) using (OracleCommand command = connection.CreateCommand()) { command.CommandText = SchemaOwner + "pkg_elmah$get_error.GetErrorXml"; command.CommandType = CommandType.StoredProcedure; OracleParameterCollection parameters = command.Parameters; parameters.Add("v_Application", OracleType.NVarChar, _maxAppNameLength).Value = ApplicationName; parameters.Add("v_ErrorId", OracleType.NVarChar, 32).Value = errorGuid.ToString("N"); parameters.Add("v_AllXml", OracleType.NClob).Direction = ParameterDirection.Output; connection.Open(); command.ExecuteNonQuery(); OracleLob xmlLob = (OracleLob)command.Parameters["v_AllXml"].Value; StreamReader streamreader = new StreamReader(xmlLob, Encoding.Unicode); char[] cbuffer = new char[1000]; int actual; StringBuilder sb = new StringBuilder(); while ((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) > 0) { sb.Append(cbuffer, 0, actual); } errorXml = sb.ToString(); } if (errorXml == null) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <summary> /// Logs an error in log for the application. /// </summary> public override string Log(Error error) { if (error == null) { throw new ArgumentNullException("error"); } string errorXml = ErrorXml.EncodeString(error); Guid id = Guid.NewGuid(); using (MySqlConnection cn = new MySqlConnection(ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand("Elmah_LogError", cn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pErrorId", id.ToString()); cmd.Parameters.AddWithValue("@pApplication", ApplicationName); cmd.Parameters.AddWithValue("@pHost", error.HostName); cmd.Parameters.AddWithValue("@pType", error.Type); cmd.Parameters.AddWithValue("@pSource", error.Source); cmd.Parameters.AddWithValue("@pMessage", error.Message); cmd.Parameters.AddWithValue("@pUser", error.User); cmd.Parameters.AddWithValue("@pStatusCode", error.StatusCode); cmd.Parameters.AddWithValue("@pTimeUtc", error.Time.ToUniversalTime()); cmd.Parameters.AddWithValue("@pAllXml", errorXml); cn.Open(); cmd.ExecuteNonQuery(); return(id.ToString()); } } }
/// <summary> /// Add user comments to an existing error. This is added in the base error node as an attribute userComments /// </summary> public override void AddUserComments(string id, string userComments) { if (id == null) { throw new ArgumentNullException("id"); } if (id.Length == 0) { throw new ArgumentException(null, "id"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } var someError = GetError(id); someError.Error.UserComments = userComments; using (var connection = new SqlConnection(ConnectionString)) using (var command = Commands.UpdateErrorXml(errorGuid, ErrorXml.EncodeString(someError.Error))) { command.Connection = connection; connection.Open(); command.ExecuteNonQuery(); } }
/// <summary> /// Returns the specified error from the filesystem, or throws an exception if it does not exist. /// </summary> public override ErrorLogEntry GetError(string id) { try { id = (new Guid(id)).ToString(); // validate GUID } catch (FormatException e) { throw new ArgumentException(e.Message, id, e); } var file = new DirectoryInfo(LogPath).GetFiles(string.Format("error-*-{0}.xml", id)) .FirstOrDefault(); if (file == null) { return(null); } if (!IsUserFile(file.Attributes)) { return(null); } using (var reader = XmlReader.Create(file.FullName)) return(new ErrorLogEntry(this, id, ErrorXml.Decode(reader))); }
/// <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); Guid id = Guid.NewGuid(); using (OracleConnection connection = new OracleConnection(this.ConnectionString)) using (OracleCommand command = connection.CreateCommand()) { connection.Open(); using (OracleTransaction transaction = connection.BeginTransaction()) { // because we are storing the XML data in a NClob, we need to jump through a few hoops!! // so first we've got to operate within a transaction command.Transaction = transaction; // then we need to create a temporary lob on the database server command.CommandText = "declare xx nclob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;"; command.CommandType = CommandType.Text; OracleParameterCollection parameters = command.Parameters; parameters.Add("tempblob", OracleType.NClob).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); // now we can get a handle to the NClob OracleLob xmlLob = (OracleLob)parameters[0].Value; // create a temporary buffer in which to store the XML byte[] tempbuff = Encoding.Unicode.GetBytes(errorXml); // and finally we can write to it! xmlLob.BeginBatch(OracleLobOpenMode.ReadWrite); xmlLob.Write(tempbuff, 0, tempbuff.Length); xmlLob.EndBatch(); command.CommandText = SchemaOwner + "pkg_elmah$log_error.LogError"; command.CommandType = CommandType.StoredProcedure; parameters.Clear(); parameters.Add("v_ErrorId", OracleType.NVarChar, 32).Value = id.ToString("N"); parameters.Add("v_Application", OracleType.NVarChar, _maxAppNameLength).Value = ApplicationName; parameters.Add("v_Host", OracleType.NVarChar, 30).Value = error.HostName; parameters.Add("v_Type", OracleType.NVarChar, 100).Value = error.Type; parameters.Add("v_Source", OracleType.NVarChar, 60).Value = error.Source; parameters.Add("v_Message", OracleType.NVarChar, 500).Value = error.Message; parameters.Add("v_User", OracleType.NVarChar, 50).Value = error.User; parameters.Add("v_AllXml", OracleType.NClob).Value = xmlLob; parameters.Add("v_StatusCode", OracleType.Int32).Value = error.StatusCode; parameters.Add("v_TimeUtc", OracleType.DateTime).Value = error.Time.ToUniversalTime(); command.ExecuteNonQuery(); transaction.Commit(); } return(id.ToString()); } }
public void ProcessRequest(HttpContext context) { HttpResponse response = context.Response; response.ContentType = "application/xml"; // // Retrieve the ID of the requested error and read it from // the store. // string errorId = Mask.NullString(context.Request.QueryString["id"]); if (errorId.Length == 0) { throw new ApplicationException("Missing error identifier specification."); } ErrorLogEntry entry = ErrorLog.GetDefault(context).GetError(errorId); // // Perhaps the error has been deleted from the store? Whatever // the reason, pretend it does not exist. // if (entry == null) { throw new HttpException((int)HttpStatusCode.NotFound, string.Format("Error with ID '{0}' not found.", errorId)); } // // Stream out the error as formatted XML. // #if !NET_1_0 && !NET_1_1 XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.CheckCharacters = false; XmlWriter writer = XmlWriter.Create(response.Output, settings); #else XmlTextWriter writer = new XmlTextWriter(response.Output); writer.Formatting = Formatting.Indented; #endif writer.WriteStartDocument(); writer.WriteStartElement("error"); ErrorXml.Encode(entry.Error, writer); writer.WriteEndElement(/* error */); writer.WriteEndDocument(); writer.Flush(); }
/// <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 (OleDbConnection connection = new OleDbConnection(this.ConnectionString)) using (OleDbCommand command = connection.CreateCommand()) { command.CommandText = @"SELECT AllXml FROM ELMAH_Error WHERE ErrorId = @ErrorId"; command.CommandType = CommandType.Text; OleDbParameterCollection parameters = command.Parameters; parameters.Add("@ErrorId", OleDbType.Integer).Value = errorId; connection.Open(); errorXml = (string)command.ExecuteScalar(); } if (errorXml == null) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <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"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } string errorXml = null; using (var connection = new MySqlConnection(ConnectionString)) using (var command = Commands.GetErrorXml(ApplicationName, errorGuid)) { command.Connection = connection; connection.Open(); using (var reader = command.ExecuteReader()) { Debug.Assert(reader != null); while (reader.Read()) { errorXml = reader.GetString("AllXml"); } reader.Close(); } } if (errorXml == null) { return(null); } var error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <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"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } string errorXml; using (var connection = CreateOpenConnection()) using (var command = connection.CreateCommand()) { command.CommandText = SchemaOwner + "pkg_elmah$get_error.GetErrorXml"; command.CommandType = CommandType.StoredProcedure; var addParameter = command.ParameterAdder(); addParameter("v_Application", DbType.String, ApplicationName); addParameter("v_ErrorId", DbType.String, errorGuid.ToString("N")); var allXml = AddProviderSpecificTypeParameter(command, "v_AllXml", ThisProviderInfo.ClobDbType); allXml.Direction = ParameterDirection.Output; command.ExecuteNonQuery(); errorXml = allXml.Value as string; if (errorXml == null) { // TODO Review whether Stream needs disposing var stream = (Stream)allXml.Value; var reader = new StreamReader(stream, Encoding.Unicode); errorXml = reader.ReadToEnd(); } } var error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <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"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } const string sql = @" SELECT [AllXml] FROM [ELMAH_Error] WHERE [ErrorId] = @ErrorId"; using (SqlCeConnection connection = new SqlCeConnection(ConnectionString)) { using (SqlCeCommand command = new SqlCeCommand(sql, connection)) { command.Parameters.Add("@ErrorId", SqlDbType.UniqueIdentifier).Value = errorGuid; connection.Open(); string errorXml = (string)command.ExecuteScalar(); if (errorXml == null) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); } } }
private ErrorLogEntry LoadErrorLogEntry(string path) { using (var reader = XmlReader.Create(path)) { if (!reader.IsStartElement("error")) { return(null); } var id = reader.GetAttribute("errorId"); var error = ErrorXml.Decode(reader); return(new ErrorLogEntry(this, id, error)); } }
/// <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"); } long key; try { key = long.Parse(id, CultureInfo.InvariantCulture); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } const string sql = @" SELECT AllXml FROM Error WHERE ErrorId = @ErrorId"; using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) using (SQLiteCommand command = new SQLiteCommand(sql, connection)) { SQLiteParameterCollection parameters = command.Parameters; parameters.Add("@ErrorId", DbType.Int64).Value = key; connection.Open(); string errorXml = (string)command.ExecuteScalar(); if (errorXml == null) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); } }
private void ErrorsXmlToList(XmlReader reader, IList errorEntryList) { Debug.Assert(reader != null); if (errorEntryList != null) { while (reader.IsStartElement("error")) { string id = reader.GetAttribute("errorId"); Error error = ErrorXml.Decode(reader); errorEntryList.Add(new ErrorLogEntry(this, id, error)); } } }
/// <summary> /// Retrieves a single application error from log given its /// identifier, or null if it does not exist. /// </summary> public override ErrorLogEntry GetError(string id) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException("id"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (Exception e) { throw new ArgumentException(e.Message, "id", e); } string errorXml = null; using (MySqlConnection cn = new MySqlConnection(ConnectionString)) { using (MySqlCommand cmd = new MySqlCommand("Elmah_GetErrorXml", cn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pApplication", ApplicationName); cmd.Parameters.AddWithValue("@pErrorId", id); cn.Open(); using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { errorXml = reader["AllXml"].ToString(); } } } } if (string.IsNullOrEmpty(errorXml)) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <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 (OleDbConnection connection = new OleDbConnection(this.ConnectionString)) using (OleDbCommand command = connection.CreateCommand()) { connection.Open(); command.CommandType = CommandType.Text; command.CommandText = @"INSERT INTO ELMAH_Error (Application, Host, Type, Source, Message, UserName, StatusCode, TimeUtc, AllXml) VALUES (@Application, @Host, @Type, @Source, @Message, @UserName, @StatusCode, @TimeUtc, @AllXml)"; command.CommandType = CommandType.Text; OleDbParameterCollection parameters = command.Parameters; parameters.Add("@Application", OleDbType.VarChar, _maxAppNameLength).Value = ApplicationName; parameters.Add("@Host", OleDbType.VarChar, 30).Value = error.HostName; parameters.Add("@Type", OleDbType.VarChar, 100).Value = error.Type; parameters.Add("@Source", OleDbType.VarChar, 60).Value = error.Source; parameters.Add("@Message", OleDbType.LongVarChar, error.Message.Length).Value = error.Message; parameters.Add("@User", OleDbType.VarChar, 50).Value = error.User; parameters.Add("@StatusCode", OleDbType.Integer).Value = error.StatusCode; parameters.Add("@TimeUtc", OleDbType.Date).Value = error.Time.ToUniversalTime(); parameters.Add("@AllXml", OleDbType.LongVarChar, errorXml.Length).Value = errorXml; command.ExecuteNonQuery(); using (OleDbCommand identityCommand = connection.CreateCommand()) { identityCommand.CommandType = CommandType.Text; identityCommand.CommandText = "SELECT @@IDENTITY"; return(Convert.ToString(identityCommand.ExecuteScalar(), CultureInfo.InvariantCulture)); } } }
/// <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); Guid id = Guid.NewGuid(); const string query = @" INSERT INTO ELMAH_Error ( [ErrorId], [Application], [Host], [Type], [Source], [Message], [User], [StatusCode], [TimeUtc], [AllXml] ) VALUES ( @ErrorId, @Application, @Host, @Type, @Source, @Message, @User, @StatusCode, @TimeUtc, @AllXml);"; using (SqlCeConnection connection = new SqlCeConnection(ConnectionString)) { using (SqlCeCommand command = new SqlCeCommand(query, connection)) { SqlCeParameterCollection parameters = command.Parameters; parameters.Add("@ErrorId", SqlDbType.UniqueIdentifier).Value = id; parameters.Add("@Application", SqlDbType.NVarChar, 60).Value = ApplicationName; parameters.Add("@Host", SqlDbType.NVarChar, 30).Value = error.HostName; parameters.Add("@Type", SqlDbType.NVarChar, 100).Value = error.Type; parameters.Add("@Source", SqlDbType.NVarChar, 60).Value = error.Source; parameters.Add("@Message", SqlDbType.NVarChar, 500).Value = error.Message; parameters.Add("@User", SqlDbType.NVarChar, 50).Value = error.User; parameters.Add("@StatusCode", SqlDbType.Int).Value = error.StatusCode; parameters.Add("@TimeUtc", SqlDbType.DateTime).Value = error.Time.ToUniversalTime(); parameters.Add("@AllXml", SqlDbType.NText).Value = errorXml; command.Connection = connection; connection.Open(); command.ExecuteNonQuery(); return(id.ToString()); } } }
/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { var logPath = LogPath; if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } var errorId = Guid.NewGuid().ToString(); var timeStamp = (error.Time > DateTime.MinValue ? error.Time : DateTime.Now); var fileName = string.Format(CultureInfo.InvariantCulture, @"error-{0:yyyy-MM-ddHHmmssZ}-{1}.xml", /* 0 */ timeStamp.ToUniversalTime(), /* 1 */ errorId); var path = Path.Combine(logPath, fileName); try { using (var writer = new XmlTextWriter(path, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } } catch (IOException) { // If an IOException is thrown during writing the file, // it means that we will have an either empty or // partially written XML file on disk. In both cases, // the file won't be valid and would cause an error in // the UI. File.Delete(path); throw; } return(errorId); }
public override string Log(Error error) { if (error == null) { throw new ArgumentNullException("error"); } string errorXml = ErrorXml.EncodeString(error); Guid id = Guid.NewGuid(); using (NpgsqlConnection connection = new NpgsqlConnection(ConnectionString)) using (NpgsqlCommand command = Commands.LogError(id, this.ApplicationName, error.HostName, error.Type, error.Source, error.Message, error.User, error.StatusCode, error.Time, errorXml)) { command.Connection = connection; connection.Open(); command.ExecuteNonQuery(); return(id.ToString()); } }
/// <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"); } Guid errorGuid; try { errorGuid = new Guid(id); } catch (FormatException e) { throw new ArgumentException(e.Message, "id", e); } string errorXml; using (SqlConnection connection = new SqlConnection(this.ConnectionString)) using (SqlCommand command = Commands.GetErrorXml(this.ApplicationName, errorGuid)) { command.Connection = connection; connection.Open(); errorXml = (string)command.ExecuteScalar(); } if (errorXml == null) { return(null); } Error error = ErrorXml.DecodeString(errorXml); return(new ErrorLogEntry(this, id, error)); }
/// <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); const string query = @" INSERT INTO Error ( Application, Host, Type, Source, Message, User, StatusCode, TimeUtc, AllXml) VALUES ( @Application, @Host, @Type, @Source, @Message, @User, @StatusCode, @TimeUtc, @AllXml); SELECT last_insert_rowid();"; using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) using (SQLiteCommand command = new SQLiteCommand(query, connection)) { SQLiteParameterCollection parameters = command.Parameters; parameters.Add("@Application", DbType.String, 60).Value = ApplicationName; parameters.Add("@Host", DbType.String, 30).Value = error.HostName; parameters.Add("@Type", DbType.String, 100).Value = error.Type; parameters.Add("@Source", DbType.String, 60).Value = error.Source; parameters.Add("@Message", DbType.String, 500).Value = error.Message; parameters.Add("@User", DbType.String, 50).Value = error.User; parameters.Add("@StatusCode", DbType.Int64).Value = error.StatusCode; parameters.Add("@TimeUtc", DbType.DateTime).Value = error.Time.ToUniversalTime(); parameters.Add("@AllXml", DbType.String).Value = errorXml; connection.Open(); return(Convert.ToInt64(command.ExecuteScalar()).ToString(CultureInfo.InvariantCulture)); } }
/// <summary> /// Returns the specified error from the filesystem, or throws an exception if it does not exist. /// </summary> public override ErrorLogEntry GetError(string id) { try { /* Make sure the identifier is a valid GUID */ id = (new Guid(id)).ToString(); } catch (FormatException e) { throw new ArgumentException(e.Message, id, e); } /* Get the file folder list - should only return one ever */ DirectoryInfo dir = new DirectoryInfo(LogPath); FileInfo[] files = dir.GetFiles(string.Format("error-*-{0}.xml", id)); if (files.Length < 1) { return(null); } FileInfo file = files[0]; if (!IsUserFile(file.Attributes)) { return(null); } XmlTextReader reader = new XmlTextReader(file.FullName); try { Error error = ErrorXml.Decode(reader); return(new ErrorLogEntry(this, id, error)); } finally { reader.Close(); } }
public override ErrorLogEntry GetError(string id) { ErrorLogEntry result; ErrorDocument document; using (var session = _documentStore.OpenSession()) { document = session.Load <ErrorDocument>(id); } if (!string.IsNullOrEmpty(document.ErrorXml)) { result = new ErrorLogEntry(this, id, ErrorXml.DecodeString(document.ErrorXml)); } else { result = new ErrorLogEntry(this, id, document.Error); } return(result); }
public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList) { if (pageIndex < 0) { throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null); } if (pageSize < 0) { throw new ArgumentOutOfRangeException("pageSize", pageSize, null); } using (NpgsqlConnection connection = new NpgsqlConnection(ConnectionString)) { connection.Open(); using (NpgsqlCommand command = Commands.GetErrorsXml(this.ApplicationName, pageIndex, pageSize)) { command.Connection = connection; using (NpgsqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string id = reader.GetString(0); string xml = reader.GetString(1); Error error = ErrorXml.DecodeString(xml); errorEntryList.Add(new ErrorLogEntry(this, id, error)); } } } using (NpgsqlCommand command = Commands.GetErrorsXmlTotal(this.ApplicationName)) { command.Connection = connection; return(Convert.ToInt32(command.ExecuteScalar())); } } }
public override string Log(Error error) { if (error == null) { throw new ArgumentNullException("error"); } var errorXml = ErrorXml.EncodeString(error); var errorDoc = new ErrorDocument { ApplicationName = ApplicationName, Error = error, ErrorXml = errorXml }; using (var session = _documentStore.OpenSession()) { session.Store(errorDoc); session.SaveChanges(); } return(errorDoc.Id); }
/// <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)); } }
/// <summary> /// Returns a page of errors from the folder in descending order /// of logged time as defined by the sortable filenames. /// </summary> public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList) { if (pageIndex < 0) { throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null); } if (pageSize < 0) { throw new ArgumentOutOfRangeException("pageSize", pageSize, null); } /* Get all files in directory */ string logPath = LogPath; DirectoryInfo dir = new DirectoryInfo(logPath); if (!dir.Exists) { return(0); } FileSystemInfo[] infos = dir.GetFiles("error-*.xml"); if (infos.Length < 1) { return(0); } string[] files = new string[infos.Length]; int count = 0; /* Get files that are not marked with system and hidden attributes */ foreach (FileSystemInfo info in infos) { if (IsUserFile(info.Attributes)) { files[count++] = Path.Combine(logPath, info.Name); } } InvariantStringArray.Sort(files, 0, count); Array.Reverse(files, 0, count); if (errorEntryList != null) { /* Find the proper page */ int firstIndex = pageIndex * pageSize; int lastIndex = (firstIndex + pageSize < count) ? firstIndex + pageSize : count; /* Open them up and rehydrate the list */ for (int i = firstIndex; i < lastIndex; i++) { XmlTextReader reader = new XmlTextReader(files[i]); try { while (reader.IsStartElement("error")) { string id = reader.GetAttribute("errorId"); Error error = ErrorXml.Decode(reader); errorEntryList.Add(new ErrorLogEntry(this, id, error)); } } finally { reader.Close(); } } } /* Return how many are total */ return(count); }
/// <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"); } var errorXml = ErrorXml.EncodeString(error); var id = Guid.NewGuid(); using (var connection = CreateOpenConnection()) using (var command = connection.CreateCommand()) using (var transaction = connection.BeginTransaction()) { // because we are storing the XML data in a NClob, we need to jump through a few hoops!! // so first we've got to operate within a transaction command.Transaction = transaction; // then we need to create a temporary lob on the database server command.CommandText = "declare xx nclob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;"; command.CommandType = CommandType.Text; var parameters = command.Parameters; AddProviderSpecificTypeParameter(command, "tempblob", ThisProviderInfo.ClobDbType).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); object xmlValue; if (parameters[0].Value is string) { xmlValue = errorXml; } else { // now we can get a handle to the NClob // TODO Review where Stream needs disposing var stream = (Stream)parameters[0].Value; // create a temporary buffer in which to store the XML var bytes = Encoding.Unicode.GetBytes(errorXml); // and finally we can write to it! stream.Write(bytes, 0, bytes.Length); xmlValue = stream; } command.CommandText = SchemaOwner + "pkg_elmah$log_error.LogError"; command.CommandType = CommandType.StoredProcedure; parameters.Clear(); var addParameter = command.ParameterAdder(); addParameter("v_ErrorId", DbType.String, id.ToString("N")); addParameter("v_Application", DbType.String, ApplicationName); addParameter("v_Host", DbType.String, error.HostName); addParameter("v_Type", DbType.String, error.Type); addParameter("v_Source", DbType.String, error.Source); addParameter("v_Message", DbType.String, error.Message); addParameter("v_User", DbType.String, error.User); AddProviderSpecificTypeParameter(command, "v_AllXml", ThisProviderInfo.ClobDbType).Value = xmlValue; addParameter("v_StatusCode", DbType.Int32, error.StatusCode); addParameter("v_TimeUtc", DbType.DateTime, error.Time.ToUniversalTime()); command.ExecuteNonQuery(); transaction.Commit(); return(id.ToString()); } }
/// <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)); }