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